Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

flash_light.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "flash_light.h"
  4. typedef struct pkt_cell *pkt_cell_t; // 电池接口
  5. struct pkt_cell
  6. {
  7. void (*init)(void *t);
  8. void (*set_link_to_next)(void *t, pkt_cell_t cell);
  9. int (*get_power)(void *t);
  10. };
  11. typedef struct pkt_light *pkt_light_t; // 手电筒接口
  12. struct pkt_light
  13. {
  14. void (*init)(void *t);
  15. void (*add_cell)(void *t, void *cell);
  16. int (*power)(void *t);
  17. };
  18. typedef struct pkt_panasonic_cell *pkt_panasonic_cell_t; // 松下电池类
  19. struct pkt_panasonic_cell
  20. {
  21. struct pkt_cell cell;
  22. int pwr;
  23. const char *name;
  24. pkt_cell_t next;
  25. };
  26. typedef struct pkt_cat_cell *pkt_cat_cell_t; // 黑猫电池类
  27. struct pkt_cat_cell
  28. {
  29. struct pkt_cell cell;
  30. int pwr;
  31. const char *name;
  32. pkt_cell_t next;
  33. };
  34. typedef struct pkt_falsh_light *pkt_falsh_light_t; // 手电筒类
  35. struct pkt_falsh_light
  36. {
  37. struct pkt_light light;
  38. pkt_cell_t head; /* 用于存储多态对象的链表头 */
  39. pkt_cell_t tail; /* 用于存储多态对象的链表尾 */
  40. };
  41. // 松下电池接口实现
  42. void pkt_panasonic_cell_init(void *t)
  43. {
  44. pkt_panasonic_cell_t this = (pkt_panasonic_cell_t)t;
  45. this->name = "panasonic";
  46. this->pwr = 20;
  47. this->next = NULL;
  48. }
  49. void pkt_panasonic_cell_set_link_to_next(void *t, pkt_cell_t cell)
  50. {
  51. pkt_panasonic_cell_t this = (pkt_panasonic_cell_t)t;
  52. this->next = cell;
  53. }
  54. int pkt_panasonic_cell_get_power(void *t)
  55. {
  56. pkt_panasonic_cell_t this = (pkt_panasonic_cell_t)t;
  57. pkt_cell_t pc = this->next;
  58. if (NULL == pc)
  59. {
  60. return this->pwr;
  61. }
  62. else
  63. {
  64. pc = this->next;
  65. return (this->pwr + pc->get_power(pc));
  66. }
  67. }
  68. pkt_panasonic_cell_t pkt_structure_panasonic_cell(void)
  69. {
  70. pkt_panasonic_cell_t n = (pkt_panasonic_cell_t)malloc(sizeof(struct pkt_panasonic_cell));
  71. if (NULL == n) return NULL;
  72. n->cell.init = pkt_panasonic_cell_init;
  73. n->cell.set_link_to_next = pkt_panasonic_cell_set_link_to_next;
  74. n->cell.get_power = pkt_panasonic_cell_get_power;
  75. return n;
  76. }
  77. // 黑猫电池接口实现
  78. void pkt_cat_cell_init(void *t)
  79. {
  80. pkt_cat_cell_t this = (pkt_cat_cell_t)t;
  81. this->name = "cat";
  82. this->pwr = 15;
  83. this->next = NULL;
  84. }
  85. void pkt_cat_cell_set_link_to_next(void *t, pkt_cell_t cell)
  86. {
  87. pkt_cat_cell_t this = (pkt_cat_cell_t)t;
  88. this->next = cell;
  89. }
  90. int pkt_cat_cell_get_power(void *t)
  91. {
  92. pkt_cat_cell_t this = (pkt_cat_cell_t)t;
  93. pkt_cell_t pc = this->next;
  94. if (NULL == pc)
  95. {
  96. return this->pwr;
  97. }
  98. else
  99. {
  100. pc = this->next;
  101. return (this->pwr + pc->get_power(pc)); // 递归
  102. }
  103. }
  104. pkt_cat_cell_t pkt_structure_cat_cell(void)
  105. {
  106. pkt_cat_cell_t n = (pkt_cat_cell_t)malloc(sizeof(struct pkt_cat_cell));
  107. if (NULL == n) return NULL;
  108. n->cell.init = pkt_cat_cell_init;
  109. n->cell.set_link_to_next = pkt_cat_cell_set_link_to_next;
  110. n->cell.get_power = pkt_cat_cell_get_power;
  111. return n;
  112. }
  113. // 手电筒接口实现
  114. void pkt_light_init(void *t)
  115. {
  116. pkt_falsh_light_t this = (pkt_falsh_light_t)t;
  117. this->head = this->tail = NULL;
  118. }
  119. void pkt_light_add_cell(void *t, void *cell)
  120. {
  121. pkt_falsh_light_t this = (pkt_falsh_light_t)t;
  122. pkt_cell_t tcell = (pkt_cell_t)cell;
  123. pkt_cell_t pc;
  124. if(this->head == NULL) // 第一个节点
  125. {
  126. this->head = tcell;
  127. this->tail = this->head;
  128. }
  129. else
  130. {
  131. pc = this->tail;
  132. pc->set_link_to_next(pc, tcell);
  133. this->tail = tcell;
  134. }
  135. }
  136. int pkt_light_power(void *t)
  137. {
  138. pkt_falsh_light_t this = (pkt_falsh_light_t)t;
  139. pkt_cell_t pc = this->head;
  140. return pc->get_power(pc);
  141. }
  142. pkt_falsh_light_t pkt_structure_falsh_light_cell(void)
  143. {
  144. pkt_falsh_light_t n = (pkt_falsh_light_t)malloc(sizeof(struct pkt_falsh_light));
  145. if (NULL == n) return NULL;
  146. n->light.init = pkt_light_init;
  147. n->light.add_cell = pkt_light_add_cell;
  148. n->light.power = pkt_light_power;
  149. return n;
  150. }
  151. int main(void)
  152. {
  153. // 构造对象
  154. pkt_falsh_light_t falsh_light = pkt_structure_falsh_light_cell();
  155. pkt_panasonic_cell_t panasonic_cell = pkt_structure_panasonic_cell();
  156. pkt_cat_cell_t cat_cell = pkt_structure_cat_cell();
  157. pkt_cat_cell_t cat_cell1 = pkt_structure_cat_cell();
  158. // 初始化对象
  159. falsh_light->light.init(falsh_light);
  160. panasonic_cell->cell.init(panasonic_cell);
  161. cat_cell->cell.init(cat_cell);
  162. cat_cell->cell.init(cat_cell1);
  163. // 将电池串联到手电筒里面
  164. falsh_light->light.add_cell(falsh_light, panasonic_cell); /* 串联一节松下电池到手电筒 */
  165. falsh_light->light.add_cell(falsh_light, cat_cell); /* 串联一节黑猫电池到手电筒 */
  166. falsh_light->light.add_cell(falsh_light, cat_cell1); /* 串联一节黑猫电池到手电筒 */
  167. // 打印手电筒的电量
  168. printf("falsh light power = %d\n", falsh_light->light.power(falsh_light));
  169. return 0;
  170. }