设计一个算法并用c语言写出一个为宿舍管理人员的一个宿舍管理查询软件, 要求: A.采用交互工作方式 B.建立数据文件,数据文件按关键字(姓名、学号、房号)进行排序(冒泡、选择、插入排序等任选一种) ,查询菜单: (用二分查找实现以下操作) A.按姓名查询 B.按学号查询 C.按房号查询 ,打印任一查询结果(可以连续操作)
时间: 2024-03-15 13:46:01 浏览: 62
好的,这个题目比较综合,我们可以分步实现。首先需要设计数据结构,然后实现排序算法,最后实现查询功能。
数据结构设计:
我们可以设计一个结构体来存储每一个宿舍的信息,包括姓名、学号、房号等。然后我们再用一个数组来存储所有宿舍的信息。
```c
#define MAX_LENGTH 100
struct dormitory {
char name[MAX_LENGTH];
char id[MAX_LENGTH];
char room_number[MAX_LENGTH];
};
struct dormitory all_dormitories[MAX_LENGTH];
```
排序算法实现:
这里我们选择冒泡排序算法,按照宿舍的姓名、学号、房号进行排序。
```c
void bubble_sort(struct dormitory arr[], int n, int sort_by) {
int i, j;
struct dormitory temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (sort_by == 1) {
if (strcmp(arr[j].name, arr[j+1].name) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
} else if (sort_by == 2) {
if (strcmp(arr[j].id, arr[j+1].id) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
} else if (sort_by == 3) {
if (strcmp(arr[j].room_number, arr[j+1].room_number) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
```
查询功能实现:
这里我们选择二分查找算法,按照姓名、学号、房号进行查询。
```c
int binary_search(struct dormitory arr[], int n, char key[], int search_by) {
int left = 0, right = n-1, mid;
while (left <= right) {
mid = (left + right) / 2;
if (search_by == 1) {
if (strcmp(arr[mid].name, key) == 0) {
return mid;
} else if (strcmp(arr[mid].name, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if (search_by == 2) {
if (strcmp(arr[mid].id, key) == 0) {
return mid;
} else if (strcmp(arr[mid].id, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if (search_by == 3) {
if (strcmp(arr[mid].room_number, key) == 0) {
return mid;
} else if (strcmp(arr[mid].room_number, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
```
完整代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
struct dormitory {
char name[MAX_LENGTH];
char id[MAX_LENGTH];
char room_number[MAX_LENGTH];
};
struct dormitory all_dormitories[MAX_LENGTH];
void bubble_sort(struct dormitory arr[], int n, int sort_by) {
int i, j;
struct dormitory temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (sort_by == 1) {
if (strcmp(arr[j].name, arr[j+1].name) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
} else if (sort_by == 2) {
if (strcmp(arr[j].id, arr[j+1].id) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
} else if (sort_by == 3) {
if (strcmp(arr[j].room_number, arr[j+1].room_number) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
int binary_search(struct dormitory arr[], int n, char key[], int search_by) {
int left = 0, right = n-1, mid;
while (left <= right) {
mid = (left + right) / 2;
if (search_by == 1) {
if (strcmp(arr[mid].name, key) == 0) {
return mid;
} else if (strcmp(arr[mid].name, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if (search_by == 2) {
if (strcmp(arr[mid].id, key) == 0) {
return mid;
} else if (strcmp(arr[mid].id, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if (search_by == 3) {
if (strcmp(arr[mid].room_number, key) == 0) {
return mid;
} else if (strcmp(arr[mid].room_number, key) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
int main() {
int n, i, sort_by, search_by, result_index;
char key[MAX_LENGTH];
printf("请输入宿舍数量:");
scanf("%d", &n);
printf("请依次输入每个宿舍的信息(姓名、学号、房号):\n");
for (i = 0; i < n; i++) {
scanf("%s %s %s", all_dormitories[i].name, all_dormitories[i].id, all_dormitories[i].room_number);
}
printf("请选择排序方式:\n");
printf("1.按姓名排序\n");
printf("2.按学号排序\n");
printf("3.按房号排序\n");
scanf("%d", &sort_by);
bubble_sort(all_dormitories, n, sort_by);
printf("排序后的宿舍信息如下:\n");
for (i = 0; i < n; i++) {
printf("%s %s %s\n", all_dormitories[i].name, all_dormitories[i].id, all_dormitories[i].room_number);
}
while (1) {
printf("请选择查询方式:\n");
printf("1.按姓名查询\n");
printf("2.按学号查询\n");
printf("3.按房号查询\n");
scanf("%d", &search_by);
printf("请输入要查询的关键字:");
scanf("%s", key);
result_index = binary_search(all_dormitories, n, key, search_by);
if (result_index == -1) {
printf("没有找到匹配结果。\n");
} else {
printf("查询结果如下:\n");
printf("%s %s %s\n", all_dormitories[result_index].name, all_dormitories[result_index].id, all_dormitories[result_index].room_number);
}
printf("是否继续查询?(1-是,0-否)");
scanf("%d", &i);
if (i == 0) {
break;
}
}
return 0;
}
```
阅读全文