利用C语言为宿舍管理人员编写一个宿舍管理查询软件。 【基本要求】 (1)采用交互工作方式;(2)按关键字(姓名、学号、房号)进行排序(冒泡、选择、插入排序等任选一种)。(3)建立查询菜单: (用二分查找实现以下操作) 按姓名查询 \按学号查询 \按房号查询 打印任一查询结果(可以连续操作)
时间: 2023-12-23 19:03:10 浏览: 24
很高兴能为您提供帮助,下面是完整的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
char id[10];
int room_number;
} Student;
void bubble_sort(Student students[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(students[j].name, students[j + 1].name) > 0) {
Student tmp = students[j];
students[j] = students[j + 1];
students[j + 1] = tmp;
}
}
}
}
int binary_search(Student students[], int n, char* key, int search_by) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (search_by == 1) {
if (strcmp(students[mid].name, key) == 0) {
return mid;
} else if (strcmp(students[mid].name, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if (search_by == 2) {
if (strcmp(students[mid].id, key) == 0) {
return mid;
} else if (strcmp(students[mid].id, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if (search_by == 3) {
if (students[mid].room_number == atoi(key)) {
return mid;
} else if (students[mid].room_number < atoi(key)) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
void query_menu(Student students[], int n) {
while (1) {
printf("\n----------查询菜单----------\n");
printf("1.按姓名查询\n");
printf("2.按学号查询\n");
printf("3.按房号查询\n");
printf("4.退出查询\n");
printf("请输入选项:");
int option;
scanf("%d", &option);
if (option == 4) {
break;
}
printf("请输入查询关键字:");
char key[20];
scanf("%s", key);
int index = binary_search(students, n, key, option);
if (index == -1) {
printf("未找到相关信息!\n");
} else {
printf("姓名:%s,学号:%s,房号:%d\n", students[index].name, students[index].id, students[index].room_number);
}
}
}
int main() {
printf("请输入学生人数:");
int n;
scanf("%d", &n);
Student students[n];
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", students[i].name);
printf("请输入第%d个学生的学号:", i + 1);
scanf("%s", students[i].id);
printf("请输入第%d个学生的房号:", i + 1);
scanf("%d", &students[i].room_number);
}
bubble_sort(students, n);
query_menu(students, n);
return 0;
}
```
在运行程序时,首先需要输入学生的人数,然后依次输入每个学生的姓名、学号和房号。程序会自动按照姓名进行排序,并进入查询菜单,可以按照姓名、学号或房号进行查询,直到退出程序。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](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://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)