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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
| #include <stdio.h> #include <stdlib.h> #include <string.h>
enum sex{women,man}; struct StuLink{ int xh; char xm[20]; enum sex xb; int cj; char dj; int mc; struct StuLink *next; }; char sex[][3]={"女","男"}; int size=sizeof(struct StuLink);
void main(){
int menu1,menu2; int i=0,n,dj_add[5]={0,0,0,0,0}; struct StuLink *head=NULL,*pw;
struct StuLink *ReadFromFile();
void SaveToFile(struct StuLink *head);
struct StuLink *SortLink(struct StuLink *head,int i);
struct StuLink *InsertNode(struct StuLink *pw);
void EditNode(struct StuLink *head);
struct StuLink *DeleteNode(struct StuLink *head);
void QueryNode(struct StuLink *head);
void QueryLink(struct StuLink *head);
void RankLink(struct StuLink *head);
void AnalysisLink(struct StuLink *head,int *dj_add);
void OutputLink_1(struct StuLink *head,int i);
void OutputLink_2(struct StuLink *head,int i);
head=ReadFromFile();
while(1){ system("cls"); printf("========================================\n"); printf("= =\n"); printf("= 学生成绩管理程序 =\n"); printf("= by:xxx =\n"); printf("========================================\n"); printf("= =\n"); printf("= 1-数据维护 2-数据查询 =\n"); printf("= 3-统计分析 4-报表输出 =\n"); printf("= 0-退出 =\n"); printf("= =\n"); printf("========================================\n");
printf("请选择:"); scanf("%d",&menu1); switch(menu1){ case 1: while(1){ system("cls"); printf("\n"); printf("========================================\n"); printf("= =\n"); printf("= 1-数据插入 2-数据修改 =\n"); printf("= 3-数据删除 0-返回上级 =\n"); printf("= =\n"); printf("========================================\n"); printf("请选择:"); scanf("%d",&menu2); switch(menu2){ case 1: printf("请输入要插入的学生个数(n>0): "); scanf("%d",&n); while(n<=0){ printf("!!!请输入正确的学生个数!!!: "); scanf("%d",&n); } pw=head; if(pw){ while(pw->next) pw=pw->next; } for(i=0;i<n;i++){ pw=InsertNode(pw); if(head==NULL) head=pw; } RankLink(head);
break; case 2: EditNode(head); RankLink(head);
break; case 3: head=DeleteNode(head); RankLink(head);
break; case 0: break; } if(menu2==0) break; } break; case 2: while(1){ system("cls"); printf("\n"); printf("========================================\n"); printf("= =\n"); printf("=1-学号查询 2-不及格学生查询 0-返回上级=\n"); printf("= =\n"); printf("========================================\n"); printf("请选择:"); scanf("%d",&menu2); switch(menu2){ case 1: QueryNode(head); printf("\n"); system("pause"); break; case 2: QueryLink(head); printf("\n"); system("pause"); break; case 0: break; } if(menu2==0) break; } break; case 3: while(1){ system("cls"); printf("\n"); printf("========================================\n"); printf("= =\n"); printf("= 1-成绩名次计算 2-成绩频次分析 =\n"); printf("= 0-返回上级 =\n"); printf("= =\n"); printf("========================================\n"); printf("请选择:"); scanf("%d",&menu2); switch(menu2){ case 1: RankLink(head); break; case 2: AnalysisLink(head,dj_add); printf("'A'有%d人;'B'有%d人,'C'有%d人,'D'有%d人,'E'有%d人,共有%d人\n", dj_add[0],dj_add[1],dj_add[2],dj_add[3],dj_add[4],dj_add[0]+dj_add[1]+dj_add[2]+dj_add[3]+dj_add[4]); printf("\n"); system("pause"); break; case 0: break; } if(menu2==0) break; } break; case 4: while(1){ system("cls"); printf("\n"); printf("========================================\n"); printf("= =\n"); printf("=1-排序显示学生信息 2-分页显示学生信息=\n"); printf("= 0-返回上级 =\n"); printf("= =\n"); printf("========================================\n"); printf("请选择:"); scanf("%d",&menu2); switch(menu2){ case 1: printf("请选择按【1:学号(升序)】或者【2:成绩(降序)】输出学生成绩表:"); scanf("%d",&i); while((i!=1)&&(i!=2)){ printf("!!!请选择正确选项!!!1 or 2 : "); scanf("%d",&i); } OutputLink_1(head,i); printf("\n"); system("pause"); break; case 2: printf("请选择按【1:学号(升序)】或者【2:成绩(降序)】输出学生成绩表:"); scanf("%d",&i); while((i!=1)&&(i!=2)){ printf("!!!请选择正确选项!!!1 or 2 : "); scanf("%d",&i); } OutputLink_2(head,i); break; case 0: break; } if(menu2==0) break; } break; case 0: printf("========================================\n"); printf("= =\n"); printf("= 你已经退出学生管理系统 =\n"); printf("= =\n"); printf("========================================\n"); return; } }
}
struct StuLink *ReadFromFile(){ FILE *fp; struct StuLink *p1,*p2,*head=NULL; char ch; if((fp=fopen("student.dat","r"))==NULL){ printf("打开student.dat失败\n"); exit(0); } ch=fgetc(fp); if(ch==EOF){ printf("文件为空\n"); return head; }else{ rewind(fp); } while(!feof(fp)){ if((p1=(struct StuLink *)malloc(size))==NULL) { printf("不能成功分配储存块"); exit(0); } p1->next=NULL; if(head==NULL) head=p1; else p2->next=p1; p2=p1; fscanf(fp,"%d %s %d %d %s %d\n",&p1->xh,p1->xm,&p1->xb,&p1->cj,&p1->dj,&p1->mc);
printf("%-5d%-10s%-5d%-5d%-5c%-5d\n",p1->xh,p1->xm,p1->xb,p1->cj,p1->dj,p1->mc); } fclose(fp); return head; }
struct StuLink *SortLink(struct StuLink *head,int i){ if(head==NULL)return head;
int xh,xb,cj,mc; char xm[20],dj;
struct StuLink *p1=head,*p2=head->next,*p3=p1; switch(i){ case 1:
while(p1->next){ p2=p1->next; p3=p1; while(p2){ if(p3->xh>p2->xh) p3=p2; p2=p2->next; } if(p3->xh<p1->xh){ xh=p3->xh; xb=p3->xb; cj=p3->cj; mc=p3->mc; strcpy(xm,p3->xm); dj=p3->dj; p3->xh=p1->xh; p3->xb=p1->xb; p3->cj=p1->cj; p3->mc=p1->mc; strcpy(p3->xm,p1->xm); p3->dj=p1->dj; p1->xh=xh; p1->xb=xb; p1->cj=cj; p1->mc=mc; strcpy(p1->xm,xm); p1->dj=dj; } p1=p1->next; } break; case 2:
while(p1->next){ p2=p1->next; p3=p1; while(p2){ if(p3->cj<p2->cj) p3=p2; p2=p2->next; } if(p3->cj>p1->cj){ xh=p3->xh; xb=p3->xb; cj=p3->cj; mc=p3->mc; strcpy(xm,p3->xm); dj=p3->dj; p3->xh=p1->xh; p3->xb=p1->xb; p3->cj=p1->cj; p3->mc=p1->mc; strcpy(p3->xm,p1->xm); p3->dj=p1->dj; p1->xh=xh; p1->xb=xb; p1->cj=cj; p1->mc=mc; strcpy(p1->xm,xm); p1->dj=dj; } p1=p1->next; } break; } return head; }
void SaveToFile(struct StuLink *head){ FILE *fp; int i=1; head=SortLink(head,i); struct StuLink *p=head; if((fp=fopen("student.dat","w"))==NULL){ printf("写开文件失败!"); exit(0); } while(p){
fprintf(fp,"%d %s %d %d %c %d\n",p->xh,p->xm,p->xb,p->cj,p->dj,p->mc); p=p->next; } fclose(fp); }
struct StuLink *InsertNode(struct StuLink *pw){ struct StuLink *p1; if((p1=(struct StuLink *)malloc(size))==NULL) { printf("不能成功分配储存块\n"); exit(0); } p1->next=NULL; if(pw) p1->xh=pw->xh+1; else p1->xh=1; char xm[20]; printf("请依次输入姓名、性别(男1女0)、成绩[0,100]: "); scanf("%s%d%d",xm,&p1->xb,&p1->cj); while(p1->xb!=0&&p1->xb!=1){ printf("! ! ! 性别只能是1或0,请重新输入性别1:男 0:女 "); scanf("%d",&p1->xb); } while(p1->cj<0 || p1->cj>100){ printf("! ! ! 成绩范围是0~100,请重新输入成绩 "); scanf("%d",&p1->cj); } strcpy(p1->xm,xm);
if(p1->cj>=90) p1->dj='A'; else if(p1->cj>=80) p1->dj='B'; else if(p1->cj>=70) p1->dj='C'; else if(p1->cj>=60) p1->dj='D'; else p1->dj='E'; if(pw==NULL) pw=p1; else{ pw->next=p1; pw=pw->next; } return pw; }
void EditNode(struct StuLink *head){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } struct StuLink *p1=head; char xm[20]; int n,re; printf("请输入你要修改的学生信息的学号:"); scanf("%d",&n) ; while(p1){ if(p1->xh==n){ break; }else{ p1=p1->next; } } if(p1){ printf("你要修改的学生信息为:\n"); printf("学号 姓名 性别 成绩 等级 名次 ##男1女0##\n"); printf("%-6d%-10s%-6d%-6d%-6c%-6d\n",p1->xh,p1->xm,p1->xb,p1->cj,p1->dj,p1->mc); } else{ printf("你输入的学号有误!\n"); printf("\n"); system("pause"); return; } printf("请选择要修改的数据项:0-不修改 1-姓名 2-性别 3-成绩 "); scanf("%d",&re); while(re<0||re>3){ printf("输入有误!重新输入: "); scanf("%d",&re); } switch(re){ case 0:break; case 1: printf("请输出新的姓名:"); scanf("%s",xm); strcpy(p1->xm,xm); break; case 2: printf("请输入新的性别:"); scanf("%d",&p1->xb); while(p1->xb!=0&&p1->xb!=1){ printf("! ! ! 性别只能是1或0,请重新输入性别1:男 0:女 "); scanf("%d",&p1->xb); } break; case 3: printf("请输入新的成绩:"); scanf("%d",&p1->cj); while(p1->cj<0 || p1->cj>100){ printf("! ! ! 成绩范围是0~100,请重新输入成绩 "); scanf("%d",&p1->cj); } if(p1->cj>=90) p1->dj='A'; else if(p1->cj>=80) p1->dj='B'; else if(p1->cj>=70) p1->dj='C'; else if(p1->cj>=60) p1->dj='D'; else p1->dj='E'; break; } }
struct StuLink *DeleteNode(struct StuLink *head){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return head; } struct StuLink *p1=head,*p2; int del_xh; printf("请输入要删除的学号:"); scanf("%d",&del_xh); while((p1->xh!=del_xh)&&(p1->next!=NULL)){ p2=p1;p1=p1->next; } if(p1->xh==del_xh){ if(head==p1) head=head->next; else p2->next=p1->next; free(p1); printf("删除成功!\n"); }else printf("未找到此学号对应节点!\n"); printf("\n"); system("pause"); return head; }
void QueryNode(struct StuLink *head){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } int query_xh; struct StuLink *p1=head,*p2; printf("请输入要查询的学生学号:"); scanf("%d",&query_xh); while((p1->xh!=query_xh)&&(p1->next!=NULL)){ p2=p1;p1=p1->next; } if(p1->xh==query_xh){ printf("查找结果为:\n"); printf("学号:%-5d姓名:%-10s成绩:%-5d等级:%-5c名次:%-5d",p1->xh,p1->xm,p1->cj,p1->dj,p1->mc); if(p1->xb==1) printf("性别:男\n"); else printf("性别:女\n"); }else printf("未找到该学号对应学生!\n"); }
void QueryLink(struct StuLink *head){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } struct StuLink *p1=head; int n=0; while(p1){ if(p1->cj<60){ n++; if(n==1) printf("未及格学生信息为:\n"); printf("学号:%-5d姓名:%-10s成绩:%-5d等级:%-5c名次:%-5d",p1->xh,p1->xm,p1->cj,p1->dj,p1->mc); if(p1->xb==1) printf("性别:男\n"); else printf("性别:女\n"); } p1=p1->next; } if(n==0) printf("没有不及格学生!\n"); }
void RankLink(struct StuLink *head){ int i=2; if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } head=SortLink(head,i); struct StuLink *p1=head,*p2; int n=1; while(p1){ p1->mc=n; n++; if((p1!=head)&&((p1->cj)==(p2->cj))) p1->mc=p2->mc; p2=p1; p1=p1->next; } SaveToFile(head); }
void AnalysisLink(struct StuLink *head,int *dj_add){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } struct StuLink *p1=head; while(p1){ if(p1->cj>=90) dj_add[0]++; else if(p1->cj>=80) dj_add[1]++; else if(p1->cj>=70) dj_add[2]++; else if(p1->cj>=60) dj_add[3]++; else dj_add[5]++; p1=p1->next; } }
void OutputLink_1(struct StuLink *head,int i){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } head=SortLink(head,i); struct StuLink *p1=head; printf("所有学生信息为:\n"); printf("学号 姓名 性别 成绩 等级 名次\n"); while(p1){ printf("%-6d%-10s",p1->xh,p1->xm); if(p1->xb==1) printf("男 "); else printf("女 "); printf("%-6d%-6c%-6d\n",p1->cj,p1->dj,p1->mc); p1=p1->next; } }
void OutputLink_2(struct StuLink *head,int i){ if(head==NULL){ printf("学生数据为空!\n"); printf("\n"); system("pause"); return; } head=SortLink(head,i);
int n=1,num1,sum=0,page_sum,page_now=1,pf_num=0; struct StuLink *p1=head; while(p1){ sum++; p1=p1->next; } p1=head; page_sum=sum/10+1; system("cls"); printf("学号 姓名 性别 成绩 等级 名次\n"); pf_num=1; while(p1&&pf_num<=10){ printf("%-6d%-10s",p1->xh,p1->xm); if(p1->xb==1) printf("男 "); else printf("女 "); printf("%-6d%-6c%-6d\n",p1->cj,p1->dj,p1->mc); pf_num++; p1=p1->next; } if(sum<=10){ printf("\n只有一页,暂无上下页功能\n"); system("pause"); return ; }else{ printf("\n 2-下一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n",page_now,page_sum); scanf("%d",&num1); while(num1<2||num1>5){ printf("请输入正确的数字: "); scanf("%d",&num1); } } while(1) { switch(num1){ case 1: system("cls"); p1=head; n=1; while(n<=page_now*10-20){ p1=p1->next; n++; } printf("学号 姓名 性别 成绩 等级 名次\n"); pf_num=1; while(p1&&pf_num<=10){ printf("%-6d%-10s",p1->xh,p1->xm); if(p1->xb==1) printf("男 "); else printf("女 "); printf("%-6d%-6c%-6d\n",p1->cj,p1->dj,p1->mc); pf_num++; p1=p1->next; } page_now--; if(page_now==1){ printf("\n 2-下一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n",page_now,page_sum); scanf("%d",&num1); while(num1<2||num1>5){ printf("请输入正确的数字: "); scanf("%d",&num1); } }else{ printf("\n1-上一页 2-下一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n"); scanf("%d",&num1); while(num1<1||num1>4){ printf("请输入正确的数字: "); scanf("%d",&num1); } } break; case 2: system("cls"); printf("学号 姓名 性别 成绩 等级 名次\n"); pf_num=1; while(p1&&pf_num<=10){ printf("%-6d%-10s",p1->xh,p1->xm); if(p1->xb==1) printf("男 "); else printf("女 "); printf("%-6d%-6c%-6d\n",p1->cj,p1->dj,p1->mc); pf_num++; p1=p1->next; } page_now++; if(page_now==page_sum){ printf("\n1-上一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n",page_now,page_sum); scanf("%d",&num1); while(num1<1||num1>5||num1==2){ printf("请输入正确的数字: "); scanf("%d",&num1); } }else{ printf("\n1-上一页 2-下一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n"); scanf("%d",&num1); while(num1<1||num1>5){ printf("请输入正确的数字: "); scanf("%d",&num1); } } break; case 3: system("cls"); p1=head; page_now=1; printf("学号 姓名 性别 成绩 等级 名次\n"); pf_num=1; while(p1&&pf_num<=10){ printf("%-6d%-10s",p1->xh,p1->xm); if(p1->xb==1) printf("男 "); else printf("女 "); printf("%-6d%-6c%-6d\n",p1->cj,p1->dj,p1->mc); pf_num++; p1=p1->next; } printf("\n 2-下一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n",page_now,page_sum); scanf("%d",&num1); while(num1<2||num1>5){ printf("请输入正确的数字: "); scanf("%d",&num1); } break; case 4: system("cls"); p1=head; page_now=page_sum; n=1; while(n<=(sum/10)*10){ p1=p1->next; n++; } printf("学号 姓名 性别 成绩 等级 名次\n"); pf_num=1; while(p1&&pf_num<=10){ printf("%-6d%-10s",p1->xh,p1->xm); if(p1->xb==1) printf("男 "); else printf("女 "); printf("%-6d%-6c%-6d\n",p1->cj,p1->dj,p1->mc); pf_num++; p1=p1->next; } printf("\n1-上一页 3-首页 4-尾页 5-返回上级\n"); printf(" 当前第%-2d页,共%-2d页\n",page_now,page_sum); scanf("%d",&num1); while(num1<1||num1>5||num1==2){ printf("请输入正确的数字: "); scanf("%d",&num1); } break; case 5: break; } if(num1==5) break; } }
|