用C语言数据结构设计一个学生宿舍管理系统设,使其具有以下功能:实现简单的学生宿舍基本信息管理,宿舍的基本信息包括楼号、房间号、面积、所容纳人数、已入住人数等。 基本要求: 1.设计简单的菜单,能够进行系统功能选择;2.实现信息的录入功能; 3.在已有信息的基础上添加新的记录; 4.删除指定房间号的记录; 5.修改指定房间号的记录; 6.实现信息的浏览功能; 7.按房间号查询功能; 8.按已入住人数排序功能
时间: 2024-03-01 08:49:44 浏览: 67
好的,下面是我的回答:
首先,我们需要定义一个结构体来存储学生宿舍的基本信息,包括楼号、房间号、面积、所容纳人数、已入住人数等字段。
```
struct Dormitory {
int building; // 楼号
int room; // 房间号
float area; // 面积
int capacity; // 所容纳人数
int occupied; // 已入住人数
};
```
接下来,我们可以定义一个数组来存储多个学生宿舍的信息。
```
#define MAX_DORMITORIES 100 // 最大宿舍数
struct Dormitory dormitories[MAX_DORMITORIES];
int num_dormitories = 0; // 当前宿舍数
```
然后,我们可以编写一个菜单函数,让用户选择不同的功能。
```
void menu() {
printf("1. 录入宿舍信息\n");
printf("2. 添加宿舍信息\n");
printf("3. 删除宿舍信息\n");
printf("4. 修改宿舍信息\n");
printf("5. 浏览宿舍信息\n");
printf("6. 按房间号查询宿舍信息\n");
printf("7. 按已入住人数排序宿舍信息\n");
printf("0. 退出\n");
printf("请选择功能:");
}
```
接下来,我们可以分别实现每个功能。
1. 录入宿舍信息
```
void add_dormitory() {
if (num_dormitories >= MAX_DORMITORIES) {
printf("宿舍数已满,无法添加!\n");
return;
}
struct Dormitory dorm;
printf("请输入宿舍楼号:");
scanf("%d", &dorm.building);
printf("请输入宿舍房间号:");
scanf("%d", &dorm.room);
printf("请输入宿舍面积:");
scanf("%f", &dorm.area);
printf("请输入宿舍所容纳人数:");
scanf("%d", &dorm.capacity);
printf("请输入宿舍已入住人数:");
scanf("%d", &dorm.occupied);
dormitories[num_dormitories++] = dorm;
printf("宿舍信息录入成功!\n");
}
```
2. 添加宿舍信息
```
void add_dormitory() {
if (num_dormitories >= MAX_DORMITORIES) {
printf("宿舍数已满,无法添加!\n");
return;
}
struct Dormitory dorm;
printf("请输入宿舍楼号:");
scanf("%d", &dorm.building);
printf("请输入宿舍房间号:");
scanf("%d", &dorm.room);
printf("请输入宿舍面积:");
scanf("%f", &dorm.area);
printf("请输入宿舍所容纳人数:");
scanf("%d", &dorm.capacity);
printf("请输入宿舍已入住人数:");
scanf("%d", &dorm.occupied);
dormitories[num_dormitories++] = dorm;
printf("宿舍信息添加成功!\n");
}
```
3. 删除宿舍信息
```
void delete_dormitory() {
int room;
printf("请输入要删除的宿舍房间号:");
scanf("%d", &room);
int found = 0;
for (int i = 0; i < num_dormitories; i++) {
if (dormitories[i].room == room) {
found = 1;
for (int j = i + 1; j < num_dormitories; j++) {
dormitories[j - 1] = dormitories[j];
}
num_dormitories--;
printf("宿舍信息删除成功!\n");
break;
}
}
if (!found) {
printf("该房间号的宿舍不存在!\n");
}
}
```
4. 修改宿舍信息
```
void modify_dormitory() {
int room;
printf("请输入要修改的宿舍房间号:");
scanf("%d", &room);
int found = 0;
for (int i = 0; i < num_dormitories; i++) {
if (dormitories[i].room == room) {
found = 1;
printf("请输入新的宿舍楼号:");
scanf("%d", &dormitories[i].building);
printf("请输入新的宿舍面积:");
scanf("%f", &dormitories[i].area);
printf("请输入新的宿舍所容纳人数:");
scanf("%d", &dormitories[i].capacity);
printf("请输入新的宿舍已入住人数:");
scanf("%d", &dormitories[i].occupied);
printf("宿舍信息修改成功!\n");
break;
}
}
if (!found) {
printf("该房间号的宿舍不存在!\n");
}
}
```
5. 浏览宿舍信息
```
void view_dormitories() {
printf("宿舍楼号\t房间号\t面积\t所容纳人数\t已入住人数\n");
for (int i = 0; i < num_dormitories; i++) {
printf("%d\t\t%d\t%.2f\t%d\t\t%d\n", dormitories[i].building, dormitories[i].room, dormitories[i].area, dormitories[i].capacity, dormitories[i].occupied);
}
}
```
6. 按房间号查询宿舍信息
```
void search_dormitory() {
int room;
printf("请输入要查询的宿舍房间号:");
scanf("%d", &room);
int found = 0;
for (int i = 0; i < num_dormitories; i++) {
if (dormitories[i].room == room) {
found = 1;
printf("宿舍楼号\t房间号\t面积\t所容纳人数\t已入住人数\n");
printf("%d\t\t%d\t%.2f\t%d\t\t%d\n", dormitories[i].building, dormitories[i].room, dormitories[i].area, dormitories[i].capacity, dormitories[i].occupied);
break;
}
}
if (!found) {
printf("该房间号的宿舍不存在!\n");
}
}
```
7. 按已入住人数排序宿舍信息
```
void sort_dormitories() {
for (int i = 0; i < num_dormitories - 1; i++) {
for (int j = 0; j < num_dormitories - i - 1; j++) {
if (dormitories[j].occupied > dormitories[j + 1].occupied) {
struct Dormitory temp = dormitories[j];
dormitories[j] = dormitories[j + 1];
dormitories[j + 1] = temp;
}
}
}
printf("宿舍楼号\t房间号\t面积\t所容纳人数\t已入住人数\n");
for (int i = 0; i < num_dormitories; i++) {
printf("%d\t\t%d\t%.2f\t%d\t\t%d\n", dormitories[i].building, dormitories[i].room, dormitories[i].area, dormitories[i].capacity, dormitories[i].occupied);
}
}
```
最后,我们可以在主函数中循环调用菜单函数和对应的功能函数。
```
int main() {
int choice;
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_dormitory();
break;
case 2:
add_dormitory();
break;
case 3:
delete_dormitory();
break;
case 4:
modify_dormitory();
break;
case 5:
view_dormitories();
break;
case 6:
search_dormitory();
break;
case 7:
sort_dormitories();
break;
case 0:
printf("谢谢使用!\n");
break;
default:
printf("无效的选择,请重新选择!\n");
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文