-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
359 lines (300 loc) · 8.58 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct user_info {
char std_No[100];
char name[100];
char contact[100];
char time[100];
}user_info;
typedef struct node {
struct user_info info;
struct node* next;
}node;
void menu() {
printf("행사 관리 프로그램입니다.\n\n");
printf("1. 신청하기\n");
printf("2. 신청 정보 수정하기(준비중)\n");
printf("3. 삭제하기(관리자용)\n");
printf("4. 신청명단 보기(관리자용)\n");
printf("5. 비밀번호 변경하기(관리자용)\n");
printf("6. 종료\n\n");
}
char loc_time[100];
char* update_time() {
time_t now_t;
struct tm *now_d;
time(&now_t);
now_d = localtime(&now_t);
strcpy(loc_time, asctime(now_d));
return loc_time;
}
node* add_node(char* (*fp) (void)) {
node* new = (node*)malloc(sizeof(node));
printf("학번: ");
scanf("%s", (new -> info).std_No);
printf("이름: ");
scanf("%s", (new -> info).name);
printf("전화번호: ");
scanf("%s", (new -> info).contact);
char* t = update_time();
strcpy((new -> info).time, t);
new -> next = NULL;
return new;
}
void add(node** phead, node* new) {
node* p1 = *phead;
node* p2 = NULL;
if (*phead == NULL) {
*phead = new;
new -> next = NULL;
}
else {
while (p1 != NULL) {
p2 = p1;
p1 = p1 -> next;
}
p1 = new;
p2 -> next = p1;
}
}
node* find(node* phead) {
node* p = phead;
user_info info;
printf("학번: ");
scanf("%s", info.std_No);
while (p != NULL) {
if(!strcmp(p -> info.std_No, info.std_No)) {
return p;
}
p = p -> next;
}
}
int admin_access() {
char pw[100];
char password[100];
FILE* fp = fopen("password.txt", "rt");
fgets(password, 100, fp);
printf("관리자 모드에 진입했습니다.\n");
printf("비밀번호: ");
scanf("%s", pw);
if (atoi(pw) == atoi(password)) {
printf("진입 성공\n\n");
return 1;
}
else {
printf("진입 실패\n\n");
return 0;
}
}
void delete(node** phead, node* del, int mode) {
if (mode == 1) {
node* p1 = *phead;
node* p2 = NULL;
if(p1 == NULL) {
return;
}
else {
if (p1 == del) {
*phead = (*phead) -> next;
free(del);
}
else {
while (p1 != NULL) {
if (p1 == del) {
p2 -> next = del -> next;
free(del);
return;
}
p2 = p1;
p1 = p1 -> next;
}
}
}
}
else if (mode == 2) {
if (admin_access() == 1) {
node* p1 = *phead;
node* p2 = NULL;
if(p1 == NULL) {
return;
}
else {
if (p1 == del) {
*phead = (*phead) -> next;
free(del);
}
else {
while (p1 != NULL) {
if (p1 == del) {
p2 -> next = del -> next;
free(del);
return;
}
p2 = p1;
p1 = p1 -> next;
}
}
}
}
}
}
void listup(node* head) {
if (admin_access() == 1) {
int n = 1;
node* p = head;
printf("신청자 명단을 출력합니다.\n");
while (p != NULL) {
printf("-%d-\n", n);
printf("학번: %s\n", (p -> info).std_No);
printf("이름: %s\n", (p -> info).name);
printf("전화번호: %s\n", (p -> info).contact);
printf("신청시간: %s\n\n", (p -> info).time);
p = p -> next;
n++;
}
}
}
void save(node* phead) {
node* p = phead;
int n = '1';
FILE* fp = fopen("DB.txt", "wt");
if (p == NULL) {
return;
}
else {
while (p != NULL) {
fputc(n, fp);
fputs("\n", fp);
fputs("학번:", fp);
fputs((p -> info).std_No, fp);
fputs("\n", fp);
fputs("이름:", fp);
fputs((p -> info).name, fp);
fputs("\n", fp);
fputs("전화번호:", fp);
fputs((p -> info).contact, fp);
fputs("\n", fp);
fputs("신청시간:", fp);
fputs((p -> info).time, fp);
p = p -> next;
n++;
}
}
fclose(fp);
return;
}
/* void update(node* head) {
}*/
void change_pw() {
if (admin_access() == 1) {
char password[100];
FILE* fp = fopen("password.txt", "wt");
printf("변경할 비밀번호를 입력하시오: ");
scanf("%s", password);
fputs(password, fp);
fclose(fp);
printf("변경 성공\n");
}
return;
}
int main() {
node* head = NULL;
int n = 0;
int i = 0;
update_time();
FILE* fp = fopen("DB.txt", "rt");
if (fp == NULL) {
printf("Failed to open file %s\n","DB.txt");
return -1;
}
int line = 0;
while (feof(fp) == 0) {
char a;
a = fgetc(fp);
if (a == ':') {
char std_No[100];
char name[100];
char contact[100];
char time[100];
if (line % 4 == 0) {
fgets(std_No, sizeof(std_No), fp);
}
else if (line % 4 == 1) {
fgets(name, sizeof(name), fp);
}
else if (line % 4 == 2) {
fgets(contact, sizeof(contact), fp);
}
else if (line % 4 == 3) {
node* new = (node*)malloc(sizeof(node));
new -> next = NULL;
fgets(time, sizeof(time), fp);
for (i = 0; i < 100; i++) {
if (std_No[i] == '\n') {
std_No[i] = 0;
}
if (name[i] == '\n') {
name[i] = 0;
}
if (contact[i] == '\n') {
contact[i] = 0;
}
if (time[i] == '\n') {
time[i];
}
}
strncpy((new -> info).std_No, std_No, sizeof((new -> info).std_No));
strncpy((new -> info).name, name, sizeof((new -> info).name));
strncpy((new -> info).contact, contact, sizeof((new -> info).contact));
strncpy((new -> info).time, time, sizeof((new -> info).time));
add(&head, new);
}
line++;
}
}
fclose(fp);
while (1) {
char answer[10];
menu();
printf("메뉴를 선택하시오: ");
scanf("%d", &n);
switch (n) {
case 1:
add(&head, add_node(update_time));
break;
case 2:
printf("주의! 신청 정보 수정 시 순서가 뒤로 밀리게 됩니다.\n");
printf("그래도 하시겠습니까? (Y/N): ");
scanf("%s", answer);
if (strcmp(answer, "Y") || strcmp(answer, "y")) {
delete(&head, find(head), 1);
add(&head, add_node(update_time));
}
else if (strcmp(answer, "N") || strcmp(answer, "n")) {
printf("취소했습니다.\n");
}
else {
printf("입력 오류입니다. Y 또는 N 중에 입력해주세요.\n");
}
break;
case 3:
delete(&head, find(head), 2);
break;
case 4:
listup(head);
break;
case 5:
change_pw();
break;
case 6:
save(head);
break;
}
if (n == 6) {
break;
}
}
return 0;
}