结构体数组的使用。有n(2<n<10)个学生,每个学生的数据包括学号(长度为12的字符串)、姓名(长度为12的字符串)、3门课程的成绩。从键盘输入n个学生的数据,要求输出3门课程的总平均成绩,以及最高分的学生的学号、姓名、3门课程成绩、平均分数。
要求:
1)输入n的值以及n个学生的上述信息,每个学生一行,各项数据之间用一个空格分隔;
2)输出学生信息成绩表,输出格式:%10s%10s%9.2f%9.2f%9.2f%8.2f;对应下述表头信息
No. name score[1] score[2] score[3] average
3)全班平均分、最高分数学生三门成绩及其平均分采用%6.2f格式输出,最高分数学生的学号和姓名均采用%10s的格式输出。
运行参考示例:
输入1:
3
1004 zhang 85 78 84
1002 chen 77 67 58
1006 zhao 96 89 88
输出1:
No. name score[1] score[2] score[3] average
1004 zhang 85.00 78.00 84.00 82.33
1002 chen 77.00 67.00 58.00 67.33
1006 zhao 96.00 89.00 88.00 91.00
average= 80.22
The highest score is: 1006 zhao
His scores are: 96.00 89.00 88.00,average: 91.00
输入2:
4
21031005 Zhou 90 84 99
21031008 Liu 96 93 94
21031009 Lin 50 97 80
21031010 Zeng 65 97 77
输出2:
No. name score[1] score[2] score[3] average
21031005 Zhou 90.00 84.00 99.00 91.00
21031008 Liu 96.00 93.00 94.00 94.33
21031009 Lin 50.00 97.00 80.00 75.67
21031010 Zeng 65.00 97.00 77.00 79.67
average= 85.17
The highest score is: 21031008 Liu
His scores are: 96.00 93.00 94.00,average: 94.33
运行代码如下:
#include<stdio.h>
struct Student
{
char num[12];
char name[12];
float score[3];
float average;
};
void max_aver(struct Student stu[10],int n)
{
int i,max;
max=0;
float aver=0;
for(i=0;i<n;i++)
{
if(stu[i].average>stu[max].average)
max=i;
aver=stu[i].average+aver;
}
printf("average=%6.2f\n",aver/n);
printf("The highest score is:%10s%10s\n",stu[max].num,stu[max].name);
printf("His scores are:%6.2f%6.2f%6.2f,average:%6.2f\n",stu[max].score[0],stu[max].score[1],stu[max].score[2],stu[max].average);
}
int main()
{
int n,i;
scanf("%d",&n);
struct Student stu[10];
for(i=0;i<n;i++)
{
scanf("%s%s%f%f%f",stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
printf(" No. name score[1] score[2] score[3] average\n");
for(i=0;i<n;i++)
{
stu[i].average=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
printf("%10s%10s%9.2f%9.2f%9.2f%8.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].average);
}
max_aver(stu,n);
return 0;
}