你的main()函数不够简洁,我增加了计算平均分并按成绩排序输出前5名的功能,
我修改后是:
#include
#define SIZE 5
struct stud_score
{
long num;
char name[10];
double score[3];
double average;
};
void input(struct stud_score *,int);
void sort(struct stud_score *,int);
void output(struct stud_score *);
void main()
{
struct stud_score student[SIZE];
input(student,SIZE);
sort(student,SIZE);
output(student);
}
void input(struct stud_score *student,int size)
{
int i;
printf("请按以下格式输入:\n\n"
"学号\t姓名\t分数1\t分数2\t分数3 \n");
for(i=0;i
{
printf("请输入第%d组数据\n>",i+1);
scanf(" %d %s %lf %lf %lf",
&student[i].num,&student[i].name,
&student[i].score[0],&student[i].score[1],&student[i].score[2]);
student[i].average=(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
}
}
void sort(struct stud_score *student,int size)
{
int i,j;
struct stud_score temp;
for(i=0;i
for(j=i+1;j
{
if(student[i].average
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
}
void output(struct stud_score *student)
{
int i;
printf("成绩前五是:\n");
printf("学号\t姓名\t分数1\t分数2\t分数3\t平均成绩\n");
for(i=0;i<5;i++)
printf(" %d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",
student[i].num, student[i].name,
student[i].score[0], student[i].score[1], student[i].score[2],
student[i].average);
}