• 智能AI问答!更聪明、更完整、原创作!
  • 一款基于GPT的超级AI机器人,可以回答问题、聊天、创作、写文案!

如何使用C语言设计学生学业成绩管理系统?

发布于 2025-05-06 02:26:52

我有一个功能基本上和这差不多。我贴给你吧。#include#include#include#include#includestruct student{char name[21];char number[21];char specialty[21];char sex[3];int c_score;int english_score;int math_score;int total_score;};typedef struct student student;student studentArray[100];char putout[9][21]={"姓名","学号","专业","性别","c语言","英语","数学","总成绩","名次"}; //为格式化输出做准备int count=0; //用来记录学生的记录个数//函数声明部分void initialStudent();void addStudent();int judge(int );void delstudent();void modifyStudent();void searchStudent();void studentProfile();void getaveragescore(int [],int ,double );void setOrder(int );void selectSort(char [][21],int [],int ,int ,int ,char [][21]);void printUnpassed();void printAll();int menuselect();void clear();//创建e:\student.txt文件,并初始化,同时计算出文件中含有的记录总数void initial(){FILE fp;char choice=''y'';int i=0;fp=fopen("e:\student.txt","r");if(!fp){printf("创建文件失败,即将返回");return ;}for(;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++);count=i;}//添加学生信息void addStudent(){FILE fp;char choice=''y'';int i=0;//int sum;fp=fopen("e:\student.txt","ab");if(!fp){printf("创建文件失败,即将返回");return ;}while(choice==''y''){printf("输入学生姓名,学号,专业,性别,中间以空格符隔开");fflush(stdin);scanf("%s%s%s%s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty,studentArray[i].sex);printf("输入学生c语言分数,英语分数,数学分数");scanf("%d%d%d",&studentArray[i].c_score,&studentArray[i].english_score,&studentArray[i].math_score);//sum=judge(studentArray[i].c_score)+judge(studentArray[i].english_score)+judge(studentArray[i].math_score);while(!judge(studentArray[i].c_score)||!judge(studentArray[i].english_score)||!judge(studentArray[i].math_score)){printf("成绩输入错误,请重新输入");scanf("%d%d%d",&studentArray[i].c_score,&studentArray[i].english_score,&studentArray[i].math_score);//sum=judge(studentArray[i].c_score)+judge(studentArray[i].english_score)+judge(studentArray[i].math_score);}studentArray[i].total_score=studentArray[i].c_score+studentArray[i].english_score+studentArray[i].math_score;fwrite(&studentArray[i],sizeof(struct student),1,fp); //将数据写入文件中i++;count++;printf("是否继续输入,继续输入输入y或者Y,其它值退出");fflush(stdin);choice=getchar();choice=tolower(choice);fflush(stdin); //清空缓存输入流}fclose(fp);printf("输入任意键继续");getch();//return ;}//判断输入成绩是否合法int judge(int score){if(score<0||score>100)return 0;else if(score<60) //只是为了计算后面的不及格学生相关信息return 4;elsereturn 1;}//删除学生信息void delStudent(){FILE fp,fp2;int i;char choice;int flag1=1;int flag2=0;int found=0; //用来标记文件中是否含有此人char studentName[21];char studentNumber[21];fp=fopen("e:\student.txt","rb");if(count==0){printf("文件中没有信息,即将退出");//getch();return ;}fp2=fopen("e:\ est.txt","wb");if(!fp2){printf("文件创建失败,即将退出");return;}i=0;found=0;printf("按照姓名删除请输入0,按照学号删除请输入1");scanf("%d",&flag2);fflush(stdin);if(flag2==0){printf("输入要删除的学生的姓名,按回车键结束");scanf("%s",studentName);for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++) //将其他人信息转移到test.txt文件中if(strcmp(studentArray[i].name,studentName))fwrite(&studentArray[i],sizeof(struct student),1,fp2);else{found=1;}fclose(fp);fclose(fp2);fflush(stdin);if(found==1)printf("是否确认删除姓名为%s该学生信息,确认删除,输入y或者Y:",studentName);else{printf("没有此人信息,即将退出");return ;}choice=getchar();if(choice==''y''||choice==''Y'') //将文件结构指针重新定向{count--;fp=fopen("e:\student.txt","wb");fp2=fopen("e:\ est.txt","rb");for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp2)!=0;i++)fwrite(&studentArray[i],sizeof(struct student),1,fp);fclose(fp);fclose(fp2);}}else if(flag2==1){printf("输入要删除的学生的学号,按回车键结束");scanf("%s",studentNumber);for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++) //将其他人信息转移到test.txt文件中if(strcmp(studentArray[i].number,studentNumber))fwrite(&studentArray[i],sizeof(struct student),1,fp2);elsefound=1;fclose(fp);fclose(fp2);fflush(stdin);if(found==1)printf("是否确认删除学号为%s的该学生信息,确认删除,输入y或者Y:",studentNumber);else{printf("没有此人信息,即将退出");return;}choice=getchar();if(choice==''y''||choice==''Y'') //将文件结构指针重新定向{count--;fp=fopen("e:\student.txt","wb");fp2=fopen("e:\ est.txt","rb");for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp2)!=0;i++)fwrite(&studentArray[i],sizeof(struct student),1,fp);fclose(fp);fclose(fp2);}}elseprintf("输入错误");}//修改学生信息void modifyStudent(){FILE fp,fp2;int flag=0;int i;char studentName[21];student newdata;fp=fopen("e:\student.txt","rb");if(count==0){printf("文件中还没有学生信息,即将退出");return ;}fp2=fopen("e:\ est.txt","wb");if(!fp2){printf("文件创建失败,即将返回");return ;}printf("输入要修改的学生的姓名");fflush(stdin);scanf("%s",studentName);for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++){if(!strcmp(studentArray[i].name,studentName)){flag=1;printf("输入要修改后学生的姓名,学号,专业,性别");fflush(stdin);scanf("%s%s%s%s",newdata.name,newdata.number,newdata.specialty,newdata.sex);printf("输入修改后的学生的c语言分数,英语分数,数学分数");scanf("%d%d%d",&newdata.c_score,&newdata.english_score,&newdata.math_score);while(!judge(studentArray[i].c_score)||!judge(studentArray[i].english_score)||!judge(studentArray[i].math_score)){printf("输入不合法,请重新输入");scanf("%d%d%d",&newdata.c_score,&newdata.english_score,&newdata.math_score);}newdata.total_score=newdata.c_score+newdata.english_score+newdata.math_score;fwrite(&newdata,sizeof(struct student),1,fp2);}elsefwrite(&studentArray[i],sizeof(struct student),1,fp2);}if(flag==0){printf("没有此人,即将退出");return ;}fclose(fp);fclose(fp2);fp=fopen("e:\student.txt","wb");fp2=fopen("e:\ est.txt","rb");for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp2)!=0;i++)fwrite(&studentArray[i],sizeof(struct student),1,fp);printf("");fclose(fp);fclose(fp2);}//查找某学生信息void searchStudent(){FILE fp;char studentName[21];int flag=0;int i;fp=fopen("e:\student.txt","rb");if(count==0){printf("文件中还没有学生记录,即将退出");return ;}printf("输入你要查找的学生姓名");fflush(stdin);scanf("%s",studentName);for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++){if(strcmp(studentName,studentArray[i].name)==0){flag=1;printf("要查找的学生相关信息如下:");printf("%-10s%-15s%-20s%-5s",putout[0],putout[1],putout[2],putout[3]);printf("%-6s%-6s%-6s%-6s", putout[4],putout[5],putout[6],putout[7]);printf("%-10s%-15s%-20s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty);printf("%-5s",studentArray[i].sex);printf("%-6d%-6d",studentArray[i].c_score,studentArray[i].english_score);printf("%-6d%-6d",studentArray[i].math_score,studentArray[i].total_score);printf("");printf("");}}if(flag==0){printf("没有找到姓名为%s的学生",studentName);fflush(stdout);printf("");return ;}}//学生信息总体概况,如平均分,总人数void studentProfile(){FILE fp;int i;int cArray[100]; //用来存储c语言分数double cAverage; //用来存储c语言平均成绩int englishArray[100];double englishAverage;int mathArray[100];double mathAverage;int totalArray[100];double totalAverage;fp=fopen("e:\student.txt","rb");if(count==0){printf("没有学生信息,即将退出");return ;}for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++){cArray[i]=studentArray[i].c_score;englishArray[i]=studentArray[i].english_score;mathArray[i]=studentArray[i].math_score;totalArray[i]=studentArray[i].total_score;}printf("共有%d个学生信息",count);getaveragescore(cArray,i,&cAverage);getaveragescore(englishArray,i,&englishAverage);getaveragescore(mathArray,i,&mathAverage);getaveragescore(totalArray,i,&totalAverage);printf("c语言平均成绩为%.2f分",cAverage);printf("英语平均成绩为%.2f分",englishAverage);printf("数学平均成绩为%.2f分",mathAverage);printf("总分平均成绩为%.2f分",totalAverage);printf("");}//统计平均分void getaveragescore(int arrayscore[],int n,double averagescore){double sum=0;int i=0;for(;isum+=arrayscore[i];averagescore=sum/n;}//有序输出学生的成绩void setOrder(int flag) //flag表示选择成绩是从低到高还是从高到底排序,0从低到高,1则反之{FILE fp;int i,j;student temp;int choice;//flag=0;fp=fopen("e:\student.txt","rb");if(count==0){printf("没有学生信息,即将退出");return ;}for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++); //将数据读入结构数组中for(choice =1;choice<5;choice++)switch(choice){case 1:printf("c语言成绩:");for(i=0;ifor(j=i+1;jif(studentArray[i].c_score>studentArray[j].c_score){temp=studentArray[i];studentArray[i]=studentArray[j];studentArray[j]=temp;}if(!flag){printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[4]);for(i=0;iprintf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].c_score);}else{printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[4]);for(i=count-1;i>=0;i--)printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].c_score);}break;case 2:printf("英语成绩:");for(i=0;ifor(j=i+1;jif(studentArray[i].english_score>studentArray[j].english_score){temp=studentArray[i];studentArray[i]=studentArray[j];studentArray[j]=temp;}if(!flag){printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[5]);for(i=0;iprintf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].english_score);}else{printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[5]);for(i=count-1;i>=0;i--)printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].english_score);}break;case 3:printf("数学成绩:");for(i=0;ifor(j=i+1;jif(studentArray[i].math_score>studentArray[j].math_score){temp=studentArray[i];studentArray[i]=studentArray[j];studentArray[j]=temp;}if(!flag){printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[6]);for(i=0;iprintf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].math_score);}else{printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[6]);for(i=count-1;i>=0;i--)printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].math_score);}break;case 4:printf("总成绩:");for(i=0;ifor(j=i+1;jif(studentArray[i].total_score>studentArray[j].total_score){temp=studentArray[i];studentArray[i]=studentArray[j];studentArray[j]=temp;}if(!flag){printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[7]);for(i=0;iprintf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].total_score);}else{printf("%-10s%-10s%-15s%-10s",putout[8],putout[0],putout[1],putout[7]);for(i=count-1;i>=0;i--)printf("%-10d%-10s%-15s%-10d",count-i,studentArray[i].name,studentArray[i].number,studentArray[i].total_score);}break;return ;}}//输出不及格学生的相关信息void printUnpassed(){FILE fp;int i;int total=0;fp=fopen("e:\student.txt","rb");if(count==0){printf("没有学生信息,即将退出");return ;}printf("不及格学生相关信息如下所示");printf("%-10s%-15s%-20s%-5s",putout[0],putout[1],putout[2],putout[3]);printf("%-6s%-6s%-6s%-6s", putout[4],putout[5],putout[6],putout[7]);for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++)if(judge(studentArray[i].c_score)+judge(studentArray[i].english_score)+judge(studentArray[i].math_score)>3){printf("%-10s%-15s%-20s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty);printf("%-5s",studentArray[i].sex);printf("%-6d%-6d",studentArray[i].c_score,studentArray[i].english_score);printf("%-6d%-6d",studentArray[i].math_score,studentArray[i].total_score);printf("");total++;}printf("不及格学生人数共有%d人",total);return ;}//输出文件中所有学生信息记录void printAll(){FILE fp;int i;//student buffer;fp=fopen("e:\student.txt","rb");if(count==0){printf("现在还没有学生记录,即将退出");printf("");return ;}printf("该文件中一共有%d个学生记录,记录如下:",count);printf("%-10s%-15s%-20s%-5s",putout[0],putout[1],putout[2],putout[3]);printf("%-6s%-6s%-6s%-6s", putout[4],putout[5],putout[6],putout[7]);for(i=0;fread(&studentArray[i],sizeof(struct student),1,fp)!=0;i++){printf("%-10s%-15s%-20s",studentArray[i].name,studentArray[i].number,studentArray[i].specialty);printf("%-5s",studentArray[i].sex);printf("%-6d%-6d",studentArray[i].c_score,studentArray[i].english_score);printf("%-6d%-6d",studentArray[i].math_score,studentArray[i].total_score);printf("");}}void clear(){FILE fp;char ch;printf("你确认删除所有学生记录吗?继续输入y活着Y");fflush(stdin);ch=getchar();if(ch==''y''||ch==''Y''){fp=fopen("e:\student.txt","wb");count=0;}return ;}//主函数int main(){int choice;printf("****欢迎进入学生信息管理系统*****");initial();while(1)switch(choice=menuselect()){case 0:printf("***欢迎下次再次使用****");exit(0);case 1:addStudent();break;case 2:delStudent();break;case 3:modifyStudent();break;case 4:searchStudent();break;case 5:studentProfile();break;case 6:setOrder(0);break;case 7:setOrder(1);break;case 8:printUnpassed();break;case 9:printAll();break;case 10:system("CLS");//clrscr();break;case 11:clear();break;}return 0;}//菜单函数int menuselect(){int choice;do{printf("0.退出管理系统");printf("1.添加学生信息记录");printf("2.删除某一指定学生记录");printf("3.修改某一给定学生信息");printf("4.查找某一给定学生信息");printf("5.输出学生信息总体概况");printf("6.按照分数从低到高输出学生相关信息");printf("7.按照分数从高到低输出学生相关信息");printf("8.输出不及格学生的相关信息");printf("9.输出文件中所有学生的记录");printf("10.清屏");printf("11.删除所有学生记录");printf("输入你的选择:");scanf("%d",&choice);}while(choice<0||choice>11);return choice;}

0 条评论

发布
问题

在线
客服