-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
323 lines (272 loc) · 7.2 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define pwd 3848
char buf_t[100];
typedef struct element // 아래 node구조체 내의 변수중 하나
{
char phone[100];
char car_num[100];
//
char time[100];
}element;
typedef struct node //node 구조체 정의
{
struct element data; //이름,전화번호 데이터
struct node* next; //다음 노드를 가리키는 포인터
}node;
//사용되는 함수들
void print_menu(void);
node* createnode(char* (*fp) (void)); //node하나를 만드는 함수. insert함수의 두번째 인자로 전달하기위해
void insert(node** phead, node* newnode); //연결리스트의 마지막 node에 newnode를 연결시키는 함수
node* search(node* phead); //입력한 이름을 가진 node를 찾는 함수
void delete(node** phead, node* remove); //remove노드를 search함수로 찾아서 인자로 전달한다
void display(node* head); //연결리스트의 모든 내용을 출력하는 함수
void writefile(node* phead); //종료시 파일에 연결리스트의 내용을 저장하는 함수
int is_admin(void);
char * return_time(void);
int main()
{
return_time();
node* head = NULL; //헤드 포인터
int num = 0;
int in = 0;
node* se; //switch case 3: 에서 찾은 노드의 주소값 임시 저장
int i = 0;
FILE* des = fopen("UserInfo.txt", "rt"); //파일의 내용을 기초로 연결리스트 구성을 위해 입력스트림 생성
if (des == NULL) //입력스트림의 생성
{
printf("파일 읽기 실패!\n");
return -1; //오류
}
while (feof(des) == 0)
{
char ch;
ch = fgetc(des);
if (ch == ':')
{
char phone[100];
char car_num[100];
char time[100];
if (i % 3 == 0)
fgets(phone, sizeof(phone), des);
else if (i % 3 == 1)
{
fgets(car_num, sizeof(car_num), des);
}
else if(i%3 == 2)
{
int j = 0;
node* newnode = (node*)malloc(sizeof(node));
newnode->next = NULL;
fgets(time, sizeof(time), des);
for (j = 0; j < 100; j++)
{
if (phone[j] == '\n') //name에 저장된 \n을 0으로 바꿔준다.
phone[j] = 0;
if (car_num[j] == '\n') //phon에 저장된 \n을 0으로 바꿔준다.
car_num[j] = 0;
//
if (time[j] == '\n')
time[j] = 0;
}
strncpy(newnode->data.phone, phone, sizeof(newnode->data.phone));
strncpy(newnode->data.car_num, car_num, sizeof(newnode->data.car_num));
strncpy(newnode->data.time, time, sizeof(newnode->data.time));
insert(&head, newnode);
//i++;
}
i++;
}
}
fclose(des);
/*파일의 내용에서 ':' 이용해서 ':' 다음에 나오는 문자열을 phone,car_num, time에 차례대로 넣는다. 그리고
만들어놓은 새로운 노드에 입력시키고 이 새로운 노드로 연결리스트를 구성한다.*/
while (1)
{
print_menu();
printf(" 메뉴를 선택하시오 (숫자 입력): ");
scanf("%d", &num);
switch (num) //switch
{
case 1:
insert(&head, createnode(return_time));
break;
case 2:
delete(&head, search(head));
break;
case 3:
se = search(head);
if (se != NULL)
{
printf(" 전화번호 : %s ", se->data.phone);
printf(" 차량번호 : %s\n", se->data.car_num);
printf(" 입차시간 : %s\n", se->data.time);
}
else
{
printf(" 일치하는 정보가 없습니다."); printf("\n");
}
break;
case 4:
if (is_admin() == 1) {
display(head);
}
break;
default:
writefile(head);
break;
}
if (num == 5)
break;
}
return 0;
}
void print_menu(void) {
printf(" _______________________________________\n");
printf(" | |\n");
printf(" | 주차 관리 시스템 |\n");
printf(" | |\n");
printf(" | |\n");
printf(" | 1. 주차 |\n");
printf(" | 2. 출차 |\n");
printf(" | 3. 차량 정보 |\n");
printf(" | 4. 관리자 |\n");
printf(" | 5. 종료 |\n");
printf(" | |\n");
printf(" |_______________________________________|\n");
printf("\n");
}
node* createnode(char* (*fp) (void))
{
node* newnode = (node*)malloc(sizeof(node)); //새로운 노드 생성
printf("\n 전화번호:"); scanf("%s", (newnode->data).phone); //phone 입력
printf("\n 차량번호:"); scanf("%s", (newnode->data).car_num); //car_num 입력
//printf("\n 시간:"); scanf("%s", (newnode->data).time);
char* s1 = return_time();
strcpy((newnode->data).time, s1);
newnode->next = NULL;
return newnode; //newnode 의 주소값을 반환
}
void insert(node** phead, node* newnode)
{
node* ptr = *phead;
node* p = NULL;
if (*phead == NULL) //연결리스트 NULL
{
(*phead) = newnode;
newnode->next = NULL;
}
else
{
while (ptr != NULL) //마지막 노드의 주소 찾기
{
p = ptr;
ptr = ptr->next;
}
ptr = newnode;
p->next = ptr; // 끝에 newnode를 연결
}
}
node* search(node* phead)
{
node* ptr = phead;
element el;
printf(" 전화번호:"); scanf("%s", el.phone);
while (ptr != NULL)
{
if (!strcmp(ptr->data.phone, el.phone)) //각 노드의 데이터 내의 전화번호과 찾을 전화번호를 비교
return ptr; //찾을 전화번호 가진 노드의 주소값을 반환
ptr = ptr->next;
}
return NULL;
}
void delete(node** phead, node* remove)
{
node* ptr = *phead;
node* p = NULL;
if (ptr == NULL)
return;
else
{
if (*phead == remove) //첫번째 노드를 삭제할때
{
*phead = (*phead)->next;
free(remove);
}
else
{
while (ptr != NULL)
{
if (ptr == remove)
{
p->next = remove->next; //remove노드를 연결리스트에서 제외
free(remove);
return;
}
p = ptr;
ptr = ptr->next;
}
}
}
}
void display(node* head)
{
int i = 1;
node* p = head;
while (p != NULL)
{
printf("\n\n-------------Member List--------------\n");
printf("%d.\n", i);
printf(" 전화번호:%s\n", (p->data).phone);
printf(" 차량번호:%s\n\n", (p->data).car_num);
//
printf(" 입차시간:%s\n\n", (p->data).time);
printf("\n");
p = p->next;
i++;
}
}
void writefile(node* phead)
{
node* ptr = phead;
int i = '1';
FILE* des = fopen("UserInfo.txt", "wt"); //출력 스트림 형성
if (ptr == NULL)
return;
else
{
while (ptr != NULL)
{
fputc(i, des); fputs("번째\n", des);
fputs("전화번호:", des); fputs(ptr->data.phone, des); fputs("\n", des);
fputs("차량번호:", des); fputs(ptr->data.car_num, des); fputs("\n", des);
//
fputs("입차시간:", des); fputs(ptr->data.time, des); fputs("\n", des);
ptr = ptr->next;
i++;
}
}
fclose(des);
return;
}
int is_admin(void) {
int num;
printf(" 비밀번호를 입력하세요: ");
scanf("%d", &num);
if (num == pwd) {
return 1;
}
else {
return 0;
}
}
char* return_time(void) {
time_t now_time;
struct tm *now_date;
time(&now_time); //현재 시각을 구한다.
now_date = localtime(&now_time);//초 단위 값을 지역 시각(DateTime)을 구한다.
strcpy(buf_t,asctime(now_date));
return buf_t;
}