-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.c
377 lines (293 loc) · 8.33 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
376
#include "student.h"
#include <stdlib.h>
struct _student_list {
struct _student_object student;
struct _student_list *next;
};
#define list_next_is_null(list_addr) \
(((list_addr)->next) == NULL)
#define list_is_empty(list_addr) \
((*((unsigned int *)(list_addr)) == 0) && (list_next_is_null(list_addr)))
#define list_generate_next(list_addr_variable) \
list_addr_variable = ((list_addr_variable)->next)
#define swap_variable(variable0, variable1) do { \
variable0 = (variable0) ^ (variable1); \
variable1 = (variable0) ^ (variable1); \
variable0 = (variable0) ^ (variable1); \
} while (0)
#define uint32_sizeof (sizeof(unsigned int))
student_base_t student_object_get_name_length(struct _student_object *object)
{
student_base_t count;
unsigned char *name_addr;
count = 0;
name_addr = object->info.name;
while (*(name_addr++) != '\0')
++count;
return count;
}
student_bool_t student_list_is_empty(struct _student_list *list)
{
return list_is_empty(list);
}
static student_ret_t student_object_name_create(struct _student_object *object,
student_base_t name_length)
{
/* 字符串尾部要放空字符'0',所以length + 1 */
object->info.name =
(unsigned char *)malloc(sizeof(unsigned char) * (name_length + 1));
if ((object->info.name) == NULL)
return STUDENT_MEM_ERROR;
*(object->info.name + name_length) = '\0';
return STUDENT_OK;
}
static void student_object_name_copy(struct _student_object *restrict src_object,
struct _student_object *restrict dest_object)
{
student_base_t name_length;
unsigned char *src_name_addr, *dest_name_addr;
name_length = student_object_get_name_length(src_object);
student_object_name_create(dest_object, name_length);
src_name_addr = src_object->info.name;
dest_name_addr = dest_object->info.name;
while (name_length--)
*(dest_name_addr++) = *(src_name_addr++);
}
static inline void student_object_name_remove(struct _student_object *object)
{
if ((object->info.name) != NULL)
free(object->info.name);
}
static void student_list_init(struct _student_list *list)
{
unsigned int *u32_list;
u32_list = (unsigned int *)list;
*u32_list = 0;
list->next = NULL;
}
static void student_object_copy(struct _student_object *restrict object_src,
struct _student_object *restrict object_dest)
{
size_t u8_bytes, u32_bytes;
unsigned char *u8_src, *u8_dest;
unsigned int *u32_src, *u32_dest;
u8_src = (unsigned char *)object_src;
u8_dest = (unsigned char *)object_dest;
u32_src = (unsigned int *)object_src;
u32_dest = (unsigned int *)object_dest;
u8_bytes = sizeof(struct _student_object);
if (u8_bytes >= uint32_sizeof)
u32_bytes = u8_bytes / uint32_sizeof;
else
u32_bytes = 0;
while (u32_bytes--) {
*(u32_dest++) = *(u32_src++);
u8_bytes -= uint32_sizeof;
}
while (u8_bytes--) {
*(u8_dest++) = *(u8_src++);
}
}
void student_object_attr_export(struct _student_object *restrict object,
struct _student_object *restrict attr)
{
student_object_copy(object, attr);
}
void student_object_modify(struct _student_object *restrict object,
struct _student_object *restrict attr)
{
student_object_name_remove(object);
student_object_copy(attr, object);
student_object_name_copy(attr, object);
}
static inline void student_object_free(struct _student_object *object)
{
student_object_name_remove(object);
free(object);
}
struct _student_list *student_list_create(void)
{
struct _student_list *list;
list = (struct _student_list *)malloc(sizeof(struct _student_list));
if (list == NULL)
return NULL;
student_list_init(list);
return list;
}
static inline struct _student_object *student_object_create(void)
{
return (struct _student_object *)student_list_create();
}
struct _student_object *student_list_generate(struct _student_list **list)
{
struct _student_object *object;
if ((*list == NULL) || list_is_empty(*list))
return NULL;
object = (struct _student_object *)(*list);
if (list_next_is_null(*list))
*list = NULL;
else
list_generate_next(*list);
return object;
}
struct _student_object *student_list_get_last(struct _student_list *list)
{
while (! list_next_is_null(list))
list_generate_next(list);
return (struct _student_object *)list;
}
struct _student_object *student_list_get_with_num(struct _student_list *list,
student_base_t num)
{
struct _student_object *object;
object = student_list_generate(&list);
while (object != NULL) {
if ((object->info.num) == num)
return object;
else
object = student_list_generate(&list);
}
return NULL;
}
struct _student_object *student_list_append(struct _student_list *list,
struct _student_object *attr)
{
struct _student_object *object;
if (list_is_empty(list)) {
object = (struct _student_object *)list;
} else {
object = student_object_create();
if (object == NULL)
return object;
if (! list_next_is_null(list))
list = (struct _student_list *)student_list_get_last(list);
list->next = (struct _student_list *)object;
}
student_object_copy(attr, object);
student_object_name_copy(attr, object);
return object;
}
student_ret_t student_list_remove(struct _student_list **restrict list,
struct _student_object *object)
{
struct _student_list *previous_list_node;
if ((struct _student_object *)(*list) == object) {
if (list_next_is_null(*list)) {
student_object_name_remove(object);
student_list_init(*list);
return STUDENT_CAN_NOT_REMOVE;
} else {
*list = (*list)->next;
student_object_free(object);
return STUDENT_OK;
}
}
previous_list_node = &(**list);
list = &((*list)->next);
while (*list != NULL) {
if (*list == (struct _student_list *)object) {
if (list_next_is_null(*list))
previous_list_node->next = NULL;
else
previous_list_node->next = (*list)->next;
student_object_free(object);
return STUDENT_OK;
}
previous_list_node = *list;
list = &((*list)->next);
}
return STUDENT_CAN_NOT_REMOVE;
}
student_ret_t student_list_remove_with_num(struct _student_list **restrict list,
student_base_t num)
{
struct _student_object *object;
object = student_list_get_with_num(*list, num);
if (object == NULL)
return STUDENT_NOT_FOUND;
student_list_remove(list, object);
return STUDENT_OK;
}
student_base_t student_list_count(struct _student_list *list)
{
student_base_t count;
count = 0;
if (list_is_empty(list))
return count;
while (! list_next_is_null(list)) {
++count;
list_generate_next(list);
}
return (count + 1);
}
void student_object_swap(struct _student_object *restrict object0,
struct _student_object *restrict object1)
{
size_t u8_bytes, u32_bytes;
unsigned char *u8_object0, *u8_object1;
unsigned int *u32_object0, *u32_object1;
u8_bytes = sizeof(struct _student_object);
u8_object0 = (unsigned char *)object0;
u8_object1 = (unsigned char *)object1;
u32_object0 = (unsigned int *)object0;
u32_object1 = (unsigned int *)object1;
if (u8_bytes >= uint32_sizeof)
u32_bytes = u8_bytes / uint32_sizeof;
else
u32_bytes = 0;
while (u32_bytes--) {
swap_variable(*u32_object0, *u32_object1);
++u32_object0;
++u32_object1;
u8_bytes -= uint32_sizeof;
}
while (u8_bytes--) {
swap_variable(*u8_object0, *u8_object1);
++u8_object0;
++u8_object1;
}
}
void student_object_scores_sum(struct _student_object *object)
{
student_base_t member_count;
float *f32_object;
member_count = sizeof(struct _student_scores) / sizeof(float);
member_count -= 1;
f32_object = (float *)(&(object->scores));
object->scores.total = 0;
while (member_count--)
(object->scores.total) += *(f32_object++);
}
void student_list_sort_by_score(struct _student_list *list)
{
struct _student_list *list_head, *list_temp;
if (list_is_empty(list))
return;
list_head = list;
while (list != NULL) {
list_temp = list_head;
while (! list_next_is_null(list_temp)) {
if ((list_temp->student.scores.total)
> (list_temp->next->student.scores.total))
student_object_swap((struct _student_object *)(list_temp),
(struct _student_object *)(list_temp->next));
list_generate_next(list_temp);
}
list = list->next;
}
}
void student_list_clear(struct _student_list **restrict list)
{
while (student_list_remove(list, (struct _student_object *)(*list))
!= STUDENT_CAN_NOT_REMOVE)
continue;
}
void student_list_delete(struct _student_list **restrict list)
{
if (list_is_empty(*list))
goto free_self;
student_list_clear(list);
free_self:
free(*list);
*list = NULL;
}