1.有5个学生,每个学生的资料包括学号、姓名、3门课成绩,用赋初值方法输入5个学生的数据到结构体数组中,输出每个学生3门课的平均成绩(保留2位小数)。输出格式为:
No name score1 score2 score3 average
101 Zhou 93 89 87 -
102 Yang 85 80 78 -
103 Chen 77 70 83 -
104 Qian 70 67 60 -
105 Li 72 70 69 -
2.在上题中,按平均成绩由高到低排序,输出每个学生的成绩,输出格式与上题相同。
#include <stdio.h>
struct student
{
int num;
char name[20];
float score[3];
float ave;
}stu[10];
int main()
{
void input(struct student stu1[]);
void ave_score(struct student stu1[]);
void max_score(struct student stu1[]);
input(stu);
ave_score(stu);
max_score(stu);
return 0;
}
void input(struct student stu1[])
{
int i;
printf("请输入5个学生的学号、姓名、3门课程成绩:\n");
for(i=0;i<5;i++)
{
printf("No.");
scanf("%d%s%f%f%f",&stu1[i].num,&stu1[i].name,&stu1[i].score[0],&stu1[i].score[1],&stu1[i].score[2]);
stu1[i].ave=(stu1[i].score[0]+stu1[i].score[1]+stu1[i].score[2])/3.0;
}
printf("\n");
}
void ave_score(struct student stu1[])
{
int i;
float sum=0;
for(i=0;i<10;i++)
sum+=stu1[i].ave;
printf("3门课程总平均成绩为:%.1f\n",sum/10.0);
}
void max_score(struct student stu1[])
{
int i,k=0;
for(i=1;i<10;i++)
{
if(stu1[k].ave<stu1[i].ave) k=i;
}
printf("\n分数最高的学生信息为:\n");
printf("学号:No.%d\n",stu1[k].num);
printf("姓名:%s\n",stu1[k].name);
printf("课程1成绩:%.1f\n",stu1[k].score[0]);
printf("课程2成绩:%.1f\n",stu1[k].score[1]);
printf("课程3成绩:%.1f\n",stu1[k].score[2]);
printf("3门课程平均成绩:%.1f\n",stu1[k].ave);
}