-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudentManager.c
More file actions
328 lines (305 loc) · 7.63 KB
/
studentManager.c
File metadata and controls
328 lines (305 loc) · 7.63 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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#else
#include <unistd.h>
#endif
// string
char *readString(){
char *str = NULL;
int capacity = 0;
int length = 0;
char temp;
while((temp = getchar()) != '\n'){
if(length + 1 >= capacity){
capacity += 10;
char *new_str = realloc(str, capacity * sizeof(char));
if(new_str == NULL){
free(str);
return NULL;
}
str = new_str;
}
str[length++] = temp;
}
if(str){
str[length] = '\0';
}else{
str = malloc(1);
if(str) str[0] = '\0';
}
return str;
}
// screen
void moveCursor(int row, int col) {
printf("\033[%d;%dH", row, col);
fflush(stdout);
}
void clearScreen(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void homeScreen(){
clearScreen();
moveCursor(1, 1);
printf("学生管理系统\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");
printf("你的操作: ");
}
void slp(int ms){ // sleep
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms*1000);
#endif
}
// student
typedef struct Student Student;
struct Student{
char *name;
char *school;
char *major;
char *class;
Student* next;
};
Student* stu_head;
void addStudent(){
Student* stu = (Student*)malloc(sizeof(Student));
printf("请输入学生姓名: ");
stu->name = readString();
printf("请输入学生学校: ");
stu->school = readString();
printf("请输入学生专业: ");
stu->major = readString();
printf("请输入学生班级: ");
stu->class = readString();
Student* p = stu_head;
while(p->next!=NULL){
if(strcmp(p->next->name, stu->name)==0){
printf("学生已存在\n");
free(stu);
return;
}
p = p->next;
}
p->next = stu;
stu->next = NULL;
printf("添加成功\n");
}
void deleteStudent(){
printf("请输入学生姓名: ");
char *name;
name = readString();
Student* p = stu_head->next;
while(p!=NULL){
if(strcmp(p->name, name)==0){
Student* q = p->next;
p->next = q->next;
free(q);
printf("删除成功\n");
return;
}
p = p->next;
}
printf("未找到该学生\n");
}
void modifyStudent(){
printf("请输入学生姓名: ");
char *name;
name = readString();
Student* p = stu_head->next;
while(p!=NULL){
if(strcmp(p->name, name)==0){
printf("请输入学生学校: ");
p->school = readString();
printf("请输入学生专业: ");
p->major = readString();
printf("请输入学生班级: ");
p->class = readString();
printf("修改成功\n");
return;
}
p = p->next;
}
printf("未找到该学生\n");
}
void queryStudent(){
printf("请输入学生姓名: ");
char *name;
name = readString();
Student* p = stu_head->next;
while(p!=NULL){
if(strcmp(p->name, name)==0){
printf("学生姓名: %s\n", p->name);
printf("学生学校: %s\n", p->school);
printf("学生专业: %s\n", p->major);
printf("学生班级: %s\n", p->class);
return;
}
p = p->next;
}
printf("未找到该学生\n");
}
void exchange(Student* a, Student* b){
Student* temp = (Student*)malloc(sizeof(Student));
temp->name = a->name;
temp->school = a->school;
temp->major = a->major;
temp->class = a->class;
a->name = b->name;
a->school = b->school;
a->major = b->major;
a->class = b->class;
b->name = temp->name;
b->school = temp->school;
b->major = temp->major;
b->class = temp->class;
free(temp);
}
void printStudent(){
//sort
Student* p = stu_head->next;
if(p==NULL){
printf("无学生信息\n");
return;
}
while(p->next!=NULL){
Student* cur1 = p;
Student* cur2 = p->next;
while(cur2!=NULL){
if(strcmp(cur1->school, cur2->school)>0) exchange(cur1, cur2);
else if(strcmp(cur1->school, cur2->school)==0 && strcmp(cur1->major, cur2->major)>0) exchange(cur1, cur2);
else if(strcmp(cur1->school, cur2->school)==0 && strcmp(cur1->major, cur2->major)==0 && strcmp(cur1->class, cur2->class)>0) exchange(cur1, cur2);
cur1 = cur1->next;
cur2 = cur2->next;
}
p = p->next;
}
//print
p = stu_head->next;
printf("%s ── %s ── %s ── %s\n", p->school, p->major, p->class, p->name);
Student* q = p;
p = p->next;
while(p!=NULL){
if(strcmp(p->school, q->school)==0){
if(strcmp(p->major, q->major)==0){
if(strcmp(p->class, q->class)==0){
printf(" └─ %s\n", p->name);
}else{
printf(" └─ %s ── %s\n", p->class, p->name);
}
}else{
printf(" └─ %s ── %s ── %s\n",p->major, p->class, p->name);
}
}else{
printf("%s ── %s ── %s ── %s\n", p->school, p->major, p->class, p->name);
}
q = p;
p = p->next;
}
}
// save & load
void save(){
FILE* fp = fopen("student.dat", "wb");
if(fp==NULL){
printf("文件打开失败\n");
return;
}
Student* p = stu_head->next;
while(p!=NULL){
fwrite(p, sizeof(Student), 1, fp);
p = p->next;
}
fclose(fp);
printf("保存成功\n");
}
void load(){
FILE* fp = fopen("student.dat", "rb");
if(fp == NULL){
printf("文件打开失败\n");
return;
}
if(stu_head == NULL) {
stu_head = (Student*)malloc(sizeof(Student));
stu_head->next = NULL;
}
Student* p = stu_head;
while(p->next != NULL) {
p = p->next;
}
while(1){
Student* stu = (Student*)malloc(sizeof(Student));
if(fread(stu, sizeof(Student), 1, fp) != 1){
free(stu);
break;
}
p->next = stu;
stu->next = NULL;
p = stu;
}
fclose(fp);
printf("加载成功\n");
}
// main
int main(){
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
DWORD mode;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hConsole, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hConsole, mode);
#endif
int op;
stu_head = (Student*)malloc(sizeof(Student)*100);
while(1){
homeScreen();
scanf("%d", &op);
getchar(); // eat '\n'
clearScreen();
moveCursor(1, 1);
switch(op){
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
printStudent();
break;
case 6:
save();
break;
case 7:
load();
break;
case 0:
return 0;
default:
break;
}
slp(1000);
}
return 0;
}