第2个回答 推荐于2016-06-20
/*自己写 的,已经发多一份到你的邮箱!*/
#include "stdio.h"
#include "stdlib.h"
typedef struct stutttt
{ /*定义结构体*/
char name[50];
char num[50];
double score;
}stu;
stu student,s[1000];
int n;
double max;
double average;
char fileI[20] = "stuin.txt";
char fileO[20] = "stuout.txt";
int cmp( const void * a, const void * b )
{
if(((stu*)a)->score < ((stu*)b)->score) return 1;
else return -1;
}
void Sort()
{
qsort(s,n,sizeof(stu),cmp);
}
void Pnt()
{
int i;
for(i=0;i<n;i++)
{
printf("%s %s %.0lf\n",s[i].num,s[i].name,s[i].score);
}
printf("\n");
}
void Read()
{
n = 0;
FILE *fp;
if((fp=fopen(fileI,"r"))==NULL)
{ /*以只读打开文件*/
printf("cannot open file");
getchar();
exit(0);
}
while( fscanf(fp,"%s%s%lf\n",s[n].num,s[n].name,&s[n].score)!=EOF /* 从文件读*/ )
{
n++;
}
fclose(fp);
}
void Solve()
{
int i;
int h=0,e=0,l=0;
int m90_100 = 0,m80_89 = 0, m70_79 = 0,m60_69=0,m0_59=0;
char sss[81] = "";/*"--------------------------------------------------------------------------------";*/
FILE * fp;
if((fp=fopen(fileO,"w"))==NULL)
{ /*以只读打开文件*/
printf("cannot open file");
getchar();
exit(0);
}
average = 0;
max = 0;
for(i=0;i<n;i++)
{
if( s[i].score > max ) max = s[i].score;
average += s[i].score;
}
average /= n;
puts("最高分的人:");
fputs("最高分的人:\n",fp);
for(i=0;i<n;i++)
{
if( s[i].score < average ) l++;
else if( s[i].score > average ) h++;
else e++;
if( s[i].score>=90 ) m90_100++;
else if( s[i].score>=80 ) m80_89 ++;
else if( s[i].score>=70 ) m70_79 ++;
else if( s[i].score>=60 ) m60_69 ++;
else m0_59 ++;
if( s[i].score < max ) continue;
printf("%s %s %.0lf\n",s[i].num,s[i].name,s[i].score);
fprintf(fp,"%s %s %.0lf\n",s[i].num,s[i].name,s[i].score);
}
printf("\n");
fprintf(fp,"\n");
printf("平均成绩:%lf\n",average);
printf("高于平均 %d 人\n",h);
printf("等于平均 %d 人\n",e);
printf("低于平均 %d 人\n",l);
fprintf(fp,"平均成绩:%lf\n",average);
fprintf(fp,"高于平均 %d 人\n",h);
fprintf(fp,"等于平均 %d 人\n",e);
fprintf(fp,"低于平均 %d 人\n",l);
puts("");
fputs("",fp);
printf("优秀(90~100) %d 人数, 百分比;%%%lf\n",m90_100,100.0*m90_100/n);
printf("优秀(80~89 ) %d 人数, 百分比;%%%lf\n",m80_89 ,100.0*m80_89 /n);
printf("优秀(70~79 ) %d 人数, 百分比;%%%lf\n",m70_79 ,100.0*m70_79 /n);
printf("优秀(60~69 ) %d 人数, 百分比;%%%lf\n",m60_69 ,100.0*m60_69 /n);
printf("优秀(00~50 ) %d 人数, 百分比;%%%lf\n",m0_59 ,100.0*m0_59 /n);
fprintf(fp,"优秀(90~100) %d 人数, 百分比;%%%lf\n",m90_100,100.0*m90_100/n);
fprintf(fp,"优秀(80~89 ) %d 人数, 百分比;%%%lf\n",m80_89 ,100.0*m80_89 /n);
fprintf(fp,"优秀(70~79 ) %d 人数, 百分比;%%%lf\n",m70_79 ,100.0*m70_79 /n);
fprintf(fp,"优秀(60~69 ) %d 人数, 百分比;%%%lf\n",m60_69 ,100.0*m60_69 /n);
fprintf(fp,"优秀(00~50 ) %d 人数, 百分比;%%%lf\n",m0_59 ,100.0*m0_59 /n);
fclose(fp);
printf("\n结果已经写入文件%s\n",fileO);
}
main( )
{
int i;
Read();
Solve();
/* printf("来自文件的数据:\n");*/
/* Pnt();*/
/* Sort();*/
/* printf("按分数降序排序后的数据:\n");*/
/* Pnt();*/
getchar();
}本回答被提问者和网友采纳