-
Notifications
You must be signed in to change notification settings - Fork 1
/
register.c
156 lines (140 loc) · 3.36 KB
/
register.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "register.h"
int selectMenu(){
int menu;
printf("\n*** 명부관리 ***\n");
printf("1. 조회\n");
printf("2. 추가\n");
printf("3. 수정\n");
printf("4. 삭제\n");
printf("5. 찾기\n");
printf("6. 저장\n");
printf("7. 정렬\n");
printf("0. 종료\n\n");
printf("=> 원하는 메뉴는? ");
scanf("%d", &menu);
return menu;
}
int writeStudent(Student *s){
printf("이름은 ? ");
scanf("%s", s->name);
printf("나이는 ? ");
scanf("%d", &s->age);
printf("학부는 ? ");
scanf("%s", s->major);
getchar();
printf("휴대폰 번호는? ex) 010-1234-1234 처럼 작성해주세요 ");
scanf("%s", s->phoneNumber);
printf("=> 추가됨!\n");
return 1;
}
void readStudent(Student s){
printf("%5s %4d %20s %14s\n",
s.name, s.age, s.major, s.phoneNumber);
}
void listStudent(Student *s, int count){
int i = 0;
printf("\nNo Name age Major Phone Number \n");
printf("============================================\n");
for(i= 0; i < count; i++){
if(s[i].age == -1) continue;
printf("%2d ", i+1);
readStudent(s[i]);
}
}
int updateStudent(Student *s){
printf("이름은 ? ");
scanf("%s", s->name);
printf("나이는 ? ");
scanf("%d", &s->age);
printf("학부는 ? ");
scanf("%s", s->major);
getchar();
printf("휴대폰 번호는? ex) 010-1234-1234 처럼 작성해주세요 ");
scanf("%s", s->phoneNumber);
printf("=> 수정완료!\n");
return 1;
}
int deleteStudent(Student *s){
s->age = -1;
printf("=> 삭제됨!\n");
return 1;
}
int selectStudentNo(Student *s, int count){
int no;
listStudent(s, count);
printf("\n번호는? (취소:0)? ");
scanf("%d", &no);
return no;
}
void searchName(Student *s, int count){
int scnt = 0;
char search[20];
printf("검색할 이름? ");
scanf("%s", search);
printf("\nNo Name age Major Phone Number \n");
printf("============================================\n");
for(int i=0;i<count;i++){
if(s[i].age == -1) continue;
if(strstr(s[i].name, search)){
printf("%2d ", i+1);
readStudent(s[i]);
scnt++;
}
}
if(scnt == 0) printf("=>검색된 데이터 없음!");
printf("\n");
}
void saveData(Student *s, int count){
FILE *fp;
fp = fopen("register.txt", "wt");
for(int i=0;i<count;i++){
if(s[i].age == -1) continue;
fprintf(fp, "%s %d %s %s\n", s[i].name, s[i].age, s[i].major, s[i].phoneNumber);
}
fclose(fp);
printf("=> 저장됨! \n");
}
int loadData(Student *s){
FILE *fp;
if(access("register.txt", 00) == -1){
printf("=> 파일 없음");
return 0;
}
fp = fopen("register.txt", "rt");
int i=0;
for(;i<100;i++){
fscanf(fp, "%s", s[i].name);
if(feof(fp)) break;
fscanf(fp, "%d", &s[i].age);
fscanf(fp, "%s", s[i].major);
fscanf(fp, "%s", s[i].phoneNumber);
}
fclose(fp);
printf("=> 로딩 성공!\n");
return i;
}
void descendingSort(Student *s, int count){
for(int i=0;i<count;i++){
for(int j=0;j<count;j++){
if(strcmp(s[i].name, s[j].name) < 0){
Student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
void ascendingSort(Student *s, int count){
for(int i=0;i<count;i++){
for(int j=0;j<count;j++){
if(strcmp(s[i].name, s[j].name) > 0){
Student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}