用c语言编写,有n个同学的信息(包括学号,姓名,成绩)要求按照成绩从高到低排序输出各个同学信息(尽可能容易读懂))
时间: 2024-01-20 18:04:36 浏览: 47
好的,这个问题很简单,以下是一个基于C语言的实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char name[20];
float score;
} Student;
int compare(const void *a, const void *b) {
return ((Student *)b)->score - ((Student *)a)->score;
}
int main() {
int n;
printf("请输入学生人数:");
scanf("%d", &n);
Student *students = (Student *)malloc(n * sizeof(Student));
printf("请分别输入每个学生的学号、姓名和成绩,用空格隔开:\n");
for (int i = 0; i < n; i++) {
scanf("%d%s%f", &students[i].id, students[i].name, &students[i].score);
}
qsort(students, n, sizeof(Student), compare);
printf("按成绩从高到低排序后的学生信息:\n");
printf("学号\t姓名\t成绩\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%.2f\n", students[i].id, students[i].name, students[i].score);
}
free(students);
return 0;
}
```
这个程序定义了一个 `Student` 结构体来存储每个学生的信息,然后通过 `qsort` 函数对学生数组进行排序,最后输出排序后的学生信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)