-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.c
375 lines (314 loc) · 8.42 KB
/
student.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <stdlib.h>
#include "student.h"
#include "list.h"
// 接收数组
char *recv_buffer[ID_LEN];
int recv_idx;
static void Student_fromcopyTo(Student_t *s1, Student_t *s2)
{
memcpy(s2->name, s1->name, sizeof(s1->name));
s2->score = s1->score;
s2->age = s1->age;
s2->sex = s1->sex;
memcpy(s2->id, s1->id, sizeof(s1->id));
}
static void swapStudent(Student_t *s1, Student_t *s2)
{
Student_t s3;
Student_fromcopyTo(s1, &s3);
Student_fromcopyTo(s2, s1);
Student_fromcopyTo(&s3, s2);
}
bool StudentManager_init(StudentManger_t *manage, int maxlen)
{
// 初始化链表
klist_init(&manage->all);
manage->sortTree = SortTree_Init("-1");
manage->capacity = maxlen;
manage->append = StudentManager_add;
manage->get_len = StudentManager_get_len;
manage->display = StudentManager_display;
manage->delt = StudentManager_delete;
manage->insert = StudentManager_insert;
manage->indexOfNum = StudentManager_indexOfNum;
manage->indexOfId = StudentManager_indexOfId;
manage->indexOfName = StudentManager_indexOfName;
manage->sort = StudentManager_sort;
return true;
}
Student_t *Student_init(const char *name, int score, int age, const char *id, Sex_t sex)
{
Student_t *s = (Student_t *)malloc(sizeof(Student_t));
memcpy(s->name, name, sizeof(name));
s->score = score;
s->age = age;
s->sex = sex;
memcpy(s->id, id, sizeof(id));
s->get_name = Student_get_name;
s->get_score = Student_get_score;
s->get_age = Student_get_age;
s->get_sex = Student_get_sex;
s->get_id = Student_get_id;
return s;
}
bool Student_free(Student_t *s)
{
if (s == nullptr)
{
return false;
}
free(s);
return true;
}
bool StudentManager_add(struct _StudentManger *manage, Student_t *s, uint8_t METHOD)
{
switch (METHOD)
{
case INSERT_HEAD:
klist_push(&manage->all, &s->node);
SortTree_insert(manage->sortTree, s->id);
break;
case INSERT_TAIL:
klist_append(&manage->all, &s->node);
SortTree_insert(manage->sortTree, s->id);
break;
default:
return false;
break;
}
return true;
}
void StudentManager_display(struct _StudentManger *manage, Student_t *target)
{
int len = manage->get_len(manage);
printf("序号\t姓名\t性别\t年龄\t学号\t分数\n");
klist_node_t *temp = manage->all.head.next;
for (int i = 0; i < len; i++)
{
Student_t *s = KLIST_STRUCT_ADDR(temp, Student_t, node);
char sex[5];
memset(sex, 0, sizeof(sex));
if (s->sex == BOY)
{
strcpy(sex, "boy");
}
else
{
strcpy(sex, "girl");
}
// 加入单体查询
if (s != SHOW_ALL && target == s)
{
printf("\033[1;32m%d\t%s\t%s\t%d\t%s\t%d\033[0m\n", i, s->name, sex, s->age, s->id, s->score);
}
else
{
printf("%d\t%s\t%s\t%d\t%s\t%d\n", i, s->name, sex, s->age, s->id, s->score);
}
// 整体查询
// printf("%d\t%s\t%s\t%s\t%d\n", i, s->name, sex, s->id, s->score);
temp = temp->next;
}
puts("");
}
bool StudentManager_delete(struct _StudentManger *manage, Student_t *s)
{
int n = manage->get_len(manage);
if (n <= 0)
{
return false;
}
SortTree_delete(manage->sortTree, manage->sortTree, s->id);
bool ret = klist_delete(&manage->all, &s->node);
Student_free(s);
if (!ret)
{
return false;
}
return true;
}
bool StudentManager_insert(struct _StudentManger *manage, Student_t *s, int idx)
{
int n = manage->get_len(manage);
// 容量是否满
if (n >= manage->capacity)
{
return false;
}
// 链表为空
if (n == 0)
{
klist_push(&manage->all, &s->node);
SortTree_insert(manage->sortTree, s->id);
return true;
}
// 检查是否超界
if (idx >= n)
{
idx = n - 1;
}
// 为负数则在开头插入
if (idx <= -1)
{
klist_push(&manage->all, &s->node);
SortTree_insert(manage->sortTree, s->id);
return true;
}
klist_node_t *node = manage->all.head.next;
for (int i = 0; i < idx; i++)
{
node = node->next;
}
if (node != nullptr)
{
klist_insert(&manage->all, node, &s->node);
SortTree_insert(manage->sortTree, s->id);
return true;
}
return false;
}
int StudentManager_get_len(struct _StudentManger *manage)
{
return klist_len(&manage->all);
}
Student_t *StudentManager_indexOfNum(struct _StudentManger *manage, int idx)
{
int n = manage->get_len(manage);
if (n == 0)
{
return nullptr;
}
if (idx >= n)
{
return nullptr;
}
klist_node_t *node = manage->all.head.next;
for (int i = 0; i < idx; i++)
{
node = node->next;
}
if (node)
{
return KLIST_STRUCT_ADDR(node, Student_t, node);
}
return nullptr;
}
Student_t *StudentManager_indexOfId(struct _StudentManger *manage, const char *id)
{
int n = manage->get_len(manage);
if (n == 0)
{
return nullptr;
}
klist_node_t *node = manage->all.head.next;
while (node && node != &manage->all.tail)
{
Student_t *s = KLIST_STRUCT_ADDR(node, Student_t, node);
if (strcmp(s->id, id) == 0)
{
return s;
}
node = node->next;
}
return nullptr;
}
Student_t *StudentManager_indexOfName(struct _StudentManger *manage, const char *name)
{
int n = manage->get_len(manage);
if (n == 0)
{
return nullptr;
}
klist_node_t *node = manage->all.head.next;
while (node && node != &manage->all.tail)
{
Student_t *s = KLIST_STRUCT_ADDR(node, Student_t, node);
if (strcmp(s->name, name) == 0)
{
return s;
}
node = node->next;
}
return nullptr;
}
bool StudentManager_sort(struct _StudentManger *manage, uint8_t METHOD)
{
if (manage->get_len(manage) == 0)
{
return false;
}
Student_t s_arr[STUDENT_LEN];
switch (METHOD)
{
case SORT_UPPER:
// 冒泡排序
// for (klist_node_t *node = manage->all.tail.prev; node != &manage->all.head; node = node->prev)
// {
// for (klist_node_t *nodej = manage->all.head.next; (nodej->next != nullptr) && (nodej->next->next != node); nodej = nodej->next)
// {
// Student_t *pnode = KLIST_STRUCT_ADDR(nodej, Student_t, node);
// Student_t *nnode = KLIST_STRUCT_ADDR(nodej->next, Student_t, node);
// if (pnode->score < nnode->score)
// {
// swapStudent(pnode, nnode);
// }
// }
// }
recv_idx = 0;
SortTree_show(manage->sortTree);
memset(s_arr, 0, sizeof(Student_t) * STUDENT_LEN);
for (int i = 1; i < recv_idx; i++)
{
Student_t *ts = manage->indexOfId(manage, recv_buffer[i]);
Student_fromcopyTo(ts, &s_arr[i]);
}
recv_idx--;
for (klist_node_t *node = manage->all.head.next; node != nullptr && node != &manage->all.tail; node = node->next)
{
Student_t *s2 = KLIST_STRUCT_ADDR(node, Student_t, node);
Student_fromcopyTo(&s_arr[recv_idx--], s2);
}
break;
case SORT_LOWER:
// 二叉排序树
recv_idx = 0;
SortTree_show(manage->sortTree);
memset(s_arr, 0, sizeof(Student_t) * STUDENT_LEN);
for (int i = 1; i < recv_idx; i++)
{
Student_t *ts = manage->indexOfId(manage, recv_buffer[i]);
Student_fromcopyTo(ts, &s_arr[i]);
}
recv_idx = 1;
for (klist_node_t *node = manage->all.head.next; node != nullptr && node != &manage->all.tail; node = node->next)
{
Student_t *s2 = KLIST_STRUCT_ADDR(node, Student_t, node);
Student_fromcopyTo(&s_arr[recv_idx++], s2);
}
break;
default:
return false;
break;
}
return true;
}
const char *Student_get_name(struct _Student *s)
{
return s->name;
}
const int Student_get_score(struct _Student *s)
{
return s->score;
}
const int Student_get_age(struct _Student *s)
{
return s->age;
}
const Sex_t Student_get_sex(struct _Student *s)
{
return s->sex;
}
const char *Student_get_id(struct _Student *s)
{
return s->id;
}