欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用C语言写的炒鸡简单的学生成绩管理系统

程序员文章站 2022-03-09 08:53:00
...

系作者原创,转载请注明出处:https://blog.csdn.net/coder_what/article/details/82683619

老师给了个课程设计,要写一个简单的学生成绩管理系统,当时由于种种原因再加上脑子短路就只用了文件而没有用链表和指针,想想都是泪啊呜呜呜。

具体的思路和功能就是

用C语言写的炒鸡简单的学生成绩管理系统

这个也没什么好说的就是完全去堆代码qaq

不过有几个地方要注意:因为有一个追加录入,所以文件stu_a中必须在之前有数据,这里已经在文件中提前在第一行输入了一个目录:

number(10字节)name(10字节)yuwen(6字节)shuxue(6字节)yingyu(6字节)

之后才能用这些功能。

还有一点要注意的是,其余文件都要从stu_a这个文件中读出目录,此时需要多次用到fscanf,有时还有用到fseek去改变文件指针的位置

最后要说的是,每个函数打开文件后,一定要记得fclose!!!

源文件在这*****

//头文件 
#include<stdio.h>
#include<stdlib.h>
//宏定义 
#define N 60
//结构体
typedef struct
{
	long int num;
	char name[10];
	float score[3];
	float aver;
	float total; 
 } Stu;
typedef struct//处理目录问题 
{
	char a[10];
	char b[10];
	char c[6];
	char d[6];
	char e[6];
	char f[6];
	char g[6];
}Temp; 
//全局变量
int n,i;
FILE *fp;
Stu stu[N];
Temp temp;
//函数声明 
int model();
void inp();//输入 
void inp_a();
void inp_o(); 
void inq();//查询 
void inq_all();
void inq_num(); 
void cha();//更改 
void sor();//排序 
void sor_num();
void sor_aver(); 
//主函数 
int main()
{
	while(1)
	{
		model();
		switch(n)
		{
			case 1:inp();break;
			case 2:inq();break;
			case 3:cha();break;
			case 4:sor();break;
			case 5:exit(0);
		}
		system("cls");
	}
	
	return 0; 
 } 
//自定义函数
int model()
{
	printf("\n\t\t\t------------Student Achievement Management System------------");
	printf("\n\n\n\t\t\t1. Input Marks ");
	printf("\n\t\t\t2. Inquire Marks");
	printf("\n\t\t\t3. Change Marks");
	printf("\n\t\t\t4. Sorting");
	printf("\n\t\t\t5. Exit");
	printf("\n\n\t\t\t------------Student Achievement Management System------------"); 
	printf("\n\n\t\t\tPlease input the order number :");
	scanf("%d",&n);	
	return n;
 } 
void inq()
{
	system("cls");
	printf("\n\t\t\t------------Student Achievement Management System------------");
	printf("\n\n\n\t\t\t1. Inquire All Marks ");
	printf("\n\t\t\t2. Inquire Special Number Marks");
	printf("\n\t\t\t3. Exit");
	printf("\n\n\t\t\t------------Student Achievement Management System------------"); 
	printf("\n\n\t\t\tPlease input the order number :");
	scanf("%d",&n);	
	switch(n)
	{
		case 1:inq_all();break;
		case 2:inq_num();break;
		case 3:return;
	}
}
void inq_all()
{
	Stu tem;
	if((fp=fopen("stu_a.txt","r"))==NULL)
	{
		printf("\t\tWe cant't find the file, please creat a file pressing the first order number!");
		getchar();
		getchar();
		return;
	}
	system("cls");
	fscanf(fp,"%s%s%s%s%s%s%s",temp.a,
						   temp.b,
						   temp.c,
						   temp.d,
						   temp.e,
						   temp.f,
						   temp.g);
	printf("\t\t%10s%10s%8s%8s%8s%8s%8s",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	printf("\n\n");
	while(!feof(fp))
	{
		fscanf(fp,"%d%s%f%f%f%f%f\n",&tem.num,
						      	  tem.name,
						      	 &tem.score[0],
						      	 &tem.score[1],
						     	 &tem.score[2],
							 	 &tem.aver,
								  &tem.total);
		printf("\t\t%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",tem.num,
						   					  	   tem.name,
						   					  	   tem.score[0],
						  					 	   tem.score[1],
						 			    	       tem.score[2],
												   tem.aver,
												   tem.total);	
	}
	fclose(fp);
	getchar();
	getchar();
}
void inq_num()
{
	long int num;
	Stu tem;
	i=0;
	printf("\n\t\tPlease input the number you wanna inquire:");
	scanf("%d",&num);
	if((fp=fopen("stu_a.txt","r"))==NULL)
	{
		printf("\t\tWe cant't find the file, please creat a file pressing the first order number!");
		getchar();
		getchar();
		return ;
	}
	fscanf(fp,"%s%s%s%s%s%s%s",temp.a,
						   temp.b,
						   temp.c,
						   temp.d,
						   temp.e,
						   temp.f,
						   temp.g);
	while(!feof(fp))
	{
		fscanf(fp,"%d%s%f%f%f%f%f\n",&tem.num,
						      	  tem.name,
						      	 &tem.score[0],
						      	 &tem.score[1],
						     	 &tem.score[2],
							 	 &tem.aver,
								  &tem.total);
		if(tem.num==num)
		{
			i++;
			break;
		}
	}
	if(i==0)
	{
		system("cls");
		printf("\n\n\n\t\t\tWe can't find your number, please return and input again pressing anykey");
		getchar();
		getchar();
		return ;
	}
	system("cls");
	printf("\t\t%10s%10s%8s%8s%8s%8s%8s",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	printf("\n\n");
	printf("\t\t%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",tem.num,
						   					  	   tem.name,
						   					  	   tem.score[0],
						  					 	   tem.score[1],
						 			    	       tem.score[2],
												   tem.aver,
												   tem.total);	
	
	fclose(fp);
	getchar();
	getchar();	
}
void cha()
{
	long int num;
	int j,k;
	float a,b;
	FILE * ft;
	i=0;k=0;
	printf("Please input your number, the order of score(you just can input 1(yuwen),2(shuxue),3(yingyu)) and processed score:");
	printf("%\n\t\t");
	scanf("%d%f%f",&num,&a,&b);
	fp=fopen("stu_a.txt","r");
	if(fp==NULL)
	{
		printf("\t\t\tWe cannot find the file");
		return;
	}
	ft=fopen("stu_change.txt","w");
	if(ft==NULL)
	{
		printf("we cannot find the file");
		return;
	}
	system("cls");
	printf("\n");
	fscanf(fp,"%s%s%s%s%s%s%s",temp.a,
						   temp.b,
						   temp.c,
						   temp.d,
						   temp.e,
						   temp.f,
						   temp.g);
	while(1)
	{
		j=fscanf(fp,"%d%s%f%f%f%f%f",&stu[i].num,
						      	  stu[i].name,
						      	 &stu[i].score[0],
						      	 &stu[i].score[1],
						     	 &stu[i].score[2],
							 	 &stu[i].aver,
								  &stu[i].total);
		if(j==-1)break;
		if(num==stu[i].num)k=1;
		i++;
	}
	if(k!=1)//输入错误提示 
	{
		printf("\n\t\tYour number or order is error, please return and input again:");
		getchar();
		getchar();
		return;
	}
	fseek(fp,60L,0);
	printf("\t\t%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	fprintf(ft,"%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	i=0;
	while(1)
	{
		j=fscanf(fp,"%d%s%f%f%f%f%f",&stu[i].num,
						      	  stu[i].name,
						      	 &stu[i].score[0],
						      	 &stu[i].score[1],
						     	 &stu[i].score[2],
							 	 &stu[i].aver,
								  &stu[i].total);
		if(j==-1)break;
		if(num==stu[i].num)
		{
			if(a==1)
				stu[i].score[0]=b;
			if(2==a)
				stu[i].score[1]=b;
			if(3==a)
				stu[i].score[2]=b;
		}
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
		stu[i].total=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
		fprintf(ft,"%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);
		printf("\t\t%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);							
		i++;
	}
	fclose(ft);
	fclose(fp);
	printf("\n\n");
	printf("\t\t\tyour score has been changed");
	getchar();
	getchar();

}
void sor()
{
	system("cls");
	printf("\n\t\t\t------------Student Achievement Management System------------");
	printf("\n\n\n\t\t\t1. Average Marks Sorting");
	printf("\n\t\t\t2. Number Marks Sorting");
	printf("\n\t\t\t3. Exit");
	printf("\n\n\t\t\t------------Student Achievement Management System------------"); 
	printf("\n\n\t\t\tPlease input the order number :");
	scanf("%d",&n);
	switch(n)
	{
		case 1:sor_aver();break;
		case 2:sor_num();break;
		case 3:return; 
	}
}
void sor_num()
{
	Stu t;
	FILE * ft;
	i=0;
	fp=fopen("stu_a.txt","r");
	if(fp==NULL)
	{
		printf("\t\t\twe cannot find the file");
		return;
	}
	ft=fopen("stu_sort.txt","w");
	if(fp==NULL)
	{
		printf("we cannot find the file");
		return;
	}
	system("cls");
	fscanf(fp,"%s%s%s%s%s%s%s",temp.a,
						   temp.b,
						   temp.c,
						   temp.d,
						   temp.e,
						   temp.f,
						   temp.g);
	printf("\t\t%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	fprintf(ft,"%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	printf("\n"); 
	while(1)
	{
		int j=fscanf(fp,"%d%s%f%f%f%f%f",&stu[i].num,
						      	  stu[i].name,
						      	 &stu[i].score[0],
						      	 &stu[i].score[1],
						     	 &stu[i].score[2],
							 	 &stu[i].aver,
								  &stu[i].total);
		if(j==-1)break;	i++;
	}
	int te=i;
	for(i=0;i<te-1;i++)
	{
		for(int j=0;j<te-1-i;j++)
			if(stu[j].num>=stu[j+1].num)
			{
				t=stu[j];
				stu[j]=stu[j+1];
				stu[j+1]=t;
			}
	}
	for(i=0;i<te;i++)
	{
		fprintf(ft,"%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);
		printf("\t\t%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);							
	}	
	fclose(fp);
	fclose(ft);
	getchar();
	getchar();	
}
void sor_aver()
{
	Stu t;
	FILE * ft;
	i=0;
	fp=fopen("stu_a.txt","r");
	if(fp==NULL)
	{
		printf("\t\t\twe cannot find the file");
		return;
	}
	ft=fopen("stu_sort.txt","w");
	if(fp==NULL)
	{
		printf("we cannot find the file");
		return;
	}
	system("cls");
	fscanf(fp,"%s%s%s%s%s%s%s",temp.a,
						   temp.b,
						   temp.c,
						   temp.d,
						   temp.e,
						   temp.f,
						   temp.g);
	printf("\t\t%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	fprintf(ft,"%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	printf("\n"); 
	while(1)
	{
		int j=fscanf(fp,"%d%s%f%f%f%f%f",&stu[i].num,
						      	  stu[i].name,
						      	 &stu[i].score[0],
						      	 &stu[i].score[1],
						     	 &stu[i].score[2],
							 	 &stu[i].aver,
								  &stu[i].total);
		if(j==-1)break;	i++;
	}
	int te=i;
	for(i=0;i<te-1;i++)
	{
		for(int j=0;j<te-1-i;j++)
			if(stu[j].aver<=stu[j+1].aver)
			{
				t=stu[j];
				stu[j]=stu[j+1];
				stu[j+1]=t;
			}
	}
	for(i=0;i<te;i++)
	{
		fprintf(ft,"%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);
		printf("\t\t%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);							
	}	
	fclose(fp);
	fclose(ft);
	getchar();
	getchar();
}
void inp()
{
	system("cls");
	printf("\n\t\t\t------------Student Achievement Management System------------");
	printf("\n\n\n\t\t\t1. Insert Marks ");
	printf("\n\t\t\t2. Input Marks Rew");
	printf("\n\t\t\t3. Exit");
	printf("\n\n\t\t\t------------Student Achievement Management System------------"); 
	printf("\n\n\t\t\tPlease input the order number :");
	scanf("%d",&n);	
	switch(n)
	{
		case 1:inp_a();break;
		case 2:inp_o();break;
		case 3:return;
	}
}
void inp_o()
{
	long int num;
	int k;
	FILE *ft;
	printf("please input the number of students:");
	scanf("%d",&num);
	printf("please input the marks of students:\n");
	for(i=0;i<num;i++)
	{
		scanf("%ld%s%f%f%f",&stu[i].num,
						    stu[i].name,
						   &stu[i].score[0],
						   &stu[i].score[1],
						   &stu[i].score[2]);
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
		stu[i].total=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];	
	}
	for(i=0;i<num-1;i++)
		for(k=1;k<num-i;k++)
			if(stu[i].num==stu[k+i].num) 
			{
				system("cls");
				printf("\n\t\t\tThe inputing number repeat!!!");
				printf("\n\t\t\tWe have ignore that number,please return pressing anykey:\n");
				getchar();
				getchar();
				return;
			}
	ft=fopen("stu_a.txt","r");
	if(ft==NULL)
	{
		printf("we cannot find the file");
		return;
	}
	fp=fopen("stu.txt","w");
	if(fp==NULL)
	{
		printf("we cannot find the file");
		return;
	}
	fscanf(ft,"%s%s%s%s%s%s%s",temp.a,
						   temp.b,
						   temp.c,
						   temp.d,
						   temp.e,
						   temp.f,
						   temp.g);
	fprintf(fp,"%10s%10s%8s%8s%8s%8s%8s\n",temp.a,
							  temp.b,
							  temp.c,
							  temp.d,
							  temp.e,
							  temp.f,
							  temp.g);
	for(i=0;i<num;i++)
	fprintf(fp,"%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);
	system("cls"); 
	printf("\n\t\t\tthe file has been recorded");
	getchar();
	getchar();
	fclose(fp);	
	fclose(ft);
}
void inp_a()
{
	long int num;
	int k,j;
	Stu tem;
	printf("please input the number of students:");
	scanf("%d",&num);
	printf("please input the marks of students:\n");
	for(i=0;i<num;i++)
	{
		scanf("%ld%s%f%f%f",&stu[i].num,
						    stu[i].name,
						   &stu[i].score[0],
						   &stu[i].score[1],
						   &stu[i].score[2]);
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
		stu[i].total=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
	}
	fp=fopen("stu_a.txt","a+");
	if(fp==NULL)
	{
		printf("we cannot find the file");
		return;
	}
	for(i=0;i<num-1;i++)
		for(k=1;k<num-i;k++)
			if(stu[i].num==stu[k+i].num) 
			{
				system("cls");
				printf("\n\t\t\tThe inputing number repeat!!!");
				printf("\n\t\t\tWe have ignore that number,please return pressing anykey:\n");
				getchar();
				getchar();
				return;
			}
	fseek(fp,60L,0);
	
	while(1)
	{
		j=fscanf(fp,"%ld%s%f%f%f%f%f\n",&tem.num,
						      	  tem.name,
						      	 &tem.score[0],
						      	 &tem.score[1],
						     	 &tem.score[2],
							 	 &tem.aver,
								  &tem.total);	
		if(j==-1)break;
		for(i=0;i<num;i++)
			if(stu[i].num==tem.num)
			{
				system("cls");
				printf("\n\t\t\tThe inputing number repeat!!!");
				printf("\n\t\t\tWe have ignore that number,please return pressing anykey:\n");
				getchar();
				getchar();
				return;
			}		
	}	
	for(i=0;i<num;i++)
	fprintf(fp,"%10ld%10s%8.2f%8.2f%8.2f%8.2f%8.2f\n",stu[i].num,
						   						 stu[i].name,
						   						 stu[i].score[0],
						  						 stu[i].score[1],
						 			    		 stu[i].score[2],
												 stu[i].aver,
												 stu[i].total);
	system("cls"); 
	printf("\n\t\t\tthe file has been recorded");
	getchar();
	getchar();
	fclose(fp);
}
/*主要功能:
 	1.界面清晰,UI交互良好,操作之后有相应提示 
 	2.录入数据时进入小窗口,分为追加录入和重新录入,且都分别存入stu和stu_a文件中 
 	3.学号信息有10位长度
	4.学号具有唯一性,输入重复后有提示且直接重新输入,当追加输入时,会和文件中的学号进行比对 
	5.查询时进入小窗口,有按学号查询和全部查询两种 
	6.排序是按stu_a文件的平均值排序且存放到stu_sort文件中 
	7.更改信息时需要输入学号和第几个学科以及要改的成绩,并存放到stu_change文件中。
		当输入学号或者学科错误时有提示且返回上一界面 
*/