某班有10个学生,定义一个结构体数组,存放学生的编号(不同学生的编号不相同)、姓名、数学成绩,成绩保留1位小数,成绩范围[0.0,100.0]; 要求:写三个函数,分别完成 1、将输入的10学生的编号、姓名和成绩保存到结构体数组中,需要有数据合法性验证; 2、将学生按成绩由大到小进行排序,成绩相同的按学号从小到大排序; 3、按排序后的结果进行输出;
时间: 2023-04-10 15:04:57 浏览: 96
好的,我可以回答这个编程类的问题。以下是代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 10
struct student {
int id;
char name[20];
float score;
};
void input_students(struct student students[]) {
int i;
for (i = 0; i < MAX_STUDENTS; i++) {
printf("请输入第%d个学生的编号、姓名和成绩:\n", i + 1);
scanf("%d %s %f", &students[i].id, students[i].name, &students[i].score);
if (students[i].score < 0.0 || students[i].score > 100.0) {
printf("成绩不合法,请重新输入!\n");
i--;
}
}
}
void sort_students(struct student students[]) {
int i, j;
struct student temp;
for (i = 0; i < MAX_STUDENTS - 1; i++) {
for (j = 0; j < MAX_STUDENTS - i - 1; j++) {
if (students[j].score < students[j + 1].score ||
(students[j].score == students[j + 1].score && students[j].id > students[j + 1].id)) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void output_students(struct student students[]) {
int i;
printf("排序后的结果为:\n");
for (i = 0; i < MAX_STUDENTS; i++) {
printf("编号:%d,姓名:%s,成绩:%.1f\n", students[i].id, students[i].name, students[i].score);
}
}
int main() {
struct student students[MAX_STUDENTS];
input_students(students);
sort_students(students);
output_students(students);
return 0;
}
```
这个程序定义了一个结构体 `student`,包含学生的编号、姓名和成绩。然后定义了三个函数,分别完成输入学生信息、按成绩排序和输出排序结果的功能。在输入学生信息时,会对成绩进行合法性验证,如果不在 [0.0, 100.0] 范围内,则需要重新输入。在按成绩排序时,如果成绩相同,则按学号从小到大排序。最后输出排序后的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)