[c]代码库
#include <stdio.h>
#define N 3
struct student {
char name[20];
long num;
float score;
};
void sort(struct student stu[]) {
struct student t;
int i, j;
for (i = 0; i < N - 1; i++) {
for (j = N - 1; j > i; j--)
if (stu[j].score > stu[j - 1].score) {
t = stu[j];
stu[j] = stu[j - 1];
stu[j - 1] = t;
}
}
}
int main() {
struct student stu[N], *p = stu;
int i;
printf("请输入姓名,学号,分数:\n");
for (i = 0; i < N; i++)
scanf("%s%ld%f", stu[i].name, &stu[i].num, &stu[i].score);
sort(p);
printf("按照分数从高到底如下:\n姓名\t学号\t分数\n");
for (i = 0; i < N; i++)
printf("%-8s%-8ld%-8f\n", stu[i].name, stu[i].num, stu[i].score);
return 0;
}