瀏覽代碼

first commit

master
huangyulong 4 年之前
當前提交
3dca8bdaa3
共有 6 個文件被更改,包括 193 次插入0 次删除
  1. 0
    0
      README.md
  2. 二進制
      oopc_interface/flash_light
  3. 181
    0
      oopc_interface/flash_light.c
  4. 5
    0
      oopc_interface/flash_light.h
  5. 二進制
      oopc_interface/flash_light.o
  6. 7
    0
      oopc_interface/makefile

+ 0
- 0
README.md 查看文件


二進制
oopc_interface/flash_light 查看文件


+ 181
- 0
oopc_interface/flash_light.c 查看文件

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

+ 5
- 0
oopc_interface/flash_light.h 查看文件

@@ -0,0 +1,5 @@
1
+#ifndef _FLASH_LIGHT_H_
2
+#define _FLASH_LIGHT_H_
3
+
4
+
5
+#endif

二進制
oopc_interface/flash_light.o 查看文件


+ 7
- 0
oopc_interface/makefile 查看文件

@@ -0,0 +1,7 @@
1
+CFLAGS = -Wall
2
+
3
+flash_light: flash_light.o
4
+
5
+.PHONY clean:
6
+clean:
7
+	rm flash_light flash_light.o

Loading…
取消
儲存