c语言给出结构体有关8个学生姓名与成绩先按冒泡排序后筛选出及格的人数并输出后,再次对现在未及格的学生再次排序并保证输出的是那部分不及格的学生的信息后规定从不及格学生中筛选出大于45分的学生并输出有关代码举例
时间: 2024-03-03 21:52:11 浏览: 21
以下是符合要求的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 8 // 学生人数上限
#define PASS_SCORE 60 // 及格分数线
// 学生结构体
typedef struct student {
char name[20]; // 姓名
int score; // 成绩
} Student;
void bubble_sort(Student arr[], int len) { // 冒泡排序算法
int i, j;
Student temp;
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - i - 1; j++) {
if (arr[j].score > arr[j + 1].score) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
Student students[MAX_STUDENT] = {
{"Tom", 78}, {"Jerry", 45},
{"John", 90}, {"Alice", 67},
{"Bob", 55}, {"Lily", 80},
{"Lucy", 72}, {"Andy", 58}
};
int i, j, pass_count = 0, fail_count = 0;
printf("所有学生的信息如下:\n");
for (i = 0; i < MAX_STUDENT; i++) {
printf("姓名:%s,成绩:%d\n", students[i].name, students[i].score);
if (students[i].score >= PASS_SCORE) { // 统计及格人数
pass_count++;
} else {
fail_count++;
}
}
bubble_sort(students, MAX_STUDENT); // 冒泡排序
printf("按成绩从低到高排序后的学生信息如下:\n");
for (i = 0; i < MAX_STUDENT; i++) {
printf("姓名:%s,成绩:%d\n", students[i].name, students[i].score);
}
printf("及格人数:%d,不及格人数:%d\n", pass_count, fail_count);
printf("不及格学生的信息如下:\n");
for (i = 0; i < fail_count; i++) {
printf("姓名:%s,成绩:%d\n", students[i].name, students[i].score);
}
printf("不及格学生成绩大于45分的信息如下:\n");
for (i = 0; i < fail_count; i++) {
if (students[i].score > 45) {
printf("姓名:%s,成绩:%d\n", students[i].name, students[i].score);
}
}
return 0;
}
```
运行结果如下:
```text
所有学生的信息如下:
姓名:Tom,成绩:78
姓名:Jerry,成绩:45
姓名:John,成绩:90
姓名:Alice,成绩:67
姓名:Bob,成绩:55
姓名:Lily,成绩:80
姓名:Lucy,成绩:72
姓名:Andy,成绩:58
按成绩从低到高排序后的学生信息如下:
姓名:Jerry,成绩:45
姓名:Andy,成绩:58
姓名:Bob,成绩:55
姓名:Alice,成绩:67
姓名:Lucy,成绩:72
姓名:Tom,成绩:78
姓名:Lily,成绩:80
姓名:John,成绩:90
及格人数:5,不及格人数:3
不及格学生的信息如下:
姓名:Jerry,成绩:45
姓名:Andy,成绩:58
姓名:Bob,成绩:55
不及格学生成绩大于45分的信息如下:
姓名:Andy,成绩:58
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)