#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 12
FILE *fi
struct student
{
int num
char name[8]
int yuwen
int shuxue
int yingyu
}st[N]
void main()
{
struct student t
int i,j
char* s="学号 姓名 语文 数学 英语 总分 平均分\n"
double avr[N],sum[N]
for(i=0i<Ni++)
sum[i]=0
printf("输入%d 学生信息\n",N)
printf("**************************************************\n")
printf("学号 姓名 语文 数学 英语\n")
for(i=0i<Ni++)
scanf("%d%s%d%d%d%d",&st[i].num,st[i].name,&st[i].yuwen,&st[i].shuxue,&st[i].yingyu)
if((fi=fopen("d:\\student.txt","w"))==NULL)
{
printf("文件无法打开\n")
exit(1)
}
for(i=0i<Ni++)
fprintf(fi,"%d %s %d %d %d %d\n",st[i].num,st[i].name,st[i].yuwen,st[i].shuxue,st[i].yingyu)
fclose(fi)
//将文件进行保存
printf("学号 姓名 平均分 总分\n")
for(i=0i<Ni++)
{
sum[i]=st[i].yuwen+st[i].shuxue+st[i].yingyu
avr[i]=(st[i].yuwen+st[i].shuxue+st[i].yingyu)/3.0
printf("%d %s %.2lf %.2lf",st[i].num,st[i].name,avr[i],sum[i])
printf("\n")
}
printf("按总成绩排序如下\n")
printf("**************************************************\n")
for(i=0i<N-1i++)
for(j=0j<N-i-1j++)
if(sum[j]<=sum[j+1])
{
t=st[j]
st[j]=st[j+1]
st[j+1]=t
}
printf("学号 姓名 语文 数学 英语 总成绩\n\n")
for(i=0i<Ni++)
printf("%d %s %d %d %d %d %.2lf\n",st[i].num,st[i].name,st[i].yuwen,st[i].shuxue,st[i].yingyu,sum[i])
for(i=0i<Ni++)
{
sum[i]=st[i].yuwen+st[i].shuxue+st[i].yingyu
avr[i]=(st[i].yuwen+st[i].shuxue+st[i].yingyu)/3.0
}
//再次打开文件将学生的成绩按总成绩的高低进行排序输出到文件保存
if((fi=fopen("d:\\student.txt","w"))==NULL)
{
printf("文件无法打开\n")
exit(1)
}
fputs(s,fi)
for(i=0i<Ni++)
fprintf(fi,"%d %s %d %d %d %d %.2lf %.2lf\n",st[i].num,st[i].name,st[i].yuwen,st[i].shuxue,st[i].yingyu,sum[i],avr[i])
fclose(fi)
getchar()
}