//134-平均分排序
//将第五题"stud"文件中的学生数据,按平均分进行排序处理,将已排序的学生数据
//存入到一个新文件“stu-sort"中
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
int number;
int score[3];
float aver;
}student;
int main(void)
{
FILE *fp1,*fp2;
fp1=fopen("stud.txt","r");
fp2=fopen("stu-sort.txt","w");
if(fp1 == NULL || fp2 == NULL)
{
printf("cannot be open !");
exit(0);
}
//关键问题如何提出来??
//读入数据
int i,j;
student stu[5];
for(i=0;i<5;i++)
{
fscanf(fp1,"%d",&stu[i].number);
for(j=0;j<3;j++)
{
fscanf(fp1,"%d",&stu[i].score[j]);
}
fscanf(fp1,"%f",&stu[i].aver);
}
student temp;
//排序
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(stu[j].aver<stu[j+1].aver)
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]= temp;
}
}
}
//输出
for(i=0;i<5;i++)
{
fprintf(fp2,"%d ",stu[i].number);
for(j=0;j<3;j++)
{
fprintf(fp2,"%d ",stu[i].score[j]);
}
fprintf(fp2,"%f\n",stu[i].aver);
}
fclose(fp1);
fclose(fp2);
return 0;
}