#include<stdio.h>
void main(){
const int count = 5;//定义数量
struct student{
char name[80];
float math,eng;
float aver;
}stu[count],temp;
//输入
for (int i = 0; i < count; i++){
scanf("%s%f%f", stu[i].name, &stu[i].math, &stu[i].eng);
}
//将平均值放入进去
for (int i = 0; i < count; i++){
stu[i].aver = (stu[i].math + stu[i].eng )/ 2;
}
/*求平均分最高的同学姓名和平均分
int sub = 0;
for (int i = 1; i < 5; i++)
if (stu[i].aver>stu[sub].aver)sub = i;
printf("%s%f\n", stu[sub].name, stu[sub].aver);*/
/*依照平均分从低到高排序*/
for (int i = 0; i < count - 1; i++)
for (int j = 0; j < count - 1 - i; j++)
if (stu[j].aver>stu[j + 1].aver){
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
//输出
for (int i = 0; i < count; i++){
printf("%s\t%f\t%f\t%f\n", stu[i].name, stu[i].math, stu[i].eng, stu[i].aver);
}
}
c语言学习之结构篇代码演示样例-输入n个同学的姓名,数学英语成绩,依照平均分从低到高排序并输出,布布扣,bubuko.com