#include<stdio.h>
struct student
{
int id;
char name[10];
float score[3];
float max;
float min;
};
float high(float a[]);
float low(float b[]);
int writefile(struct student stu[]);
void main()
{
struct student stu[5];
int n,m,p;
for(n=0;n<5;n++)
{ printf("please input the <%d> student information:\n",n+1);
printf("student number <100~999>:");
scanf("%d",&stu[n].id);
while(stu[n].id>999||stu[n].id<100)
{ printf("The number is wrong,please repeat now.\n");
printf("student number <100~999>:");
scanf("%d",&stu[n].id);
}
printf("student name:");
scanf("%s",stu[n].name);
for(m=0;m<3;m++)
{ printf("the %d subject score:",m+1);
scanf("%f",&stu[n].score[m]);
while(stu[n].score[m]>100||stu[n].score[m]<0)
{ printf("The score is wrong,please repeat now.\n");
printf("the %d subject score:",m+1);
scanf("%f",&stu[n].score[m]);
}
}
stu[n].max=high(stu[n].score);
stu[n].min=low(stu[n].score);
}
p=writefile(stu);
if(p==-1) printf("file can not open!\n");
for(n=0;n<5;n++)
{ printf("The %d student information:\n",n+1);
printf("\tstudent-no:%d\n",stu[n].id);
printf("\tstudent-name:%s\n",stu[n].name);
for(m=0;m<3;m++)
printf("\tthe m+1 subject:%f\n",stu[n].score[m]);
printf("\n");
printf("\n");
printf("\tthe highest score is %f\n",stu[n].max);
printf("\tthe lowst score is %f\n",stu[n].min);
}
}
float high(float a[])
{
float MAX=a[0];
int n;
for(n=0;n<3;n++)
if(MAX<a[n]) MAX=a[n];
return MAX;
}
float low(float b[])
{
float MIN=b[0];
int j;
for(j=0;j<3;j++)
if(MIN>b[j]) MIN=b[j];
return MIN;
}
int writefile(struct student stu[])
{
FILE *fp;
int m;
char *p="%d %s %f %f %f %f %f\n";
fp=fopen("StudentScore.txt","w");
if(fp==NULL)
{ printf("file can not open!\n");
return -1;
}
for(m=0;m<5;m++)
fprintf(fp,p,stu[m].id,stu[m].name,stu[m].score[0],stu[m].score[1],stu[m].score[2],stu[m].max,stu[m].min);
fclose(fp);
return 0;
}
献丑了