用c语言写一个学生宿舍管理系统,要求功能:实现简单的学生宿舍基本信息管理,宿舍的基本信息包括楼号、房间号、面积、所容纳人数、已入住人数等。 1.设计简单的菜单,能够进行系统功能选择; 2.实现信息的录入功能; 3.在已有信息的基础上添加新的记录; 4.删除指定房间号的记录; . 5.修改指定房间号的记录; 6.实现信息的浏览功能; 7.按房间号查询功能; 8.按已入住人数排序功能;
时间: 2024-02-11 07:06:57 浏览: 84
好的,这是一个比较复杂的问题,我会尽可能地回答你。
首先,为了实现这个学生宿舍管理系统,我们需要设计一个数据结构来存储宿舍的基本信息,例如楼号、房间号、面积、所容纳人数、已入住人数等。以下是一个可能的数据结构示例:
```
struct dormitory {
int building_number; // 楼号
int room_number; // 房间号
float area; // 面积
int total_capacity; // 所容纳人数
int occupied_capacity; // 已入住人数
};
```
接下来,我们需要设计一个菜单来实现系统功能的选择。以下是一个可能的菜单示例:
```
void display_menu(void) {
printf("1. 录入宿舍信息\n");
printf("2. 添加宿舍信息\n");
printf("3. 删除宿舍信息\n");
printf("4. 修改宿舍信息\n");
printf("5. 浏览宿舍信息\n");
printf("6. 按房间号查询宿舍信息\n");
printf("7. 按已入住人数排序宿舍信息\n");
printf("8. 退出程序\n");
}
```
接下来,我们需要实现每个菜单选项的功能:
1. 录入宿舍信息
```
void input_dormitory_info(struct dormitory *d) {
printf("请输入宿舍信息:\n");
printf("楼号:");
scanf("%d", &d->building_number);
printf("房间号:");
scanf("%d", &d->room_number);
printf("面积:");
scanf("%f", &d->area);
printf("所容纳人数:");
scanf("%d", &d->total_capacity);
printf("已入住人数:");
scanf("%d", &d->occupied_capacity);
}
```
2. 添加宿舍信息
```
void add_dormitory_info(struct dormitory *dormitories, int *count) {
input_dormitory_info(&dormitories[*count]);
(*count)++;
}
```
3. 删除宿舍信息
```
void delete_dormitory_info(struct dormitory *dormitories, int *count, int room_number) {
int index = find_dormitory_by_room_number(dormitories, *count, room_number);
if (index == -1) {
printf("未找到该房间号的宿舍信息!\n");
return;
}
for (int i = index; i < *count - 1; i++) {
dormitories[i] = dormitories[i + 1];
}
(*count)--;
}
```
4. 修改宿舍信息
```
void modify_dormitory_info(struct dormitory *dormitories, int count, int room_number) {
int index = find_dormitory_by_room_number(dormitories, count, room_number);
if (index == -1) {
printf("未找到该房间号的宿舍信息!\n");
return;
}
input_dormitory_info(&dormitories[index]);
}
```
5. 浏览宿舍信息
```
void display_all_dormitory_info(struct dormitory *dormitories, int count) {
printf("楼号\t房间号\t面积\t所容纳人数\t已入住人数\n");
for (int i = 0; i < count; i++) {
printf("%d\t%d\t%f\t%d\t\t%d\n", dormitories[i].building_number, dormitories[i].room_number, dormitories[i].area, dormitories[i].total_capacity, dormitories[i].occupied_capacity);
}
}
```
6. 按房间号查询宿舍信息
```
void display_dormitory_info_by_room_number(struct dormitory *dormitories, int count, int room_number) {
int index = find_dormitory_by_room_number(dormitories, count, room_number);
if (index == -1) {
printf("未找到该房间号的宿舍信息!\n");
return;
}
printf("楼号\t房间号\t面积\t所容纳人数\t已入住人数\n");
printf("%d\t%d\t%f\t%d\t\t%d\n", dormitories[index].building_number, dormitories[index].room_number, dormitories[index].area, dormitories[index].total_capacity, dormitories[index].occupied_capacity);
}
int find_dormitory_by_room_number(struct dormitory *dormitories, int count, int room_number) {
for (int i = 0; i < count; i++) {
if (dormitories[i].room_number == room_number) {
return i;
}
}
return -1;
}
```
7. 按已入住人数排序宿舍信息
```
void sort_dormitory_info_by_occupied_capacity(struct dormitory *dormitories, int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (dormitories[j].occupied_capacity > dormitories[j + 1].occupied_capacity) {
struct dormitory temp = dormitories[j];
dormitories[j] = dormitories[j + 1];
dormitories[j + 1] = temp;
}
}
}
}
```
最后,我们可以在主函数中调用菜单和对应的函数来实现整个程序的功能:
```
int main(void) {
struct dormitory dormitories[MAX_DORMITORY_COUNT];
int count = 0;
while (1) {
display_menu();
int choice;
printf("请选择功能:");
scanf("%d", &choice);
switch (choice) {
case 1: // 录入宿舍信息
add_dormitory_info(dormitories, &count);
break;
case 2: // 添加宿舍信息
add_dormitory_info(dormitories, &count);
break;
case 3: // 删除宿舍信息
int room_number;
printf("请输入要删除的房间号:");
scanf("%d", &room_number);
delete_dormitory_info(dormitories, &count, room_number);
break;
case 4: // 修改宿舍信息
printf("请输入要修改的房间号:");
scanf("%d", &room_number);
modify_dormitory_info(dormitories, count, room_number);
break;
case 5: // 浏览宿舍信息
display_all_dormitory_info(dormitories, count);
break;
case 6: // 按房间号查询宿舍信息
printf("请输入要查询的房间号:");
scanf("%d", &room_number);
display_dormitory_info_by_room_number(dormitories, count, room_number);
break;
case 7: // 按已入住人数排序宿舍信息
sort_dormitory_info_by_occupied_capacity(dormitories, count);
display_all_dormitory_info(dormitories, count);
break;
case 8: // 退出程序
return 0;
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
```
希望这个答案能帮助到你!
阅读全文