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

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

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define N 10
typedef struct student
{
  long studentID;
  char studentName[100];
  char studentSex[100];
  char studentClass[100];
  int score;	
	
}STUDENT; 
int sum1(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");
    return 0;
}
int high1(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");
return 0;	
}
int low1(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");
return 0;	
}
int chaxun1(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");
	return 0;
}
int shanchu1(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");
 
	return 0; 
}
int charu1(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");
  
  return 0;
}
int main()
{  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)
		{
			case 1:(*sum)(stu,n);break;
			case 2:(*high)(stu,n);break;
			case 3:(*low)(stu,n);break;
			case 4:(*chaxun)(stu,n);break;
			case 5:(*shanchu)(stu,&n);break;
			case 6:(*charu)(stu,&n);break;
			case 7: {
			(*sum)(stu,n);
			(*high)(stu,n);
			(*low)(stu,n);
			(*chaxun)(stu,n);
			(*shanchu)(stu,&n);
			(*charu)(stu,&n);
				break;
			} 
			case 8:break;
		}
		if(k==8)break;
		printf("请输入要执行的数字:");
		scanf("%d",&k); 
        }	
  fclose(fp);
  
return 0;
} 

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐