/*本题要求用结构体来存储三个学生的成绩,
每个学生有3门课的成绩(成绩为小数),
从键盘输入以上数据(包括姓名以及各课程成绩),
计算出平均成绩,然后按照平均分的降序排列输出。
*/
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define T 30
struct student
{
char name[T];
double subject1;
double subject2;
double subject3;
}student[N];
int Max(struct student student[])//求最高成绩学生
{
double max=student[0].subject1+student[0].subject2+student[0].subject3;
for(int i=0;i<N;i++)
max=max>student[i].subject1+student[i].subject2+student[i].subject3?max:student[i].subject1+student[i].subject2+student[i].subject3;
for(int i=0;i<N;i++)
if(max==student[i].subject1+student[i].subject2+student[i].subject3)
return i;
}
int Min(struct student student[])//求最低成绩学生
{
double min=student[0].subject1+student[0].subject2+student[0].subject3;
for(int i=0;i<N;i++)
min=min<student[i].subject1+student[i].subject2+student[i].subject3?min:student[i].subject1+student[i].subject2+student[i].subject3;
for(int i=0;i<N;i++)
if(min==student[i].subject1+student[i].subject2+student[i].subject3)
return i;
}
int compare(struct student student[],int (*p)(struct student student[]))//通用函数
{
int r=(*p)(student);
return r;
}
int main()
{
for(int i=0;i<N;i++)
scanf("%s %lf %lf %lf",&student[i].name,
&student[i].subject1,
&student[i].subject2,
&student[i].subject3);
int j=compare(student,Max);
int j2=compare(student,Min);
int j1;
for(int i=0;i<N;i++)
if(i!=j&&i!=j2)
j1=i;
double eva=(student[j].subject1+student[j].subject2+student[j].subject3)/3;
printf("%s %.2lf\n",student[j],eva);
double eva1=(student[j1].subject1+student[j1].subject2+student[j1].subject3)/3;
printf("%s %.2lf\n",student[j1],eva1);
double eva2=(student[j2].subject1+student[j2].subject2+student[j2].subject3)/3;
printf("%s %.2lf\n",student[j2],eva2);
return 0;
}