-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.c
executable file
·701 lines (631 loc) · 20.7 KB
/
student.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include "student.h"
#include "show.h"
/*#include "list.h"*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//功 能: 学生的主要功能选择
//返回值: 0, 正常返回
// -1,文件不存在、打开或创建失败
// -2, 退出登录
int stu_opt()
{
STUDENT stu;
int ret = stu_vertify(&stu);
if(ret == -1)
return -1;
if(ret == -2)
return -2;
alt_tips("学生登录成功");
for(;;) {
stu_opt_show();
login_stu_msg(stu);
int select = get_select();
switch(select)
{
case 1: //老师查询
/*
*ret = stu_add();
*if( ret==0 ) {
* alt_tips("添加成功");
*} else if(ret == -1) {
* alt_tips("文件错误,密码修改失败");
*} else if(ret == -2) {
* alt_tips("已取消添加");
*}
*/
break;
case 2: //学生修改密码
break;
case -1:
return -2;
default:
break;
}
}
}
//功 能: 添加学生到数据
//返回值: 0,添加成功
// -1,文件操作失败
// -2, 取消添加
int stu_add()
{
if( access(STU_DB, F_OK) != 0 ) {
//没有学生数据,初始化数据文件
FILE *fp = fopen(STU_DB, "w");
ERRP( fp==NULL, "fopen", return -1);
int id_start = STU_ID_START;
fwrite(&id_start, 4, 1, fp);
fclose(fp);
}
alt_tips("");
stu_add_show();
STUDENT stu;
FILE *fp = fopen(STU_DB, "r");
ERRP( fp==NULL, "fopen", return -1);
fread(&stu.id, 4, 1, fp);
fclose(fp);
printf("\033[%d;%dH\033[34m添加学号 \033[32m%d \033[34m的学生信息.\033[0m",MAIN_X_SUB-2, MAIN_Y_SUB + 15, ++stu.id);
printf("\033[%d;%dH", MAIN_X_SUB, MAIN_Y_SUB+10);
get_name(stu.name, MAIN_X_SUB, MAIN_Y_SUB+10);
if( strcmp(stu.name, "B")==0 || strcmp(stu.name, "b")==0 )
return -2;
printf("\033[%d;%dH", MAIN_X_SUB+2, MAIN_Y_SUB+10);
get_gender(stu.gender, MAIN_X_SUB+2, MAIN_Y_SUB+10);
if( strcmp(stu.gender, "B")==0 || strcmp(stu.gender, "b")==0 )
return -2;
char ch_num[4];
printf("\033[%d;%dH", MAIN_X_SUB+4, MAIN_Y_SUB+10);
get_int(ch_num);
if( strcmp(ch_num, "B")==0 || strcmp(ch_num, "b")==0 )
return -2;
stu.age = atoi(ch_num);//年龄
if(stu.age<=0 || stu.age >=100)
stu.age = 23;
printf("\033[%d;%dH", MAIN_X_SUB+6, MAIN_Y_SUB+10);
get_int(ch_num);
if( strcmp(ch_num, "B")==0 || strcmp(ch_num, "b")==0 )
return -2;
stu.c_score = atoi(ch_num);//c成绩
if(stu.c_score<0 || stu.c_score >100)
stu.c_score = 0;
printf("\033[%d;%dH", MAIN_X_SUB+8, MAIN_Y_SUB+10);
get_int(ch_num);
if( strcmp(ch_num, "B")==0 || strcmp(ch_num, "b")==0 )
return -2;
stu.cpp_score = atoi(ch_num);//cpp成绩
if(stu.cpp_score<0 || stu.cpp_score >100)
stu.cpp_score = 0;
strcpy(stu.psw,"123");
alt_tips("");
confirm_show("是否添加此学生信息?");
add_stu_dislay(stu);
printf("\033[%d;%dH\033[0m", MAIN_X_SUB-5, MAIN_Y_SUB + 6);
char ch;
scanf("%c", &ch);
while(getchar() != '\n');
if(ch != 'y' && ch != 'Y') {
return -2;
}
fp = fopen(STU_DB, "r+");
ERRP( fp==NULL, "fopen", return -1);
fwrite(&stu.id, 4, 1, fp);
fseek(fp, 0, SEEK_END);
fwrite(&stu, sizeof(STUDENT), 1, fp);
fclose(fp);
system("clear");
return 0;
}
//功 能: 新添加的学生显示在侧边栏
void add_stu_dislay(STUDENT stu)
{
printf("\033[%d;%dH\033[36m", MAIN_X_SUB -1, DIVISION_START_COL+20);
printf("学 号: %d",stu.id);
printf("\033[%d;%dH", MAIN_X_SUB + 1, DIVISION_START_COL+20);
printf("姓 名: %s",stu.name);
printf("\033[%d;%dH", MAIN_X_SUB + 3, DIVISION_START_COL+20);
printf("性 别: %s",stu.gender);
printf("\033[%d;%dH", MAIN_X_SUB + 5, DIVISION_START_COL+20);
printf("年 龄: %d",stu.age);
printf("\033[%d;%dH", MAIN_X_SUB + 7, DIVISION_START_COL+20);
printf("C 成绩: %d",stu.c_score);
printf("\033[%d;%dH", MAIN_X_SUB + 9, DIVISION_START_COL+20);
printf("CPP 成绩: %d",stu.cpp_score);
printf("\033[%d;%dH", MAIN_X_SUB + 11, DIVISION_START_COL+20);
printf("密 码: %s",stu.psw);
printf("\033[0m");
fflush(NULL);
}
//功 能: 显示登录学生的信息在作者栏以上
void login_stu_msg(STUDENT stu)
{
printf("\033[%d;%dH\033[33m", AUTHOR_X_SUB-3, DIVISION_START_COL);
printf("|学号: ");
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+6);
printf("%d", stu.id);
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+17+6);
printf("|姓名:");
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+6+24);
printf("%s",stu.name);
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+17+30);
printf("|性别:");
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+6+47);
printf("%s",stu.gender);
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+17+53);
printf("|年龄:");
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+6+70);
printf("%d",stu.age);
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+17+76);
printf("|c成绩:");
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+7+93);
printf("%d",stu.c_score);
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+17+100);
printf("|cpp成绩:");
printf("\033[%d;%dH", AUTHOR_X_SUB-3, DIVISION_START_COL+9+117);
printf("%d",stu.cpp_score);
printf("\033[%d;%dH\033[0m", MAIN_X_SUB-5, MAIN_Y_SUB + 6);
fflush(NULL);
}
//功 能: 学生验证
//返回值: 0,验证成功
// -1,文件操作失败
// -2,取消登录,返回主界面
// -3,没有学生信息
int stu_vertify(STUDENT *stu)
{
if( access(STU_DB, F_OK) != 0 ) {
return -1;
}
int last_id,tmp_id=0;
FILE *fp = fopen(STU_DB, "r");
ERRP( fp==NULL, "fopen", return -1);
fread(&last_id, 4, 1, fp);
fclose(fp);
if( last_id == STU_ID_START ) {
return -3;
}
alt_tips("");
stu_tea_vertify_show();
for(;;) {
cover_char( MAIN_X_SUB, MAIN_Y_SUB + 12 , CONSOLE_LEN - (MAIN_Y_SUB + 12));
cover_char( MAIN_X_SUB + 2, MAIN_Y_SUB + 12 , CONSOLE_LEN - (MAIN_Y_SUB + 12));
char ch_id[11];
for(;;) { //获取学生ID;
printf("\033[%d;%dH", MAIN_X_SUB, MAIN_Y_SUB+12);
get_int(ch_id);
if( strcmp(ch_id, "B")==0 || strcmp(ch_id, "b")==0 )
return -2;
tmp_id = atoi(ch_id);
if(tmp_id <= STU_ID_START || tmp_id > last_id) {
cover_char( WARNING_X, 0, CONSOLE_LEN);
printf("\033[%d;%dH\033[31m没有学号为 %d 的学生\033[0m\n",WARNING_X, WARNING_Y, tmp_id);
cover_char( MAIN_X_SUB, MAIN_Y_SUB + 12 , CONSOLE_LEN - (MAIN_Y_SUB + 12));
continue;
}
cover_char( WARNING_X, 0, CONSOLE_LEN);
break;
}
char str_psw[PSW_LEN+1];
printf("\033[%d;%dH", MAIN_X_SUB + 2, MAIN_Y_SUB+12);
get_psw(str_psw); //获取学生登录密码
if( strcmp(str_psw, "B")==0 || strcmp(str_psw, "b")==0 )
return -2;
FILE *fp = fopen(STU_DB, "r");
ERRP( fp==NULL, "fopen", return -1);
fseek( fp, 4+(tmp_id-STU_ID_START-1)*sizeof(STUDENT), SEEK_SET);
fread(stu, sizeof(STUDENT), 1, fp);
fclose(fp);
if( stu->id == STU_DELETE_ID ) {
cover_char( WARNING_X, 0, CONSOLE_LEN);
printf("\033[%d;%dH\033[31m学号为 %d 的学生已被管理员删除\033[0m\n",WARNING_X, WARNING_Y, stu->id);
continue;
}
if( strcmp(str_psw, stu->psw) != 0) {
cover_char( WARNING_X, 0, CONSOLE_LEN);
printf("\033[%d;%dH\033[31m用户名或密码错误\033[0m\n",WARNING_X, WARNING_Y);
continue;
}
break;
}
return 0;
}
//功 能: 学生查找
//返回值: 0,退出查询
// -1,文件错误
int stu_find()
{
if( access(STU_DB, F_OK) != 0 ) {
return -1;
}
alt_tips("学生信息");
stu_find_show();
find_stu_msg();
int len=0;
FILE *fp = fopen(STU_DB, "r");
ERRP( fp==NULL, "fopen", return -1);
fread(&len, 4, 1, fp);
fclose(fp);
len -= STU_ID_START;
//显示学生信息
STU_LIST handle;
memset(&handle, 0, sizeof(STU_LIST));
handle.next = &handle;
handle.prev = &handle;
/*将STU_SHOW_CNT个学生的信息,读到handle中*/
stu_list_read( &handle, 0, len);
int cnt=0,tmp;
char ch;
const int GAP = 15;
const int offset = 24;
printf("\033[%d;%dH\033[32m", MAIN_X_SUB , DIVISION_START_COL+offset);
printf("学号");
printf("\033[%d;%dH", MAIN_X_SUB, DIVISION_START_COL+offset+GAP);
printf("姓名");
printf("\033[%d;%dH", MAIN_X_SUB, DIVISION_START_COL+offset+2*GAP);
printf("性别");
printf("\033[%d;%dH", MAIN_X_SUB, DIVISION_START_COL+offset+3*GAP);
printf("年龄");
printf("\033[%d;%dH", MAIN_X_SUB, DIVISION_START_COL+offset+4*GAP);
printf("C成绩");
printf("\033[%d;%dH", MAIN_X_SUB, DIVISION_START_COL+offset+5*GAP);
printf("Cpp成绩");
fflush(NULL);
for(;;) {
/*将handle中的信息显示出来*/
tmp = stu_list_display(&handle, cnt, SHOW_CNT);
cnt += tmp;
INPUT:
//光标定位在输入区
cover_char( MAIN_X_SUB-5 , MAIN_Y_SUB+6, CONSOLE_LEN-(MAIN_Y_SUB+6));
printf("\033[%d;%dH\033[0m", MAIN_X_SUB-5, MAIN_Y_SUB + 6);
//用户输入选择
ch = getchar();
if(ch=='\n') {
break;
}
while(getchar()!='\n');
cover_char( WARNING_X, 0, CONSOLE_LEN);
switch(ch)
{
case 'n':
if(len <= cnt) {
cnt -= tmp;
printf("\033[%d;%dH\033[31m已经是最后页\033[0m\n",WARNING_X, WARNING_Y);
} else {
clear_stu_display();
}
break;
case 'N':
cnt -= tmp;
cnt -= SHOW_CNT;
if(cnt < 0) {
cnt += SHOW_CNT;
printf("\033[%d;%dH\033[31m已经是开始页\033[0m\n",WARNING_X, WARNING_Y);
} else {
clear_stu_display();
}
break;
case 'Q':
case 'q':
system("clear");
exit(0);
case 'B':
case 'b':
return 0;
case '1':
clear_stu_display();
stu_sort_id(&handle);
cnt =0;
break;
case '2':
clear_stu_display();
stu_sort_gender(&handle);
cnt =0;
break;
case '3':
clear_stu_display();
stu_sort_age(&handle);
cnt =0;
break;
case '4':
clear_stu_display();
stu_sort_c(&handle);
cnt =0;
break;
case '5':
clear_stu_display();
stu_sort_cpp(&handle);
cnt =0;
break;
default:
cover_char( WARNING_X, 0, CONSOLE_LEN);
printf("\033[%d;%dH\033[31m请按提示输入\033[0m\n",WARNING_X, WARNING_Y);
goto INPUT;
}
}
//获取学号
char ch_id[6]="";
printf("\033[%d;%dH\033[32m", MAIN_X_SUB-3, DIVISION_START_COL + 6);
fgets(ch_id, NAME_LEN+1, stdin);
if(ch_id[strlen(ch_id)-1] == '\n')
ch_id[strlen(ch_id)-1] = '\0';
else
while(getchar()!='\n');
if( strcmp(ch_id, "B")==0 || strcmp(ch_id, "b")==0 )
return 0;
//获取名字
char ch_name[NAME_LEN+1]="";
printf("\033[%d;%dH", MAIN_X_SUB-3, DIVISION_START_COL+17+6 + 6);
fgets(ch_name, NAME_LEN+1, stdin);
if(ch_name[strlen(ch_name)-1] == '\n')
ch_name[strlen(ch_name)-1] = '\0';
else
while(getchar()!='\n');
if( strcmp(ch_name, "B")==0 || strcmp(ch_name, "b")==0 )
return 0;
/*
//获取性别
char ch_gender[4]="";
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+30+6);
if(ch_gender[strlen(ch_gender)-1] == '\n')
ch_gender[strlen(ch_gender)-1] = '\0';
else
while(getchar()!='\n');
if( strcmp(ch_gender, "B")==0 || strcmp(ch_gender, "b")==0 )
return 0;
//获取年龄
char ch_age[4]="";
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+53+6);
get_int(ch_age);
if( strcmp(ch_age, "B")==0 || strcmp(ch_age, "b")==0 )
return 0;
//获取c成绩
char ch_c[4]="";
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+76+6);
get_int(ch_c);
if( strcmp(ch_c, "B")==0 || strcmp(ch_c, "b")==0 )
return 0;
char ch_cpp[4]="";
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+100+6);
get_int(ch_cpp);
if( strcmp(ch_cpp, "B")==0 || strcmp(ch_cpp, "b")==0 )
return 0;
*/
clear_stu_display();
STU_LIST *tail = handle.next;
if(strcmp(ch_id,"")!=0) {
int id = atoi(ch_id);
for(tail=handle.next; tail!=&handle; tail =tail->next) {
if(tail->stu.id == id) {
stu_data_display(&tail->stu, 1);
break;
}
}
if(tail==&handle) {
printf("\033[%d;%dH\033[32m", MAIN_X_SUB+2 , DIVISION_START_COL+24);
printf("查询没有结果\n");
}
} else {
printf("\033[%d;%dH\033[32m", MAIN_X_SUB+2 , DIVISION_START_COL+24);
printf("查询没有结果.\n");
}
printf("\033[0m");
getchar();
stu_free(&handle);
}
//学号排序
void stu_sort_id(STU_LIST *head)
{
STU_LIST *p = head->next;
STU_LIST *q = head->next;
STUDENT stu;
for(p=head; p->next!=head; p=p->next) {
for(q=head->next; q!=head; q=q->next) {
if(p->stu.id > q->stu.id) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
} //end if
} //end q
}
p = head->prev;
for(q=head->next;q!=p;q=q->next) {
if(p->stu.id > q->stu.id) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
}
}
}
//性别排序
void stu_sort_gender(STU_LIST *head)
{
STU_LIST *p = head->next;
STU_LIST *q = head->next;
STUDENT stu;
for(p=head; p->next!=head; p=p->next) {
for(q=head->next; q!=head; q=q->next) {
if(strcmp(p->stu.gender, q->stu.gender)>0) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
} //end if
} //end q
}
p = head->prev;
for(q=head->next;q!=p;q=q->next) {
if(strcmp(p->stu.gender, q->stu.gender)>0) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
}
}
}
//年龄排序
void stu_sort_age(STU_LIST *head)
{
STU_LIST *p = head->next;
STU_LIST *q = head->next;
STUDENT stu;
for(p=head; p->next!=head; p=p->next) {
for(q=head->next; q!=head; q=q->next) {
if(p->stu.age > q->stu.age) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
} //end if
} //end q
}
p = head->prev;
for(q=head->next;q!=p;q=q->next) {
if(p->stu.age > q->stu.age) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
}
}
}
//c成绩排序
void stu_sort_c(STU_LIST *head)
{
STU_LIST *p = head->next;
STU_LIST *q = head->next;
STUDENT stu;
for(p=head; p->next!=head; p=p->next) {
for(q=head->next; q!=head; q=q->next) {
if(p->stu.c_score > q->stu.c_score) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
} //end if
} //end q
}
p = head->prev;
for(q=head->next;q!=p;q=q->next) {
if(p->stu.c_score > q->stu.c_score) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
}
}
}
//cpp成绩排序
void stu_sort_cpp(STU_LIST *head)
{
STU_LIST *p = head->next;
STU_LIST *q = head->next;
STUDENT stu;
for(p=head; p->next!=head; p=p->next) {
for(q=head->next; q!=head; q=q->next) {
if(p->stu.cpp_score > q->stu.cpp_score) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
} //end if
} //end q
}
p = head->prev;
for(q=head->next;q!=p;q=q->next) {
if(p->stu.cpp_score > q->stu.cpp_score) {
memcpy(&stu, &q->stu, sizeof(STUDENT));
memcpy(&q->stu, &p->stu, sizeof(STUDENT));
memcpy(&p->stu,&stu, sizeof(STUDENT));
}
}
}
//功 能: 释放空间
void stu_free(STU_LIST *head)
{
STU_LIST *tail, *tmp;
for(tail = head->next; tail!=head; tail = tmp ) {
tmp = tail->next;
free(tail);
}
}
//功 能: 在handle中存入STU_DB中start开始的cnt个数据
//返回值: 实际存入hanlde中的数据
int stu_list_read(STU_LIST *handle, int start, int cnt)
{
FILE *fp = fopen(STU_DB, "r");
ERRP( fp==NULL, "fopen", return 0);
int i,len;
fread(&len, 4, 1, fp);
len = len - STU_ID_START;
if(len <= start)
return 0;
if(len > cnt+start)
len = cnt+start;
fseek(fp, 4+(start)*sizeof(STUDENT), SEEK_SET);
STU_LIST *tail= handle;
for(i=0; i<len-start; ++i,tail= tail->next) {
STU_LIST *newD = (STU_LIST *)malloc(sizeof(STU_LIST));
fread(&newD->stu, sizeof(STUDENT), 1, fp);
newD->next = handle;
newD->prev = tail;
tail->next = newD;
handle->prev = newD;
}
return len-start;
}
//功 能: 显示handle中的数据
int stu_list_display(STU_LIST *handle, int start, int cnt)
{
STU_LIST *tail;
int i;
for(tail=handle->next, i=0; tail!=handle && i!=start ; ++i, tail = tail->next);
if(tail == handle)
return 0;
for(i=0; tail!=handle && i<cnt; ++i, tail = tail->next) {
stu_data_display(&tail->stu, i);
}
return i;
}
//功 能: 覆盖已经显示的学生信息
void clear_stu_display()
{
int num;
for(num=0;num<SHOW_CNT; ++num) {
cover_char( 2+MAIN_X_SUB + 2*num, 0, CONSOLE_LEN);
}
fflush(NULL);
}
//显示学生结构体信息
void stu_data_display(STUDENT *stu, int num)
{
const int GAP = 15;
const int offset = 24;
printf("\033[%d;%dH\033[32m",2+ MAIN_X_SUB+2*num , DIVISION_START_COL+offset);
printf("%d",stu->id);
printf("\033[%d;%dH",2+ MAIN_X_SUB+2*num, DIVISION_START_COL+offset+GAP);
printf("%s",stu->name);
printf("\033[%d;%dH",2+ MAIN_X_SUB+2*num, DIVISION_START_COL+offset+2*GAP);
printf("%s",stu->gender);
printf("\033[%d;%dH", 2+MAIN_X_SUB+2*num, DIVISION_START_COL+offset+3*GAP);
printf("%d",stu->age);
printf("\033[%d;%dH",2+ MAIN_X_SUB+2*num, DIVISION_START_COL+offset+4*GAP);
printf("%d",stu->c_score);
printf("\033[%d;%dH",2+ MAIN_X_SUB+2*num, DIVISION_START_COL+offset+5*GAP);
printf("%d",stu->cpp_score);
printf("\033[%d;%dH\033[0m", MAIN_X_SUB-5, MAIN_Y_SUB + 6);
fflush(NULL);
}
//功 能: 显示查找条件
void find_stu_msg()
{
printf("\033[%d;%dH\033[33m", MAIN_X_SUB-3, DIVISION_START_COL);
printf("学号:_______");
/*
printf("\033[%d;%dH", MAIN_X_SUB-3, DIVISION_START_COL+17+6);
printf("姓名:_______");
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+30);
printf("性别:_______");
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+53);
printf("年龄:________");
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+76);
printf("C :_______");
printf("\033[%d;%dH", MAIN_X_SUB -3, DIVISION_START_COL+17+100);
printf("Cpp :_____");
*/
printf("\033[%d;%dH\033[0m", MAIN_X_SUB-5, MAIN_Y_SUB + 6);
fflush(NULL);
}