-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
286 lines (243 loc) · 7.14 KB
/
main.c
File metadata and controls
286 lines (243 loc) · 7.14 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
276
277
278
279
280
281
282
283
284
285
286
#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 *next;
} Node;
typedef struct {
Node *head;
Node *crnt;
} List;
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("%sするデータを入力してください。\n", message);
if (sw & PHYSCHECK_VISION) {
printf("視力:");
scanf("%lf", &temp.body.vision);
}
if (sw & PHYSCHECK_HEIGHT) {
printf("身長:");
scanf("%d", &temp.body.height);
}
if (sw & PHYSCHECK_NAME) {
temp.name = calloc(82, sizeof(char));
printf("氏名:");
scanf("%s", temp.name);
}
return temp;
}
static Node *AllocNode(void) {
return calloc(1, sizeof(Node));
}
static void SetNode(Node *n, const PhysCheck *x, const Node *next) {
n->data = *x;
n->next = next;
}
void Initialize(List *list) {
list->head = NULL;
list->crnt = NULL;
}
Node *Search(List *list, const PhysCheck *x,
int compare(const PhysCheck *x, const PhysCheck *y)) {
Node *ptr = list->head;
while (ptr != NULL) {
if (compare(&ptr->data, x) == 0) {
list->crnt = ptr;
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
void InsertFront(List *list, const PhysCheck *x) {
Node *ptr = list->head;
list->head = list->crnt = AllocNode();
SetNode(list->head, x, ptr);
}
void InsertRear(List *list, const PhysCheck *x) {
if (list->head == NULL)
InsertFront(list, x);
else {
Node *ptr = list->head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = list->crnt = AllocNode();
SetNode(ptr->next, x, NULL);
}
}
void RemoveFront(List *list) {
if (list->head != NULL) {}
Node *ptr = list->head->next;
free(list->head->data.name);
free(list->head);
list->head = list->crnt = ptr;
}
void RemoveRear(List *list) {
if (list->head != NULL) {
if ((list->head)->next == NULL)
RemoveFront(list);
else {
Node *ptr = list->head;
Node *pre;
while (ptr->next != NULL) {
pre = ptr;
ptr = ptr->next;
}
pre->next = NULL;
free(ptr);
list->crnt = pre;
}
}
}
void RemoveCurrent(List *list) {
if (list->head != NULL) {
if (list->crnt == list->head)
RemoveFront(list);
else {
Node *ptr = list->head;
while (ptr->next != list->crnt)
ptr = ptr->next;
ptr->next = list->crnt->next;
free(list->crnt);
list->crnt = ptr;
}
}
}
void Clear(List *list) {
while (list->head != NULL)
RemoveFront(list);
list->crnt = NULL;
}
void PrintCurrent(const List *list) {
if (list->crnt == NULL)
printf("着目ノードはありません。");
else
PrintPhysCheck(&list->crnt->data);
}
void PrintLnCurrent(const List *list) {
PrintCurrent(list);
putchar('\n');
}
void Print(const List *list) {
if (list->head == NULL)
puts("ノードがありません。");
else {
Node *ptr = list->head;
puts("【一覧表】");
while (ptr != NULL) {
PrintLnPhysCheck(&ptr->data);
ptr = ptr->next;
}
}
}
void Terminate(List *list) {
Clear(list);
}
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[] = {
"先頭にノードを挿入", "末尾にノードを挿入", "先頭のノードを削除",
"末尾のノードを削除", "着目ノードを表示", "着目ノードを削除",
"視力で探索", "身長で探索", "氏名で探索", "全ノードを表示", "全ノードを削除"
};
do {
for (i = TERMINATE; i < CLEAR; i++) {
printf("(%2d) %-21s \t", i + 1, mstring[i]);
if ((i % 3) == 2)
putchar('\n');
}
printf("( 0) 終了 :");
scanf("%d", &ch);
} while (ch < TERMINATE || ch > CLEAR);
return (Menu) ch;
}
int main(void) {
Menu menu;
List list;
PhysCheck x;
Initialize(&list);
do {
switch (menu = SelectMenu()) {
case INS_FRONT:
x = ScanPhysCheck("先頭に挿入", PHYSCHECK_VISION | PHYSCHECK_HEIGHT | PHYSCHECK_NAME);
InsertFront(&list, &x);
break;
case INS_REAR:
x = ScanPhysCheck("末尾に挿入", 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("探索", PHYSCHECK_VISION);
if (Search(&list, &x, PhysCheckVisionCmp) != NULL)
PrintLnCurrent(&list);
else
puts("その視力のデータはありません。");
break;
case SRCH_HEIGHT:
x = ScanPhysCheck("探索", PHYSCHECK_HEIGHT);
if (Search(&list, &x, PhysCheckHeightCmp) != NULL)
PrintLnCurrent(&list);
else
puts("その身長のデータはありません。");
break;
case SRCH_NAME:
x = ScanPhysCheck("探索", PHYSCHECK_NAME);
if (Search(&list, &x, PhysCheckNameCmp) != NULL)
PrintLnCurrent(&list);
else
puts("その名前のデータはありません。");
break;
case PRINT_ALL:
Print(&list);
break;
case CLEAR:
Clear(&list);
break;
}
} while (menu != TERMINATE);
Terminate(&list);
return 0;
}