小憨总结:结构体按照某一项数据的大小进行排序时,需要交换的是结构体,而不是某一项
#include <stdio.h>
struct Student
{
int num;
char name[20];
float score;
};
int main()
{
struct Student stu[5] = {{101,"Mike",89.23},{102,"lucy",98.23},{103,"minky",95.23},{104,"mijlh",90.23},{105,"mghky",89.23}};
struct Student temp;
int n = 5;
int i,j,k;
printf("The order is\n");
for(i=0;i<n-1;i++)
{
k = i;
for(j=i+1;j<n;j++)
{
if(stu[k].score>stu[j].score)
{
temp = stu[k];
stu[k]=stu[j];
stu[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("学号 %5d 姓名 %8s 分数 %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
}
}
运行结果:
学号 101 姓名 Mike 分数 89.23
学号 105 姓名 mghky 分数 89.23
学号 104 姓名 mijlh 分数 90.23
学号 103 姓名 minky 分数 95.23
学号 102 姓名 lucy 分数 98.23