您好,登錄后才能下訂單哦!
本文實例為大家分享了C語言版學生成績管理系統的具體代碼,供大家參考,具體內容如下
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h> #include<string.h> #include<algorithm> char buf[255]; char c=14; char path[]="D:\\data"; char tmp[]="D:\\tmp"; struct Student { char Name[20]; char No[20]; char Dept[20]; char ClassNo[20]; double Math,Chinese,English; double Ave; bool Pass; }students[500]; bool cmp1(Student s1,Student s2) { return s1.Chinese>s2.Chinese; } bool cmp2(Student s1,Student s2) { return s1.Math>s2.Math; } bool cmp3(Student s1,Student s2) { return s1.English>s2.English; } bool cmp4(Student s1,Student s2) { return s1.Ave>s2.Ave; } //---------------------------------------------------------void Sort(int cmd) /* 1 ChineseSort 2 MathSort 3 EnglishSort 4 AveSort */ int Sort(int cmd) { Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=0; while((n=fread(&stu,sizeof(stu),1,fp)>0)) { students[cnt++]=stu; } fclose(fp); switch(cmd) { case 1: std::sort(students,students+cnt,cmp1); break; case 2: std::sort(students,students+cnt,cmp2); break; case 3: std::sort(students,students+cnt,cmp3); break; case 4: std::sort(students,students+cnt,cmp4); break; default: exit(0); } return cnt; } //---------------------------------------------------------PrintTitle() void PrintTitle() { printf("%-8s%-12s%-12s%-8s%-7s%-7s%-7s%-7s%-s\n","姓名","班級","學號","系院","語文","數學","英語","平均分","及格"); } //---------------------------------------------------------Print(Student stu) void Print(Student stu) { printf("%-8s%-12s%-12s%-8s%-7.2lf%-7.2lf%-7.2lf%-7.2lf%-s\n",stu.Name,stu.ClassNo,stu.No,stu.Dept,stu.Chinese,stu.Math,stu.English,stu.Ave,stu.Pass?"是":"否"); } //---------------------------------------------------------PrintNo(char No[]) void PrintNo(char No[]) { Student stu; int n; bool find=0; FILE *fp=fopen(path,"ab+");//以ab+方式打開防止data文件不存在時發生錯誤 while((n=fread(&stu,sizeof(stu),1,fp))>0) { if(!strcmp(stu.No,No)) { find=1; break; } } fclose(fp); if(find) { PrintTitle(); Print(stu); } else puts("未找到!"); } //---------------------------------------------------------PrintClass(char ClassNo[]) void PrintClass(char ClassNo[]) { Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=0; while((n=fread(&stu,sizeof(stu),1,fp)>0)) { if(!strcmp(stu.ClassNo,ClassNo)) { if(cnt==0) PrintTitle(); Print(stu); cnt++; } } fclose(fp); if(cnt==0) puts("無數據"); else printf("共%d條\n",cnt); } //---------------------------------------------------------PrintDept(char Dept[]) void PrintDept(char Dept[]) { Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=0; while((n=fread(&stu,sizeof(stu),1,fp)>0)) { if(!strcmp(stu.Dept,Dept)) { if(cnt==0) PrintTitle(); Print(stu); cnt++; } } fclose(fp); if(cnt==0) puts("無數據"); else printf("共%d條\n",cnt); } //---------------------------------------------------------PrintAll() void PrintAll() { Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=0; while((n=fread(&stu,sizeof(stu),1,fp)>0)) { if(cnt==0) PrintTitle(); Print(stu); cnt++; } fclose(fp); if(cnt==0) puts("無數據"); else printf("共%d條\n",cnt); } //---------------------------------------------------------Input() Student Input() { Student stu; puts("請輸入姓名、班級、學號、系院:"); scanf("%s%s%s%s",&stu.Name,&stu.ClassNo,&stu.No,&stu.Dept); puts("請輸入各科成績:語文、數學、英語"); scanf("%lf%lf%lf",&stu.Chinese,&stu.Math,&stu.English); stu.Ave=(stu.Chinese+stu.Math+stu.English)/3; if(stu.Chinese>=60&&stu.Math>=60&&stu.English>=60) stu.Pass=1; else stu.Pass=0; return stu; } //---------------------------------------------------------Insert() void Insert() { int cmd; do{ Student stu=Input(); FILE *fp=fopen(path,"ab+"); fwrite(&stu,sizeof(stu),1,fp); fclose(fp); puts("錄入成功"); puts("繼續錄入嗎?[1]是[0]否"); scanf("%d",&cmd); }while(cmd==1); } //---------------------------------------------------------Del() void Del() { int cmd,cmd2; char No[20]; do{ puts("請輸入要刪除的學號:"); scanf("%s",&No); Student stu; FILE *fp=fopen(path,"ab+"); FILE *fp2=fopen(tmp,"wb"); int n,find=0; while((n=fread(&stu,sizeof(stu),1,fp))>0) { if(!strcmp(stu.No,No)) { find=1; Print(stu); puts("確定要刪除嗎?[1]確定[0]取消"); scanf("%d",&cmd2); if(cmd2==1) puts("已刪除"); else fwrite(&stu,sizeof(stu),1,fp2); } else fwrite(&stu,sizeof(stu),1,fp2); } fclose(fp); fclose(fp2); if(find==0) { printf("未找到學號%s\n",No); remove(tmp); } else { remove(path); rename(tmp,path); } puts("繼續刪除嗎?[1]是[0]否"); scanf("%d",&cmd); }while(cmd==1); } //---------------------------------------------------------Change() void Change() { int cmd; char No[20]; do{ puts("請輸入要更改的學號:"); scanf("%s",&No); Student stu; int find=0; FILE *fp=fopen(path,"ab+"); FILE *fp2=fopen(tmp,"wb"); int n; while((n=fread(&stu,sizeof(stu),1,fp))>0) { if(!strcmp(stu.No,No)) { find=1; stu=Input(); fwrite(&stu,sizeof(stu),1,fp2); } else fwrite(&stu,sizeof(stu),1,fp2); } fclose(fp); fclose(fp2); if(find) { remove(path); rename(tmp,path); puts("更改成功!"); } else { printf("未找到學號%s\n",No); remove(tmp); } puts("繼續更改嗎?[1]是[0]否"); scanf("%d",&cmd); }while(cmd==1); } //---------------------------------------------------------AboutCourse void Chinese() { double sum=0,ave; int pass=0,fail=0,better=0; Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=Sort(1); while((n=fread(&stu,sizeof(stu),1,fp))>0) { sum+=stu.Chinese; if(stu.Chinese>=60) { pass++; if(stu.Chinese>=80) better++; } else fail++; } ave=sum/cnt; puts("語文成績分析……"); printf("共有%d學生\n優秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt); printf("%-8s%-10s%-12s%-s\n","名次","姓名","學號","語文"); for(int i=0;i<cnt;i++) { printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Chinese); } printf("平均分:%.2lf\n",ave); } void Math() { double sum=0,ave; int pass=0,fail=0,better=0; Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=Sort(2); while((n=fread(&stu,sizeof(stu),1,fp))>0) { sum+=stu.Math; if(stu.Math>=60) { pass++; if(stu.Math>=80) better++; } else fail++; } ave=sum/cnt; puts("數學成績分析……"); printf("共有%d學生\n優秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt); printf("%-8s%-10s%-12s%-s\n","名次","姓名","學號","數學"); for(int i=0;i<cnt;i++) { printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Math); } printf("平均分:%.2lf\n",ave); } void English() { double sum=0,ave; int pass=0,fail=0,better=0; Student stu; FILE *fp=fopen(path,"ab+"); int n,cnt=Sort(3); while((n=fread(&stu,sizeof(stu),1,fp))>0) { sum+=stu.English; if(stu.English>=60) { pass++; if(stu.English>=80) better++; } else fail++; } ave=sum/cnt; puts("英語成績分析……"); printf("共有%d學生\n優秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt); printf("%-8s%-10s%-12s%-s\n","名次","姓名","學號","英語"); for(int i=0;i<cnt;i++) { printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].English); } printf("平均分:%.2lf\n",ave); } void Average() { Student stu; int n,cnt=Sort(4); double sum=0; FILE *fp=fopen(path,"ab+"); while((n=fread(&stu,sizeof(stu),1,fp))>0) { sum+=stu.Chinese; sum+=stu.Math; sum+=stu.English; } puts("平均成績分析……"); printf("%-8s%-10s%-12s%-s\n","名次","姓名","學號","平均成績"); for(int i=0;i<cnt;i++) { printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Ave); } printf("平均分:%.2lf\n",sum/cnt/3); } //---------------------------------------------------------Search() void Search() { int cmd,cmd2; while(1) { system("cls"); puts("[1] 按學號查詢"); puts("[2] 按班級查詢"); puts("[3] 輸出全部"); puts("[4] 按系別查詢"); puts("[5] 課程分析"); puts("[0] 返回主界面"); scanf("%d",&cmd); if(cmd==0) break; switch(cmd) { case 1: { char No[20]; puts("請輸入要查詢的學號:"); scanf("%s",&No); PrintNo(No); } break; case 2: { char ClassNo[20]; puts("請輸入要查詢班級號:"); scanf("%s",&ClassNo); PrintClass(ClassNo); } break; case 3: PrintAll(); break; case 4: { char Dept[20]; puts("請輸入要查詢的系別:"); scanf("%s",&Dept); PrintDept(Dept); } break; case 5: while(1) { puts("[1] 語文成績分析"); puts("[2] 數學成績分析"); puts("[3] 英語成績分析"); puts("[4] 平均成績分析"); puts("[0] 返回上一級"); scanf("%d",&cmd2); if(cmd2==0) break; switch(cmd2) { case 1: Chinese(); break; case 2: Math(); break; case 3: English(); break; case 4: Average(); break; default: puts("不合法指令!"); } } break; default: puts("不合法指令!"); } while(1) { puts("[0] 返回"); puts("[1] 錄入"); puts("[2] 刪除"); puts("[3] 修改"); scanf("%d",&cmd2); if(cmd2==0) break; switch(cmd2) { case 1: Insert(); break; case 2: Del(); break; case 3: Change(); break; default: puts("不合法指令!"); } } } } //---------------------------------------------------------Menu() void ShowMenu1() { int i; puts(""); printf(" "); for(i=0;i<14;i++) putchar(c); printf("成績管理分析系統"); for(i=0;i<14;i++) putchar(c); puts(""); printf(" %c %c\n",c,c); printf(" %c 請輸入指令 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [0] 退出 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [1] 錄入 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [2] 刪除 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [3] 查詢 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [4] 更改 %c\n",c,c); printf(" %c %c\n",c,c); printf(" "); for(i=0;i<44;i++) putchar(c); puts(""); } void ShowMenu2() { int i; puts(""); printf(" "); for(i=0;i<14;i++) putchar(c); printf("成績管理分析系統"); for(i=0;i<14;i++) putchar(c); puts(""); printf(" %c %c\n",c,c); printf(" %c 請輸入指令 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [0] 退出 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [1] 錄入 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [2] 刪除 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [3] 查詢 %c\n",c,c); printf(" %c %c\n",c,c); printf(" %c [4] 更改 %c\n",c,c); printf(" %c %c\n",c,c); printf(" "); for(i=0;i<44;i++) putchar(c); puts(""); } //---------------------------------------------------------void RandColor() void RandColor() { int a,b; char param[20]="color ",param2[10]; do{ a=rand()%16; b=rand()%16; }while(a==b); sprintf(param2,"%x%x",a,b); strcat(param,param2); system(param); } //---------------------------------------------------------main() int main() { int cmd; srand(time(NULL)); for(int i=0;i<8;i++) { RandColor(); system("cls"); ShowMenu1(); system("cls"); ShowMenu2(); } system("color 0a"); while(1) { system("cls"); ShowMenu1(); printf("[ ]\b\b"); scanf("%d",&cmd); if(cmd==0) break; switch(cmd) { case 1: Insert(); break; case 2: Del(); break; case 3: Search(); break; case 4: Change(); break; default: puts("命令不合法!"); } } return 0; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。