C语言 学生成绩统计

根据已有的学生成绩表文件,对学生成绩进行统计显示,包括:

1、按平均分进行排名

2、统计每门课的平均分

3、打印所有学生成绩条

4、打印不及格学生信息

5、打印优等生名单(优等生条件:平均分大于90;平均分大于85且有一门满分;平均分大于85且有两门95以上;三门都及格)

#include <stdio.h>
#include <stdlib.h>

//学生数据结构
typedef struct _student
{
	int id;
	char name[20];
	int engscore;
	int cscore;
	int mathscore;
	float avg;
}student;

//平均分数据结构,用于平均分排名
typedef struct _avginfo
{
	int idx;		//学生信息在stu数组中的序号
	float avg;		//学生平均分
	int ranking;	//名次
}avginfo;

student stu[100];	//班级学生数据存储
avginfo	avg[100];	//学生的平均分信息,用于排名
int stucount = 0;	//班级学生数量
int scoreseg[3][5]; //统计的每门课平均分分数段人数
float classavg[3];	//每门课的班级平均分
char seg[5][20] = {"<60","60~69","70~79","80~89",">=90"};

//对平均成绩进行冒泡排序并统计排名
void avgsort()
{
	//冒泡排序
	int i,j;
	avginfo t;
	for(i=0;i<stucount-1;i++)
		for(j=0;j<stucount-i-1;j++)
		{
			if(avg[j].avg < avg[j+1].avg)
			{
				t = avg[j];
				avg[j] = avg[j+1];
				avg[j+1] = t;
			}
		}
	//统计排名情况
	for(i=0;i<stucount;i++)
	{
		if(i==0)
		{
			avg[i].ranking = 1;
		}
		else
		{
			if(avg[i].avg == avg[i-1].avg)
				avg[i].ranking = avg[i-1].ranking;
			else
				avg[i].ranking = i+1;
		}
	}
}

//从文件读取学生信息
void readstudentinfo()
{
	FILE *fp;
	if((fp = fopen("studentinfo.txt","r")) == NULL)
	{
		printf("studentinfo.txt打开失败。\n");
	}
	else
	{
		char buf[200] = {0};
		while(fgets(buf,200,fp) != NULL)
		{
			fscanf(fp,"%d\t%s\t%d\t%d\t%d",
				&stu[stucount].id,
				stu[stucount].name,
				&stu[stucount].engscore,
				&stu[stucount].cscore,
				&stu[stucount].mathscore);
			stu[stucount].avg = (stu[stucount].engscore + stu[stucount].cscore + stu[stucount].mathscore)/3.0;
			avg[stucount].idx = stucount;
			avg[stucount].avg = stu[stucount].avg;
			stucount++;
		}
		fclose(fp);
	}
}

//显示主菜单
void showmenu()
{
	system("cls");
	printf("				学生成绩管理系统\n\n");
	printf("			1、按平均分排名\n");
	printf("			2、统计每门课平均分\n");
	printf("			3、打印所有学生成绩条\n");
	printf("			4、打印不及格学生信息\n");
	printf("			5、打印优等生名单\n");
	printf("			0、退出系统\n");
	printf("		\n请输入您的选择:");
}

//按平均分排名
void showavg()
{
	printf("%5s\t%20s\t%10s\t%10s\t%10s\t%10s\t%10s\n","学号","姓名","英语","C语言","高等数学","平均成绩","名次");
	for(int i=0;i<stucount;i++)
	{
		printf("%5d\t%20s\t%10d\t%10d\t%10d\t%10.1f\t%10d\n",
			stu[avg[i].idx].id,
			stu[avg[i].idx].name,
			stu[avg[i].idx].engscore,
			stu[avg[i].idx].cscore,
			stu[avg[i].idx].mathscore,
			avg[i].avg,
			avg[i].ranking);
	}
}

//存储平均分
void saveavg()
{
	FILE *fp;
	if((fp = fopen("avgsort.txt","w"))==NULL)
	{
		printf("avgsort.txt打开失败。\n");
	}
	else
	{
		fprintf(fp,"%5s\t%20s\t%10s\t%10s\t%10s\t%10s\t%10s\n","学号","姓名","英语","C语言","高等数学","平均成绩","名次");
		for(int i=0;i<stucount;i++)
		{
			fprintf(fp,"%5d\t%20s\t%10d\t%10d\t%10d\t%10.1f\t%10d\n",
				stu[avg[i].idx].id,
				stu[avg[i].idx].name,
				stu[avg[i].idx].engscore,
				stu[avg[i].idx].cscore,
				stu[avg[i].idx].mathscore,
				avg[i].avg,
				avg[i].ranking);
		}
		fclose(fp);
	}
}

//统计分数段
void statclassavg()
{
	int i,j,n;
	for(i=0;i<3;i++)
	{
		for(j=0;j<5;j++)
			scoreseg[i][j] = 0;
		classavg[i] = 0;
	}
	//
	for(j=0;j<stucount;j++)
	{
		//统计英语
		n = stu[j].engscore/10;
		switch(n)
		{
		case 10:
		case 9:
			scoreseg[0][4]++;
			break;
		case 8:
			scoreseg[0][3]++;
			break;
		case 7:
			scoreseg[0][2]++;
			break;
		case 6:
			scoreseg[0][1]++;
			break;
		default:
			scoreseg[0][0]++;
			break;
		}
		classavg[0] += stu[j].engscore;
		//统计C
		n = stu[j].cscore/10;
		switch(n)
		{
		case 10:
		case 9:
			scoreseg[1][4]++;
			break;
		case 8:
			scoreseg[1][3]++;
			break;
		case 7:
			scoreseg[1][2]++;
			break;
		case 6:
			scoreseg[1][1]++;
			break;
		default:
			scoreseg[1][0]++;
			break;
		}
		classavg[1] += stu[j].cscore;
		//统计数学
		n = stu[j].mathscore/10;
		switch(n)
		{
		case 10:
		case 9:
			scoreseg[2][4]++;
			break;
		case 8:
			scoreseg[2][3]++;
			break;
		case 7:
			scoreseg[2][2]++;
			break;
		case 6:
			scoreseg[2][1]++;
			break;
		default:
			scoreseg[2][0]++;
			break;
		}
		classavg[2] += stu[j].mathscore;
	}
	classavg[0] = classavg[0]/stucount;
	classavg[1] = classavg[1]/stucount;
	classavg[2] = classavg[2]/stucount;
}

//显示分数段统计
void showclassavg()
{
	printf("%10s\t%10s\t%10s\t%10s\n","分数段","英语","C语言","高等数学");
	for(int i=0;i<5;i++)
	{
		printf("%10s\t%10d\t%10d\t%10d\n",seg[i],scoreseg[0][i],scoreseg[1][i],scoreseg[2][i]);
	}
	printf("%10s\t%10.1f\t%10.1f\t%10.1f\n","平均分",classavg[0],classavg[1],classavg[2]);
}

//存储分数段统计
void saveclassavg()
{
	FILE *fp;
	if((fp = fopen("scoreseg.txt","w")) == NULL)
	{
		printf("scoreseg.txt文件打开失败。\n");
	}
	else
	{
		fprintf(fp,"%10s\t%10s\t%10s\t%10s\n","分数段","英语","C语言","高等数学");
		for(int i=0;i<5;i++)
		{
			fprintf(fp,"%10s\t%10d\t%10d\t%10d\n",seg[i],scoreseg[0][i],scoreseg[1][i],scoreseg[2][i]);
		}
		fprintf(fp,"%10s\t%10.1f\t%10.1f\t%10.1f\n","平均分",classavg[0],classavg[1],classavg[2]);
		fclose(fp);
	}
}

//显示所有学生成绩条
void showallscore()
{
	for(int i=0;i<stucount;i++)
	{
		printf("%5s\t%20s\t%10s\t%10s\t%10s\t%10s\n","学号","姓名","英语","C语言","高等数学","平均成绩");
		printf("%5d\t%20s\t%10d\t%10d\t%10d\t%10.1f\n\n",
			stu[i].id,
			stu[i].name,
			stu[i].engscore,
			stu[i].cscore,
			stu[i].mathscore,
			stu[i].avg);
	}
}

//显示成绩不及格学生信息
void showfailinfo()
{
	int i;
	printf("%5s\t%20s\t%10s\t%10s\n", "学号", "姓名", "课程","成绩");
	for (i = 0; i < stucount; i++)
	{
		if (stu[i].engscore < 60)
			printf("%5d\t%20s\t%10s\t%10d\n", stu[i].id,stu[i].name,"英语",stu[i].engscore);
		if (stu[i].cscore < 60)
			printf("%5d\t%20s\t%10s\t%10d\n", stu[i].id, stu[i].name, "C语言", stu[i].cscore);
		if (stu[i].mathscore < 60)
			printf("%5d\t%20s\t%10s\t%10d\n", stu[i].id, stu[i].name, "高等数学", stu[i].mathscore);
	}
}

//显示优等生信息
void showgoodinfo()
{
	printf("%5s\t%20s\t%10s\t%10s\t%10s\t%10s\t%10s\n", "学号", "姓名", "英语", "C语言", "高等数学", "平均成绩", "名次");
	for (int i = 0; i < stucount; i++)
	{
		//前三名
		if (avg[i].ranking <= 3)
		{
			//必须都及格
			if (stu[avg[i].idx].engscore < 60 ||
				stu[avg[i].idx].cscore < 60 ||
				stu[avg[i].idx].mathscore < 60)
				continue;
			bool good = false;
			if (avg[i].avg > 90) //平均分大于90则优等
				good = true;
			else if (avg[i].avg > 85)
			{
				if (stu[avg[i].idx].engscore == 100 ||  //平均分大于85且有一门100则优等
					stu[avg[i].idx].cscore == 100 ||
					stu[avg[i].idx].mathscore == 100)
					good = true;
				else //平均分大于85,且有两门达到95则优等
				{
					int n = 0;
					if (stu[avg[i].idx].engscore >= 95)  
						n++;
					if (stu[avg[i].idx].cscore >= 95)
						n++;
					if (stu[avg[i].idx].mathscore >= 95)
						n++;
					if (n >= 2)
						good = true;
				}
			}
			if (good)
			{
				printf("%5d\t%20s\t%10d\t%10d\t%10d\t%10.1f\t%10d\n",
					stu[avg[i].idx].id,
					stu[avg[i].idx].name,
					stu[avg[i].idx].engscore,
					stu[avg[i].idx].cscore,
					stu[avg[i].idx].mathscore,
					avg[i].avg,
					avg[i].ranking);
			}
		}
		else
			break;
	}
}

void main()
{
	system("mode con cols=120 lines=40");  //调整控制台窗口大小,以便信息显示完整
	readstudentinfo();
	while(1)
	{
		showmenu();
		//计算平均分并排序
		avgsort();
		//统计分数段数据
		statclassavg();
		int sel;
		scanf("%d",&sel);
		getchar();
		switch(sel)
		{
		case 1:		//按平均分排名
			showavg();
			saveavg();
			break;
		case 2:		//统计每门课平均分
			showclassavg();
			saveclassavg();
			break;
		case 3:		//打印所有学生成绩条
			showallscore();
			break;
		case 4:		//打印不及格学生信息
			showfailinfo();
			break;
		case 5:		//打印优等生名单
			showgoodinfo();
			break;
		case 0:
			exit(0);
		}
		system("pause");
	}
}

上一篇:使用struts2实现一个可以接收并返回Json数据的接口,此接口对接一个第三方接口


下一篇:MVC开发规则