用结构体struct变量,设计一个学生期末成绩查询程序。 要求: 1.输入学生姓名或者学号查找对应期末成绩。 2.按照分数高低将所有同学成绩依次输出。 3.应有简易菜单提示...
时间: 2024-03-07 19:50:05 浏览: 26
好的,以下是一个简单的学生期末成绩查询程序的代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char name[20];
int id;
int score;
};
int main() {
struct Student students[MAX_STUDENTS];
int num_students = 0;
// 添加学生信息
strcpy(students[0].name, "张三");
students[0].id = 1001;
students[0].score = 85;
strcpy(students[1].name, "李四");
students[1].id = 1002;
students[1].score = 92;
strcpy(students[2].name, "王五");
students[2].id = 1003;
students[2].score = 78;
num_students = 3;
// 显示菜单
printf("请选择操作:\n");
printf("1. 按姓名查询\n");
printf("2. 按学号查询\n");
printf("3. 按分数排序\n");
int choice;
scanf("%d", &choice);
// 按姓名查询
if (choice == 1) {
char name[20];
printf("请输入学生姓名:");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学生学号:%d,学生成绩:%d\n", students[i].id, students[i].score);
break;
}
}
}
// 按学号查询
else if (choice == 2) {
int id;
printf("请输入学生学号:");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("学生姓名:%s,学生成绩:%d\n", students[i].name, students[i].score);
break;
}
}
}
// 按分数排序
else if (choice == 3) {
// 冒泡排序
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
if (students[j].score < students[j+1].score) {
struct Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
// 输出排序后的结果
for (int i = 0; i < num_students; i++) {
printf("学生姓名:%s,学号:%d,学生成绩:%d\n", students[i].name, students[i].id, students[i].score);
}
}
else {
printf("无效的操作!\n");
}
return 0;
}
```
这个程序使用了结构体来保存每个学生的姓名、学号和成绩。程序首先添加了三个学生的信息,在菜单中提供了三个选项:按姓名查询、按学号查询、按分数排序。根据用户的选择,程序会执行相应的操作。
在按姓名查询和按学号查询时,程序会遍历保存的学生信息,找到对应的学生信息并输出。在按分数排序时,程序使用了冒泡排序算法将学生信息按成绩从高到低排序,然后一一输出。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](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://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)