编写C语言程序:输入5个学生的信息(包括学号、姓名、成绩),要求按照成绩的高低顺序输出各学生的信息。要求用结构体数组定义学生的信息。
时间: 2024-03-15 20:44:06 浏览: 142
好的,以下是符合您要求的 C 语言程序:
```c
#include <stdio.h>
#include <string.h>
struct student {
int id;
char name[20];
float score;
};
int main() {
struct student stu[5];
int i, j;
struct student temp;
printf("请输入五个学生的信息:\n");
for (i = 0; i < 5; i++) {
printf("学生 %d:\n", i + 1);
printf("学号:");
scanf("%d", &stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("成绩:");
scanf("%f", &stu[i].score);
}
// 按成绩从高到低排序
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (stu[i].score < stu[j].score) {
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
// 输出排序后的学生信息
printf("按成绩从高到低输出各学生的信息:\n");
for (i = 0; i < 5; i++) {
printf("学生 %d:\n", i + 1);
printf("学号:%d\n", stu[i].id);
printf("姓名:%s\n", stu[i].name);
printf("成绩:%.2f\n", stu[i].score);
}
return 0;
}
```
程序中定义了一个结构体 `student`,包括学号、姓名、成绩三个成员变量。然后定义了一个结构体数组 `stu`,用来存储五个学生的信息。接着通过 for 循环输入每个学生的信息,并在输入时将其存储到相应的结构体变量中。
接下来是排序部分,这里使用了冒泡排序算法。按成绩从高到低排序后,再用一个 for 循环逐个输出每个学生的信息即可。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![asm](https://img-home.csdnimg.cn/images/20250102104920.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)