#define STU_NUM		2	  			/* 最多的学生人数 */
#define COURSE_NUM	2				/* 最多的考试科目 */
#define TUB 50                      /* 学生数 */	

#include<stdio.h>
FILE *fp;

struct student
{
	int		number;					/* 每个学生的学号 */
	char	name[10];              	/* 每个学生的姓名 */
	int		score[COURSE_NUM];		/* 每个学生M门功课的成绩 */
	int		sum;                   	/* 每个学生的总成绩 */
	float	average;               	/* 每个学生的平均成绩 */
}s[TUB]={0,'ti',0,0,0};
typedef struct student STU;


char menu(void)/*菜单函数*/
{
	char ch;
	printf("\nManagement for Students' scores \n");
	printf("1  Append record\n");
	printf("2  List record \n");
	printf("3  Delete record\n");
	printf("4  Modify record \n");
	printf("5  Search record \n");
	printf("6  Sort Score in descending order by sum \n");
	printf("7  Sort Score in ascending order by sum\n");
	printf("8  Sort Score in descending order by num \n");
	printf("9  Sort Score in ascending order by num\n");
	printf("w  Write to a File \n");
	printf("r  Read from a File \n");
	printf("0  Exit\n ");
	printf("Please Input your choice:");
	scanf(" %c",&ch);
	return(ch);
}

void append()/* 添加记录,计算出总分和平均值*/
{
	
	int i,j;
	printf("请输入数据:\n");//输入数据
	for(i=0;i<STU_NUM;i++)
	{	printf("学号:");
		scanf("%d",&s[i].number);
		printf("姓名:");
		scanf("%s",s[i].name);
		for(j=0;j<COURSE_NUM;j++)
		{	printf("成绩:");
			scanf("%d",&s[i].score[j]);
		}
	}
	
	for(i=0;i<STU_NUM;i++)//求总分
	{	for(j=0;j<COURSE_NUM;j++)
		s[i].sum=s[i].sum+s[i].score[j];
		printf("总分%d  ",s[i].sum);
	}


	for(i=0;i<STU_NUM;i++)//求平均数
	{	s[i].average=(s[i].sum)/(COURSE_NUM);
		printf("平均分%.2f ",s[i].average);
	}


}
void list()/*显示记录_在屏幕中打印所有学生成绩*/
{
	int i,j;
	for(i=0;i<STU_NUM;i++)
	{	printf("学号%3d,姓名%s,",s[i].number,s[i].name);
		for(j=0;j<COURSE_NUM;j++)
			printf("分数%4d,",s[i].score[j]);
		printf("总分%4d,平均分%4.2f\n",s[i].sum,s[i].average);
	}
}


void delete()/*删除记录_用后面的数据替换前一个*/
{
	int a,i,b=0;
	printf("Please input student's number you want to delete:\n");
	scanf("%d",&a);
	for(i=0;i<STU_NUM;i++)
	{		
		if(s[i].number==a)
		{b=1;s[i]=s[i+1];}
		
	}
	if(b==0)
		printf("The number is not found!\nplease check it again!\n");
}

void modify()/*修改记录_对要修改的数据重新复值*/
{
	int b=0,a,i,j;
	printf("Please input student's number you want to modify:\n");
	scanf("%d",&a);
	for(i=0;i<STU_NUM;i++)
	{	if(s[i].number==a)
		{	b=1;
			scanf("%d%s",&s[i].number,&s[i].name);			
			for(j=0;j<COURSE_NUM;j++)
				scanf("%d",&s[i].score[j]);
		}
	}
	if(b==0)
		printf("The number is not found!\nplease check it again!\n");
}


void search()/*查询记录_输入学生学号,输出对应的成绩数据*/
{
	int a,i,j;
	printf("Please input student's number you want to modify:\n");
	scanf("%d",&a);
	for(i=0;i<STU_NUM;i++)
	{	if(s[i].number==a)
		printf("%d,%s,",s[i].number,s[i].name);
		for(j=0;j<COURSE_NUM;j++)
			printf("%d,",s[i].score[j]);
		printf("%d,%.2f\n",s[i].sum,s[i].average);
	}
}


void descending_sum()/*按总分排序_降序排列,打印出学生姓名和成绩*/
{
	int i,j;
	STU t;
	for(i=0;i<STU_NUM;i++)
	{	for(j=i+1;j<STU_NUM;j++)
		if(s[i].sum<s[j].sum)
		{
			t=s[j];s[j]=s[i];s[i]=t;
		}
	}
	for(i=0;i<STU_NUM;i++)	
		{	printf("%d,%s,",s[i].number,s[i].name);
			for(j=0;j<COURSE_NUM;j++)
				printf("%d,",s[i].score[j]);
			printf("%d,%.2f\n",s[i].sum,s[i].average);
		}

}

void ascending_sum()/*按总分排序_升序排列,打印出学生姓名和成绩*/
{
	int i,j;
	STU t;
	for(i=0;i<STU_NUM;i++)
	{	for(j=i+1;j<STU_NUM;j++)
		if(s[i].sum>s[j].sum)
		{
			t=s[j];s[j]=s[i];s[i]=t;
		}
	}
	for(i=0;i<STU_NUM;i++)	
		{	printf("%d,%s,",s[i].number,s[i].name);
			for(j=0;j<COURSE_NUM;j++)
				printf("%d,",s[i].score[j]);
			printf("%d,%.2f\n",s[i].sum,s[i].average);
		}

}

void descending_number()/*按学号排序_按学号降序排列,打印出学生姓名和成绩*/
{
	int i,j;
	STU t;
	for(i=0;i<STU_NUM;i++)
	{	for(j=i+1;j<STU_NUM;j++)
		if(s[i].number<s[j].number)
		{
			t=s[j];s[j]=s[i];s[i]=t;
		}
	}
	for(i=0;i<STU_NUM;i++)	
		{	printf("%d,%s,",s[i].number,s[i].name);
			for(j=0;j<COURSE_NUM;j++)
				printf("%d,",s[i].score[j]);
			printf("%d,%.2f\n",s[i].sum,s[i].average);
		}

}
void ascending_number()/*按学号排序_按学号降序排列,打印出学生姓名和成绩*/
{
	int i,j;
	STU t;
	for(i=0;i<STU_NUM;i++)
	{	for(j=i+1;j<STU_NUM;j++)
		if(s[i].number>s[j].number)
		{
			t=s[j];s[j]=s[i];s[i]=t;
		}
	}
	for(i=0;i<STU_NUM;i++)	
		{	printf("%d,%s,",s[i].number,s[i].name);
			for(j=0;j<COURSE_NUM;j++)
				printf("%d,",s[i].score[j]);
			printf("%d,%.2f\n",s[i].sum,s[i].average);
		}

}

void write()/*写入文件*/
{
	FILE *fp;
	int i;
	fp=fopen("student_data","wb");
	for(i=0;i<STU_NUM;i++)
	{
		fwrite(&s[i],sizeof(struct student),1,fp);
		printf("write to file successfully.\n");
	}
	fclose(fp);
}
void read()/*从文件中读出成绩*/
{
	FILE *fp;
	int i,j;
	fp=fopen("student_data","rb");
	if(fp==NULL)
	{
		printf("cannot open this file!\n");
		exit(0);
	}
	for(i=0;i<STU_NUM;i++)
		fread(&s[i],sizeof(struct student),1,fp);
	for(i=0;i<STU_NUM;i++)	
	{	printf("%d,%s,",s[i].number,s[i].name);
		for(j=0;j<COURSE_NUM;j++)
			printf("%d,",s[i].score[j]);
		printf("%d,%.2f\n",s[i].sum,s[i].average);
	}
	fclose(fp);
}
int main()/*主程序_选择要执行的命令*/
{
	FILE *fp;
	char ch;
	while(1)
	{
		ch=menu();
		switch(ch)
		{
			case'1':append();break;
			case'2':list();break;
			case'3':delete();list();break;
			case'4':modify();list();break;
			case'5':search();break;
			case'6':descending_sum();break;
			case'7':ascending_sum();break;
			case'8':descending_number();break;
			case'9':ascending_number();break;
			case'w':write();break;
			case'r':read();break;
			case'0':return 0;
			default:printf("Wrong choice!\n");
		}
	}
}