-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_1.c
394 lines (361 loc) · 9.94 KB
/
basic_1.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// Created by 1708cjy on 2020/11/25.
//
#include "basic_1.h"
char *studentInformation_PATH = "D:\\Learn\\CProgrammingLanguage\\programming\\CLion experiment\\StudentInformation.txt";
char *teacherInformation_PATH = "D:\\Learn\\CProgrammingLanguage\\programming\\CLion experiment\\TeacherInformation.txt";
char *courseInformation_PATH = "D:\\Learn\\CProgrammingLanguage\\programming\\CLion experiment\\CourseInformation.txt";
char *account_PATH = "D:\\Learn\\CProgrammingLanguage\\programming\\CLion experiment\\Information\\Administrator.bat";
extern Student *Head;
extern Course *CourseList;
extern Teacher *TeacherList;
extern Teacher *pNowTea;
extern Student *pNowStu;
extern Student *pPastStu;
//判断是否有内容
int weatherStuInformation(){
FILE *fp = fopen(studentInformation_PATH,"ab");
fseek(fp,0,SEEK_SET);
Student text = {.ID = 0};
fread(&text,sizeof(Student),1,fp);
fseek(fp,0,SEEK_SET);
fclose(fp);
if (text.ID == 0) return -1;
else return 0;
}
int weatherCourseInformation(){
FILE *fp = fopen(courseInformation_PATH,"ab");
fseek(fp,0,SEEK_SET);
Course text = {.Course_ID = 0};
fread(&text,sizeof(Course),1,fp);
fseek(fp,0,SEEK_SET);
fclose(fp);
if (text.Course_ID == 0) return -2;
else return 0;
}
int weatherTeacherInformation(){
FILE *fp = fopen(teacherInformation_PATH,"ab");
fseek(fp,0,SEEK_SET);
Teacher text = {.Teacher_ID = 0};
fread(&text,sizeof(Teacher),1,fp);
fseek(fp,0,SEEK_SET);
fclose(fp);
if (text.Teacher_ID == 0) return -4;
else return 0;
}
//判断错误情况
void MisjudgmentCase(int result){
printf("加载中,请稍后");
if(!result) {
printf("数据完整,加载成功!");
Head = readInformation();
TeacherList = readTeacherInformation();
CourseList = readCourseInformation();
}
else if (result == -1){
printf("无学生数据!\n");
TeacherList = readTeacherInformation();
CourseList = readCourseInformation();
}
else if (result == -2){
printf("无课程数据!\n");
Head = readInformation();
TeacherList = readTeacherInformation();
}
else if (result == -4){
printf("无教师数据!\n");
Head = readInformation();
CourseList = readCourseInformation();
}
else if (result == -3){
printf("无学生和课程数据!\n");
TeacherList = readTeacherInformation();
}
else if (result == -5){
printf("无学生和教师数据!\n");
CourseList = readCourseInformation();
}
else if (result == -6){
printf("无课程和教师数据!\n");
Head = readInformation();
}
else if (result == -7) printf("无学生、课程和教师数据!\n");
}
//从文件中读出学生信息
Student *readInformation(){
Student *head = NULL;
Student *p = NULL;
Student *q = NULL;
FILE *fp = fopen(studentInformation_PATH,"rb");
q = (Student *)malloc(sizeof(Student));
if (q == NULL){
fclose(fp);
exit(EXIT_FAILURE);
}
fread(q,sizeof(Student),1,fp);
head = q;
do{
p = (Student *)malloc(sizeof(Student));
if (p == NULL){
safeFreeStu(head);
exit(EXIT_FAILURE);
}
fread(p,sizeof(Student),1,fp);
q->pNext = p;
q = q->pNext;
}while (q->pNext != NULL);
fclose(fp);
return head;
}
//从文件中读出课程信息
Course *readCourseInformation(){
Course *courseList = NULL;
Course *pTemp = NULL;
Course temp;
int index = 1;
FILE *fp = fopen(courseInformation_PATH,"rb");
courseList = (Course *)malloc(sizeof(Course));
if (courseList == NULL){
exit(EXIT_FAILURE);
}
fread(courseList,sizeof(Course),1,fp);
do{
pTemp = (Course *)realloc(courseList,(index + 1) * sizeof(Course));
if (pTemp == NULL){
safeFreeCour(courseList);
exit(EXIT_FAILURE);
}
courseList = pTemp;
if (fread(&temp,sizeof(Course),1,fp) != 0){
courseList[index] = temp;
}else{
courseList[index].Course_ID = 0;
break;
}
}while (index++);
fclose(fp);
return courseList;
}
//从文件中读出教师信息
Teacher *readTeacherInformation(){
Teacher *pTemp = NULL;
Teacher temp;
int index = 1;
FILE *fp = fopen(teacherInformation_PATH,"rb");
TeacherList = (Teacher *)malloc(sizeof(Course));
if (TeacherList == NULL){
exit(EXIT_FAILURE);
}
fread(TeacherList,sizeof(Course),1,fp);
do{
pTemp = (Teacher *)realloc(TeacherList,(index + 1) * sizeof(Teacher));
if (pTemp == NULL){
safeFreeTea(TeacherList);
exit(EXIT_FAILURE);
}
TeacherList = pTemp;
if (fread(&temp,sizeof(Course),1,fp) != 0){
TeacherList[index] = temp;
}else{
TeacherList[index].Teacher_ID = 0;
break;
}
}while (index++);
fclose(fp);
return TeacherList;
}
//读取管理员信息,无信息、账号密码不正确返回0,正确返回1
int weatherAdministrator(char *account,char *password){
Account wayForPoint;
Account *p = &wayForPoint;
FILE *fp = fopen(account_PATH,"rb");
if (fp == NULL){
printf("账号密码错误,程序3秒后结束\n");
sleep(3);
return 0;
}else{
int count;
do{
count = fread(p,sizeof(Account),1,fp);
if(strcmp(p->name,account) == 0 && strcmp(p->password,password) == 0){
printf("当前管理员%s已登录\n",p->name);
fclose(fp);
return 1;
}
}while (count == 1);
fclose(fp);
printf("账号密码错误,程序3秒后结束\n");
sleep(3);
return 0;
}
}
//登录信息,成功就返回账号,失败返回0;管理员返回1;
int loginIn(){
int ID;
char account[20] = {};
char tryPassword[16] = {};
printf("请输入账号或姓名:");
fflush(stdin);
scanf("%s",account);
printf("请输入密码:");
fflush(stdin);
scanf("%s",tryPassword);
if(isdigit(account[0]) && isdigit(account[6])){
ID = atoi(account);
if (ID <= 20000000 && (pNowTea = searchTeacher(ID)) != NULL){
printf("欢迎%s老师登录",pNowTea->name);
return ID;
}
else if (ID > 20000000 && ID < 21000000 && (pNowStu = searchStudent(ID)) != NULL){
printf("欢迎%s同学登录",pNowStu->name);
return ID;
}
printf("账号密码错误,程序在3秒后结束\n");
sleep(3);
return 0;
}else{
return weatherAdministrator(account,tryPassword);
}
}
//以学生ID搜索链表内容
Student *searchStudent(int ID){
Student *p = Head;
Student *q = NULL;
while(p != NULL){
if(p->ID == ID){
return p;
}
pPastStu = p;
q = p->pNext;
p = q;
}
return NULL;
}
//以教师ID搜索教师工号
Teacher *searchTeacher(int ID){
for(int i = 0;TeacherList[i].Teacher_ID != 0;i++){
if(TeacherList[i].Teacher_ID == ID){
return &TeacherList[i];
}
}
return NULL;
}
//以课程ID搜索课程名称
Course *searchCourse(unsigned int ID){
for (int i = 0; (CourseList + i)->Course_ID != 0; i++) {
if ((CourseList + i)->Course_ID == ID){
return (CourseList + i);
}
}
return NULL;
}
//以学生姓名搜索学生链表内容
Student *searchStuByName(char *name){
Student *p = pNowStu;
Student *q = NULL;
while((p->ID - pNowStu->ID) < 100){
if(strcmp(p->name,name) == 0){
return p;
}
pPastStu = p;
q = p->pNext;
p = q;
}
return NULL;
}
//以课程ID搜索教师
Teacher *searchTeaByCourseID(int courseID){
for (int i = 0;(TeacherList + i)->taughtSubjectsID != 0;i++){
if ((TeacherList + i)->taughtSubjectsID == courseID){
return (TeacherList + i);
}
}
return NULL;
}
//输入学生信息
void inputStuInformation_ID(Student *pAddStudent) {
printf("请输入\n学生ID:");
fflush(stdin);
scanf("%d", &pAddStudent->ID);
while (1) {
if (searchStudent(pAddStudent->ID) != NULL) {
printf("该ID已存在,输入不合法!\n"
"请重新输入:");
fflush(stdin);
scanf("%d", &pAddStudent->ID);
} else {
break;
}
}
}
//输入、修改学生信息
void inputStuInformation(Student *pAddStudent){
printf("学生姓名:");
fflush(stdin);
scanf("%s",pAddStudent->name);
printf("学生年龄:");
fflush(stdin);
scanf("%d",&pAddStudent->age);
printf("学生性别:");
fflush(stdin);
scanf("%c",&pAddStudent->sex);
for(int i = 0;i < 16;i++) {
pAddStudent->course[i] = 0;
pAddStudent->teacher[i] = 0;
}
pAddStudent->class = 0;
}
void inputStuInformationLevelUp(Student *pAddStudent){
printf("学生姓名:");
fflush(stdin);
scanf("%s",pAddStudent->name);
printf("学生年龄:");
fflush(stdin);
scanf("%d",&pAddStudent->age);
printf("学生性别:");
fflush(stdin);
scanf("%c",&pAddStudent->sex);
}
//完善学生信息
void improveStuInformation(Student *pTarget){
Teacher *pTempTea = NULL;
//
if (pTarget->class == 0){
pTarget->class = pTarget->ID /100;
}
//以课程ID完善教师ID
if (pTarget->course[0] != 0){
for (int i = 0;pTarget->course[i] != 0;i++){
pTempTea = searchTeaByCourseID(pTarget->course[i]);
if (pTempTea == NULL){
pTarget->teacher[i] = -1;
}else{
pTarget->teacher[i] = pTempTea->Teacher_ID;
}
}
}
}
//安全释放学生链表
void safeFreeStu(Student *head){
Student *p,*q;
p = head;
while(p != NULL){
q = p->pNext;
free(p);
p = q;
}
}
//安全释放课程数据
void safeFreeCour(Course *courseList){
free(courseList);
}
//安全释放教师数据
void safeFreeTea(Teacher *teacherList){
free(teacherList);
}
//安全释放数据
void safeFreeAll(Course *courseList,Teacher *teacherList,Student *head){
safeFreeCour(courseList);
safeFreeTea(teacherList);
safeFreeStu(head);
}