结构体中将学生学号,姓名和成绩放到同一个结构体中。
根据学生成绩进行排名,相应的调整学生的学号和姓名的顺序。
如果使用多个数组或字符串来调整的话也可以实现,但是会比较繁琐。
#include <stdio.h>
#include<string.h>
/* 功能:结构体的应用 按成绩信息排名 时间:2024年10月 地点:贤者楼129 作者:LChen
*/struct student{int ID; // 学号 char name[20]; // 姓名 double score; // 成绩
};
typedef struct student Student; int main() {int i,j;Student temp,stu[5]; // 定义 for(i=0;i<5;i++)scanf("%d %s %lf",&stu[i].ID,stu[i].name,&stu[i].score);for(i=0;i<4;i++) // 共进行n-1趟排序for(j=4;j>i;j--) // 递减循环,从后往前比较if(stu[j].score>stu[j-1].score){temp=stu[j-1];stu[j-1]=stu[j];stu[j]=temp;}// 输出成绩排名 for(i=0;i<5;i++)printf("%d %s %.2lf\n",stu[i].ID,stu[i].name,stu[i].score);return 0;
}