-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
275 lines (231 loc) · 6.71 KB
/
main.c
File metadata and controls
275 lines (231 loc) · 6.71 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PHYSCHECK_VISION 1
#define PHYSCHECK_HEIGHT 2
#define PHYSCHECK_NAME 4
typedef struct {
double vision;
int height;
} Body;
typedef struct {
char *name;
Body body;
} PhysCheck;
typedef struct __node {
PhysCheck data;
struct __node *prev;
struct __node *next;
} Dnode;
typedef struct {
Dnode *head;
Dnode *crnt;
} Dlist;
int PhysCheckHeightCmp(const PhysCheck *x, const PhysCheck *y) {
return x->body.height < y->body.height ? -1 : x->body.height > y->body.height ? 1 : 0;
}
int PhysCheckVisionCmp(const PhysCheck *x, const PhysCheck *y) {
return x->body.vision < y->body.vision ? -1 : x->body.vision > y->body.vision ? 1 : 0;
}
int PhysCheckNameCmp(const PhysCheck *x, const PhysCheck *y) {
return strcmp(x->name, y->name);
}
void PrintPhysCheck(const PhysCheck *x) {
printf("%0.2lf %d %s", x->body.vision, x->body.height, x->name);
}
void PrintLnPhysCheck(const PhysCheck *x) {
printf("%0.2lf %d %s\n", x->body.vision, x->body.height, x->name);
}
PhysCheck ScanPhysCheck(const char *message, int sw) {
PhysCheck temp;
printf("Please input data to %s\n", message);
if (sw & PHYSCHECK_VISION) {
printf("vision: ");
scanf("%lf", &temp.body.vision);
}
if (sw & PHYSCHECK_HEIGHT) {
printf("height: ");
scanf("%d", &temp.body.height);
}
if (sw & PHYSCHECK_NAME) {
temp.name = calloc(82, sizeof(char));
printf("name: ");
scanf("%s", temp.name);
}
return temp;
}
static Dnode *AllocNode(void) {
return calloc(1, sizeof(Dnode));
}
static void SetNode(Dnode *n, const PhysCheck *x, const Dnode *prev, const Dnode *next) {
n->data = *x;
n->prev = (Dnode *) prev;
n->next = (Dnode *) next;
}
static int IsEmpty(const Dlist *list) {
return list->head->next == list->head;
}
void Initialize(Dlist *list) {
Dnode *dummyNode = AllocNode();
list->head = list->crnt = dummyNode;
dummyNode->prev = dummyNode->next = dummyNode;
}
void PrintCurrent(const Dlist *list) {
if (IsEmpty(list))
printf("The current node does not exist.");
else
PrintPhysCheck(&list->crnt->data);
}
void PrintLnCurrent(const Dlist *list) {
PrintCurrent(list);
putchar('\n');
}
Dnode *Search(Dlist *list, const PhysCheck *x,
int compare(const PhysCheck *x, const PhysCheck *y)) {
Dnode *ptr = list->head->next;
while (ptr != list->head) {
if (compare(&ptr->data, x) == 0) {
list->crnt = ptr;
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
void Print(const Dlist *list) {
if (IsEmpty(list))
puts("The node does not exist.");
else {
Dnode *ptr = list->head->next;
puts("---List---");
while (ptr != list->head) {
PrintLnPhysCheck(&ptr->data);
ptr = ptr->next;
}
}
}
void InsertAfter(Dlist *list, Dnode *p, const PhysCheck *x) {
Dnode *ptr = AllocNode();
Dnode *nxt = p->next;
p->next = p->next->prev = ptr;
SetNode(ptr, x, p, nxt);
list->crnt = ptr;
}
void InsertFront(Dlist *list, const PhysCheck *x) {
InsertAfter(list, list->head, x);
}
void InsertRear(Dlist *list, const PhysCheck *x) {
InsertAfter(list, list->head->prev, x);
}
void Remove(Dlist *list, Dnode *p) {
p->prev->next = p->next;
p->next->prev = p->prev;
list->crnt = p->prev;
free(p);
if (list->crnt == list->head)
list->crnt = list->head->next;
}
void RemoveFront(Dlist *list) {
if (!IsEmpty(list))
Remove(list, list->head->next);
}
void RemoveRear(Dlist *list) {
if (!IsEmpty(list))
Remove(list, list->head->prev);
}
void RemoveCurrent(Dlist *list) {
if (list->crnt != list->head)
Remove(list, list->crnt);
}
void Clear(Dlist *list) {
while (!IsEmpty(list))
RemoveFront(list);
}
void Terminate(Dlist *list) {
Clear(list);
free(list->head);
}
typedef enum {
TERMINATE, INS_FRONT, INS_REAR, RMV_FRONT, RMV_REAR, PRINT_CRNT,
RMV_CRNT, SRCH_VISION, SRCH_HEIGHT, SRCH_NAME, PRINT_ALL, CLEAR
} Menu;
Menu SelectMenu(void) {
int i, ch;
char *mstring[] = {
"Insert Front", "Insert Rear", "Remove Front",
"Remove Rear", "Print Current", "Remove Current",
"Search Vision", "Search Height", "Search Name",
"Print All", "Clear All"
};
do {
for (i = TERMINATE; i < CLEAR; i++) {
printf("(%2d) %-18.18s ", i + 1, mstring[i]);
if ((i % 3) == 2)
putchar('\n');
}
printf("( 0) End :");
scanf("%d", &ch);
} while (ch < TERMINATE || ch > CLEAR);
return (Menu) ch;
}
int main(void) {
Menu menu;
Dlist list;
PhysCheck x;
Initialize(&list);
do {
switch (menu = SelectMenu()) {
case INS_FRONT:
x = ScanPhysCheck("insert front", PHYSCHECK_VISION | PHYSCHECK_HEIGHT | PHYSCHECK_NAME);
InsertFront(&list, &x);
break;
case INS_REAR:
x = ScanPhysCheck("insert rear", PHYSCHECK_VISION | PHYSCHECK_HEIGHT | PHYSCHECK_NAME);
InsertRear(&list, &x);
break;
case RMV_FRONT:
RemoveFront(&list);
break;
case RMV_REAR:
RemoveRear(&list);
break;
case RMV_CRNT:
RemoveCurrent(&list);
break;
case PRINT_CRNT:
PrintLnCurrent(&list);
break;
case SRCH_VISION:
x = ScanPhysCheck("search", PHYSCHECK_VISION);
if (Search(&list, &x, PhysCheckVisionCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The vision does not exist.");
break;
case SRCH_HEIGHT:
x = ScanPhysCheck("search", PHYSCHECK_HEIGHT);
if (Search(&list, &x, PhysCheckHeightCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The height does not exist.");
break;
case SRCH_NAME:
x = ScanPhysCheck("search", PHYSCHECK_NAME);
if (Search(&list, &x, PhysCheckNameCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The name does not exist.");
break;
case PRINT_ALL:
Print(&list);
break;
case CLEAR:
Clear(&list);
break;
default:
break;
}
} while (menu != TERMINATE);
Terminate(&list);
return 0;
}