-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.cpp
494 lines (381 loc) · 15.3 KB
/
student.cpp
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include"public.h"
// 学生选课管理系统
// 2019级 - 电管 - 丁丁的玩具车队 - 小学期作业
// 演示地址: https://www.bilibili.com/video/BV12Z4y1P7QQ?spm_id_from=333.999.0.0
// WeChat:dky1251001066
// GitHub:https://github.com/OmegaDING
//确定学生已经选课,向文件中加入相关信息
void student_select_course(int student_number, int course_number) {
char student_course_filename[50], course_students_filename[50];
FILE* fp_rewrite_student_course, * fp_rewrite_course_students;
errno_t err;
sprintf_s(student_course_filename, 50, "bin//student_course//%d.txt", student_number);
err = fopen_s(&fp_rewrite_student_course, student_course_filename, "a+");
fprintf(fp_rewrite_student_course, "%d ", course_number);
fclose(fp_rewrite_student_course);
//向学生个人信息文件中添加该课程。
sprintf_s(course_students_filename, 50, "bin//course_students//%d.txt", course_number);
err = fopen_s(&fp_rewrite_course_students, course_students_filename, "a+");
fprintf(fp_rewrite_course_students, "%d ", student_number);
fclose(fp_rewrite_course_students);
//向课程的学生名单信息文件中添加该学生的学号。
return;
}
//返回课程编号,根据选课人数
void sort_accroding_to_number_of_student(int order[]) {//将数组order根据人数写入课程号排序//返回排好序的课程序号数组
int total_course_num = 0;
total_course_num = get_total_course_num();
FILE* course_f;
int stu_id = 0;
int* course_population = NULL;
course_population = (int*)malloc(1000000 * sizeof(int));//因为课程号六位所以最大有1000000个班
//for (int i = 0; i < 10000000000; i++) {
// course_population[i] = 0;
// //printf("第%d\n", i);
//}
//获取所有课程人数大小
char file_id[100];
for (int course_num = 100001; course_num < 100001 + total_course_num; course_num++) {
sprintf_s(file_id, "bin\\course_students\\%d.txt", course_num);
fopen_s(&course_f, file_id, "r");
course_population[course_num] = 0;
if (course_f == NULL)
{
continue;
}
while (fscanf_s(course_f, ("%d"), &stu_id) != EOF)
course_population[course_num]++;
fclose(course_f);
// printf("课号为%d的课程人数%d\n",course_num,course_population[course_num]);
}
int* course_population_order;//存储课程编号
course_population_order = (int*)malloc(sizeof(int) * 1000000);
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
course_population_order[i] = course_num;
}
//连带课程编号再一起排序
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
if (course_population[course_num] < course_population[course_num + 1]) {
//排序人数
int tmp_course_population = 0;
tmp_course_population = course_population[course_num + 1];
course_population[course_num + 1] = course_population[course_num];
course_population[course_num] = tmp_course_population;
//排序课程号
int tmp_course_num = 0;
tmp_course_num = course_population_order[i + 1];
course_population_order[i + 1] = course_population_order[i];
course_population_order[i] = tmp_course_num;
}
}
}
// printf("排序 选课人数\n");
// for (int course_num = 100001; course_num < 100001 + total_course_num; course_num++) {
// printf("%d\n",course_population[course_num]);
// }
//
//printf("排序_课程号\n");
//for (int course_num = 100001,i=0; course_num < 100001 + total_course_num;i++, course_num++) {
// printf("%d\n", course_population_order[i]);
//}
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
order[i] = course_population_order[i];
}
free(course_population_order);
free(course_population);
return ;
}
//返回课程编号,根据课余量
void sort_accroding_to_number_of_left(int order[]) {//将数组order根据人数写入课程号排序//返回排好序的课程序号数组
int total_course_num = 0;
total_course_num = get_total_course_num();
FILE* course_f;
int stu_id = 0;
int* course_population = NULL;
course_population = (int*)malloc((1000000) * sizeof(int));//因为课程号六位所以最大有1000000个班
//for (int i = 0; i < 10000000000; i++) {
// course_population[i] = 0;
// //printf("第%d\n", i);
//}
//获取所有课程人数大小
char file_id[100];
for (int course_num = 100001; course_num < 100001 + total_course_num; course_num++) {
sprintf_s(file_id, "bin\\course_students\\%d.txt", course_num);
fopen_s(&course_f, file_id, "r");
course_population[course_num] = 0;
if (course_f == NULL)
continue;
while (fscanf_s(course_f, ("%d"), &stu_id) != EOF)
course_population[course_num]++;
fclose(course_f);
// printf("课号为%d的课程人数%d\n",course_num,course_population[course_num]);
}
//获取所有课程课余量大小
int* course_left_population;
course_left_population = (int*)malloc(sizeof(int) * 1000000);
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++)
{
course_left_population[course_num] = get_course_limt_population(course_num) - course_population[course_num];
}
int* course_population_order;//存储课程编号
course_population_order = (int*)malloc(sizeof(int) * 1000000);
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
course_population_order[i] = course_num;
}
//连带课程编号再一起排序
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
if (course_left_population[course_num] < course_left_population[course_num + 1]) {
//排序人数
int tmp_course_left_population = 0;
tmp_course_left_population = course_left_population[course_num + 1];
course_left_population[course_num + 1] = course_left_population[course_num];
course_left_population[course_num] = tmp_course_left_population;
//排序课程号
int tmp_course_num = 0;
tmp_course_num = course_population_order[i + 1];
course_population_order[i + 1] = course_population_order[i];
course_population_order[i] = tmp_course_num;
}
}
}
// printf("排序 选课人数\n");
// for (int course_num = 100001; course_num < 100001 + total_course_num; course_num++) {
// printf("%d\n",course_population[course_num]);
// }
//
//printf("排序_课程号\n");
//for (int course_num = 100001,i=0; course_num < 100001 + total_course_num;i++, course_num++) {
// printf("%d\n", course_population_order[i]);
//}
for (int course_num = 100001, i = 0; course_num < 100001 + total_course_num; i++, course_num++) {
order[i] = course_population_order[i];
}
free(course_left_population);
free(course_population_order);
free(course_population);
return;
}
//搜索并返回课程结构数组
courses* search_course(char* key_word) {
courses* cou;
cou = get_course_info();
courses* course_search_result;
course_search_result = (courses*)malloc(sizeof(courses) * get_total_course_num() );
for (int i = 0,k=0;i < get_total_course_num();i++) {
if ((strstr_cnt(cou[i].name, key_word) != 0 )||(strstr_cnt(cou[i].school,key_word) !=0))
{
course_search_result[k].id = cou[i].id;
for (int n = 0; n < 100; n++) {
course_search_result[k].book[n] = cou[i].book[n];
course_search_result[k].name[n] = cou[i].name[n];
/*course_search_result[k].time_s[n] = cou[i].time_s[n];
course_search_result[k].term_end[n] = cou[i].term_end[n];
course_search_result[k].term_start[n] = cou[i].term_start[n];*/
}
/*course_search_result[k].class_hours = cou[i].class_hours;
course_search_result[k].end_mounth = cou[i].end_mounth;
course_search_result[k].end_year = cou[i].end_year;
course_search_result[k].ismust = cou[i].ismust;*/
course_search_result[k].limit_number = cou[i].limit_number;
course_search_result[k].score = cou[i].limit_number;
for (int n = 0; n < 50; n++)
course_search_result[k].place[n] = cou[i].place[n];
/*course_search_result[k].start_mounth = cou[i].start_mounth;
course_search_result[k].start_year = cou[i].start_year;*/
course_search_result[k].teacher_number = cou[i].teacher_number;
for(int n=0 ;n<80;n++)
course_search_result[k].time[n] = cou[i].time[n];
for (int x = 0;x < 100;x++)
course_search_result[k].school[x] = cou[i].school[x];
course_search_result[k].ismust = cou[i].ismust;
k++;
}
}
free(cou);
return course_search_result;
}
//搜索并返回符合课程的数量
int get_search_course_amount(char* key_word) {
courses* cou;
cou = get_course_info();
courses* course_search_result;
course_search_result = (courses*)malloc(sizeof(courses) * get_total_course_num());
int k = 0;
for (int i = 0; i < get_total_course_num(); i++) {
if ((strstr_cnt(cou[i].name, key_word) != 0) || (strstr_cnt(cou[i].school, key_word) != 0)) {
/*course_search_result[k].id = cou[i].id;
for (int n = 0; n < 100; n++) {
course_search_result[k].book[n] = cou[i].book[n];
course_search_result[k].name[n] = cou[i].name[n];
course_search_result[k].time_s[n] = cou[i].time_s[n];
course_search_result[k].term_end[n] = cou[i].term_end[n];
course_search_result[k].term_start[n] = cou[i].term_start[n];
}
course_search_result[k].class_hours = cou[i].class_hours;
course_search_result[k].end_mounth = cou[i].end_mounth;
course_search_result[k].end_year = cou[i].end_year;
course_search_result[k].ismust = cou[i].ismust;
course_search_result[k].limit_number = cou[i].limit_number;
course_search_result[k].score = cou[i].limit_number;
for (int n = 0; n < 50; n++)
course_search_result[k].place[n] = cou[i].place[n];
course_search_result[k].start_mounth = cou[i].start_mounth;
course_search_result[k].start_year = cou[i].start_year;
course_search_result[k].teacher_number = cou[i].teacher_number;
for (int n = 0; n < 80; n++)
course_search_result[k].time[n] = cou[i].time[n];*/
k++;
}
}
free(cou);
free(course_search_result);
return k;
}
//根据选课人数排序课程,操作课程结构数组
void sort_number_of_stu(courses* cou_order) {
courses* total_cou;
total_cou = get_course_info();
int* cou_num_order;
cou_num_order = (int*)malloc(sizeof(int) * get_total_course_num());
sort_accroding_to_number_of_student(cou_num_order);
for(int i=0;i<get_total_course_num();i++)
for (int j = 0; j < get_total_course_num(); j++)
if (total_cou[j].id == cou_num_order[i]) {
// cou_order[i].id = cou_num_order[i];
courses_copy_single((cou_order[i]), (total_cou[j]));
/* sprintf_s(tmp_char, "%d", cou_order[i].id);
CharToTchar(tmp_char, tmp_wchar);
MessageBox(NULL, tmp_wchar, TEXT("Msg"), 0);*/
}
free(cou_num_order);
free(total_cou);
return ;
}
//根据课余量排序课程,操作课程结构数组
void sort_number_of_left(courses *order) {
courses* total_cou;
total_cou = get_course_info();
int* cou_num_order;
cou_num_order = (int*)malloc(sizeof(int) * get_total_course_num());
sort_accroding_to_number_of_left(cou_num_order);
for (int i = 0; i < get_total_course_num(); i++)
for (int j = 0; j < get_total_course_num(); j++)
if (total_cou[j].id == cou_num_order[i]) {
// cou_order[i].id = cou_num_order[i];
courses_copy_single((order[i]), (total_cou[j]));
/* sprintf_s(tmp_char, "%d", cou_order[i].id);
CharToTchar(tmp_char, tmp_wchar);
MessageBox(NULL, tmp_wchar, TEXT("Msg"), 0);*/
}
free(cou_num_order);
free(total_cou);
return ;
}
//返回某学生已选课
void course_chosen(int student_id,courses* cour_chosen) {
int* cour_chosen_id;
cour_chosen_id = get_course_chosen_id_student(student_id);
courses* all_courses;
all_courses = get_course_info();
for (int j = 0, k = 0; j < get_total_course_num(); j++) {
for (int i = 0; i < get_total_course_num(); i++) {
if (all_courses[i].id == cour_chosen_id[j]) {
courses_copy_single(cour_chosen[k], all_courses[i]);
k++;
}
}
}
return;
}
//删除学生课程,学生删课
void student_delete_course(int student_number, int course_number) {
char student_course_filename[50], course_students_filename[50], student_id[11] = { 0 }, course_id[7] = { 0 }, linedata[11] = { 0 };
FILE* fp_read_student_course, * fp_rewrite_student_course, * fp_read_course_students, * fp_rewrite_course_students;
errno_t err;
sprintf_s(student_course_filename, 50, "bin//student_course//%d.txt", student_number);
err = fopen_s(&fp_read_student_course, student_course_filename, "r+");
err = fopen_s(&fp_rewrite_student_course, "bin//student_course//tmp.txt", "w+");
_itoa_s(course_number, course_id, 10);
while ((fscanf_s(fp_read_student_course, "%s", linedata, 11)) != EOF) {
if (strcmp(linedata, course_id) != 0)
fprintf(fp_rewrite_student_course, "%s ", linedata);
}
fclose(fp_read_student_course);
fclose(fp_rewrite_student_course);
remove(student_course_filename);
rename("bin//student_course//tmp.txt", student_course_filename);
//删除学生个人信息文件中的该课程。
sprintf_s(course_students_filename, 50, "bin//course_students//%d.txt", course_number);
err = fopen_s(&fp_read_course_students, course_students_filename, "r+");
err = fopen_s(&fp_rewrite_course_students, "bin//course_students//tmp.txt", "w+");
_itoa_s(student_number, student_id, 10);
while ((fscanf_s(fp_read_course_students, "%s", linedata, 11)) != EOF) {
if (strcmp(linedata, student_id) != 0)
fprintf(fp_rewrite_course_students, "%s ", linedata);
}
fclose(fp_read_course_students);
fclose(fp_rewrite_course_students);
remove(course_students_filename);
rename("bin//course_students//tmp.txt", course_students_filename);
//删除课程的学生名单信息文件中该学生的学号。
return;
}
//学生更改电话
int student_phone_change(int student_id, long long new_phone_num) {
int total_student_num = get_total_student_num();
students* all_stu;
all_stu = get_student_info();
for (int i = 0; i < get_total_student_num(); i++) {
if (all_stu[i].id == student_id) {
all_stu[i].phone_number = new_phone_num;
break;
}
}
FILE* student_file;
fopen_s(&student_file, "bin//user_student.txt", "w");
for (int i = 0; i < total_student_num; i++)
fprintf_s(student_file, "%d %lld %s %s %s %s %s\n", all_stu[i].id, all_stu[i].phone_number, all_stu[i].department, all_stu[i].sex, all_stu[i].email, all_stu[i].password, all_stu[i].name);
fclose(student_file);
free(all_stu);
return 0;
}
//学生更改邮箱
int student_email_change(int student_id, char*email) {
int total_student_num = get_total_student_num();
students* all_stu;
all_stu = get_student_info();
for (int i = 0; i < get_total_student_num(); i++) {
if (all_stu[i].id == student_id) {
strcpy_s(all_stu[i].email, email);
break;
}
}
FILE* student_file;
fopen_s(&student_file, "bin//user_student.txt", "w");
for (int i = 0; i < total_student_num; i++)
fprintf_s(student_file, "%d %lld %s %s %s %s %s\n", all_stu[i].id, all_stu[i].phone_number, all_stu[i].department, all_stu[i].sex, all_stu[i].email, all_stu[i].password, all_stu[i].name);
fclose(student_file);
free(all_stu);
return 0;
}
//学生更改密码
int student_password_change(int student_id, char* password) {
int total_student_num = get_total_student_num();
students* all_stu;
all_stu = get_student_info();
for (int i = 0; i < get_total_student_num(); i++) {
if (all_stu[i].id == student_id) {
strcpy_s(all_stu[i].password , password);
break;
}
}
FILE* student_file;
fopen_s(&student_file, "bin//user_student.txt", "w");
for (int i = 0; i < total_student_num; i++)
fprintf_s(student_file, "%d %lld %s %s %s %s %s\n", all_stu[i].id, all_stu[i].phone_number, all_stu[i].department, all_stu[i].sex, all_stu[i].email, all_stu[i].password, all_stu[i].name);
fclose(student_file);
free(all_stu);
return 0;
}