0


C语言实现学生成绩管理系统

C语言实现学生成绩管理系统
要求如下:
1、录入记录数据,建立一张数据表,并将数据表写入文件保存;
2、对已经保存在外存中的文件,根据系统内容设计一个计算功能,如计算总和,或平均值等,将结果显示在屏幕上;
3、对已经保存在外存中的文件,根据系统内容设计排序功能,既可以从小到大,又可以从大到小排序,将排序前后的数据表显示在屏幕上;
4、对已经保存在外存中的文件,根据系统内容设计查询功能,查询结果显示在屏幕上;
5、对已经保存在外存中的文件,进行数据记录的删除操作,重新写文件更新;
6、对已经保存在外存中的文件,进行数据记录的插入操作,重新写文件更新;
7、读入文件内容,将数据表输出在屏幕上。

程序需从D盘读出数据,需要在D盘根目录下存入如下格式数据且命名为student:
在这里插入图片描述
具体代码如下:

#include<stdio.h>#include<ctype.h>#include<string.h>#define N 10typedefstruct student
{long studentID;char studentName[100];char studentSex[100];char studentClass[100];int score;}STUDENT;intsum1(STUDENT stu[],int n){int i,j,sum=0;for(i=0;i<n;i++)
    sum+=stu[i].score;printf("学科成绩总分及平均分:%d,%d\n",sum,sum/n);printf("\n");return0;}inthigh1(STUDENT stu[],int n){int i,j;long t;char c[100],x[100];printf("成绩由高到低排序\n");for(i=0;i<n-1;i++)for(j=i+1;j<n;j++)if(stu[j].score>stu[i].score){ t=stu[i].studentID;stu[i].studentID=stu[j].studentID;stu[j].studentID=t;
         t=stu[i].score;stu[i].score=stu[j].score;stu[j].score=t;strcpy(x,stu[i].studentName);strcpy(stu[i].studentName,stu[j].studentName);strcpy(stu[j].studentName,x);strcpy(c,stu[i].studentSex);strcpy(stu[i].studentSex,stu[j].studentSex);strcpy(stu[j].studentSex,c);strcpy(c,stu[i].studentClass);strcpy(stu[i].studentClass,stu[j].studentClass);strcpy(stu[j].studentClass,c);}for(i=0;i<n;i++){printf("%d ",stu[i].studentID);printf("%s ",stu[i].studentName);printf("%s ",stu[i].studentSex);printf("%s ",stu[i].studentClass);printf("%d ",stu[i].score);printf("\n");}printf("\n");return0;}intlow1(STUDENT stu[],int n){int i,j;long t;char c[100],x[100];printf("成绩由低到高排序(格式:学号,成绩)\n");for(i=0;i<n-1;i++)for(j=i+1;j<n;j++)if(stu[j].score<stu[i].score){ t=stu[i].studentID;stu[i].studentID=stu[j].studentID;stu[j].studentID=t;
         t=stu[i].score;stu[i].score=stu[j].score;stu[j].score=t;strcpy(x,stu[i].studentName);strcpy(stu[i].studentName,stu[j].studentName);strcpy(stu[j].studentName,x);strcpy(c,stu[i].studentSex);strcpy(stu[i].studentSex,stu[j].studentSex);strcpy(stu[j].studentSex,c);strcpy(c,stu[i].studentClass);strcpy(stu[i].studentClass,stu[j].studentClass);strcpy(stu[j].studentClass,c);}for(i=0;i<n;i++){printf("%d ",stu[i].studentID);printf("%s ",stu[i].studentName);printf("%s ",stu[i].studentSex);printf("%s ",stu[i].studentClass);printf("%d ",stu[i].score);printf("\n");}printf("\n");return0;}intchaxun1(STUDENT stu[],int n){int i,j,z,t=0;do{printf("请输入查询的学号:");scanf("%d",&z);
    t=0;for(i=0;i<n;i++)if(z==stu[i].studentID){ t=1;printf("%d ",stu[i].studentID);printf("%s ",stu[i].studentName);printf("%s ",stu[i].studentSex);printf("%s ",stu[i].studentClass);printf("%d ",stu[i].score);break;}if(t==0)printf("无此学号,请重新输入\n");}while(i==n);printf("\n\n");return0;}intshanchu1(STUDENT stu[],int*n){int z,i,j,t,q=*n;char c[20],x[20];
      FILE *fp;
     
      fp=fopen("D:\\student(s1).txt","wb+");for(i=0;i<*n-1;i++)for(j=i+1;j<*n;j++)if(stu[j].studentID<stu[i].studentID){ t=stu[i].studentID;stu[i].studentID=stu[j].studentID;stu[j].studentID=t;
         t=stu[i].score;stu[i].score=stu[j].score;stu[j].score=t;strcpy(x,stu[i].studentName);strcpy(stu[i].studentName,stu[j].studentName);strcpy(stu[j].studentName,x);strcpy(c,stu[i].studentSex);strcpy(stu[i].studentSex,stu[j].studentSex);strcpy(stu[j].studentSex,c);strcpy(c,stu[i].studentClass);strcpy(stu[i].studentClass,stu[j].studentClass);strcpy(stu[j].studentClass,c);}do{printf("请输入要删除的学号:");scanf("%d",&z);for(i=0;i<*n;i++)if(z==stu[i].studentID)break;}while(i==*n);for(j=i;j<*n-1;j++){
         stu[j].studentID=stu[j+1].studentID;strcpy(stu[j].studentName,stu[j+1].studentName);strcpy(stu[j].studentSex,stu[j+1].studentSex);
        stu[j].score=stu[j+1].score;strcpy(stu[j].studentClass,stu[j+1].studentClass);}*n=*n-1;printf("删除后为:\n");for(i=0;i<*n;i++){printf("%d ",stu[i].studentID);printf("%s ",stu[i].studentName);printf("%s ",stu[i].studentSex);printf("%s ",stu[i].studentClass);printf("%d ",stu[i].score);printf("\n");}fprintf(fp,"%d\n",*n);for(i=0;i<*n;i++){fprintf(fp,"%d ",stu[i].studentID);fprintf(fp,"%s ",stu[i].studentName);fprintf(fp,"%s ",stu[i].studentSex);fprintf(fp,"%s ",stu[i].studentClass);fprintf(fp,"%d\n",stu[i].score);printf("\n");}*n=q;fclose(fp);
  FILE *fq;
  fq=fopen("D:\\student.txt","rb+");fscanf(fq,"%d",&t);for(i=0;i<t;i++){fscanf(fq,"%ld",&stu[i].studentID);fscanf(fq,"%s",stu[i].studentName);fscanf(fq,"%s",stu[i].studentSex);fscanf(fq,"%s",stu[i].studentClass);fscanf(fq,"%d",&stu[i].score);}fclose(fq);printf("已保存至D盘根目录student(s1).txt\n");return0;}intcharu1(STUDENT stu[],int*n){int z,i,j,t;char c[20],x[20];
  
  FILE *fp;
  fp=fopen("D:\\student(s2).txt","wb+");printf("请输入插入的位置(数字 比如2):");scanf("%d",&z);for(j=*n;j>z;j--){     stu[j].studentID=stu[j-1].studentID;strcpy(stu[j].studentName,stu[j-1].studentName);strcpy(stu[j].studentSex,stu[j-1].studentSex);strcpy(stu[j].studentClass,stu[j-1].studentClass);
        stu[j].score=stu[j-1].score;}printf("请输入学号:");scanf("%d",&stu[z].studentID);printf("请输入姓名:");scanf("%s",stu[z].studentName);printf("请输入性别:");scanf("%s",stu[z].studentSex);printf("请输入班级:");scanf("%s",stu[z].studentClass);printf("请输入分数:");scanf("%d",&stu[z].score);*n=*n+1;printf("插入后为:\n");for(i=0;i<*n;i++){printf("%d ",stu[i].studentID);printf("%s ",stu[i].studentName);printf("%s ",stu[i].studentSex);printf("%s ",stu[i].studentClass);printf("%d ",stu[i].score);printf("\n");}fprintf(fp,"%d\n",*n);for(i=0;i<*n;i++){fprintf(fp,"%d ",stu[i].studentID);fprintf(fp,"%s ",stu[i].studentName);fprintf(fp,"%s ",stu[i].studentSex);fprintf(fp,"%d ",stu[i].studentClass);fprintf(fp,"%d\n",stu[i].score);printf("\n");}printf("已保存至D盘根目录student(s2).txt\n\n");return0;}intmain(){printf("学生成绩管理系统\n");
  FILE *fp;
  fp=fopen("D:\\student.txt","rb+");
  STUDENT stu[N],stu1[N];int n,m,i,j,k,s=0,order,t;fscanf(fp,"%d",&n);for(i=0;i<n;i++){fscanf(fp,"%ld",&stu[i].studentID);fscanf(fp,"%s",stu[i].studentName);fscanf(fp,"%s",stu[i].studentSex);fscanf(fp,"%s",stu[i].studentClass);fscanf(fp,"%d",&stu[i].score);}printf("文本数据为:\n");for(i=0;i<n;i++){printf("%d ",stu[i].studentID);printf("%s ",stu[i].studentName);printf("%s ",stu[i].studentSex);printf("%s ",stu[i].studentClass);printf("%d ",stu[i].score);printf("\n");}printf("\n");int(*sum)(STUDENT stu[],int n);int(*low)(STUDENT stu[],int n);int(*high)(STUDENT stu[],int n);int(*chaxun)(STUDENT stu[],int n);int(*shanchu)(STUDENT stu[],int*n);int(*charu)(STUDENT stu[],int*n);
  
  sum=sum1;high=high1;low=low1;chaxun=chaxun1;shanchu=shanchu1;charu=charu1;printf("输入“1”计算总分和平均分\n");printf("输入“2”给出成绩降序表\n");printf("输入“3”给出成绩升序表\n");printf("输入“4”按学号查询学生排名及成绩\n");printf("输入“5”删除相应学号数据\n");printf("输入“6”插入相应学号数据\n");printf("输入“7”计算所有\n");printf("输入“8”结束程序\n");printf("---------------------1 --------------------\n");printf("请输入:");scanf("%d",&k);printf("\n");while(1){switch(k){case1:(*sum)(stu,n);break;case2:(*high)(stu,n);break;case3:(*low)(stu,n);break;case4:(*chaxun)(stu,n);break;case5:(*shanchu)(stu,&n);break;case6:(*charu)(stu,&n);break;case7:{(*sum)(stu,n);(*high)(stu,n);(*low)(stu,n);(*chaxun)(stu,n);(*shanchu)(stu,&n);(*charu)(stu,&n);break;}case8:break;}if(k==8)break;printf("请输入要执行的数字:");scanf("%d",&k);}fclose(fp);return0;}

程序结果如下
在这里插入图片描述

标签: c算法 算法

本文转载自: https://blog.csdn.net/waterteam/article/details/111571956
版权归原作者 waterteam 所有, 如有侵权,请联系我们删除。

“C语言实现学生成绩管理系统”的评论:

还没有评论