用C语言实现一个宿舍管理系统代码
时间: 2024-02-29 15:52:34 浏览: 20
以下是一个稍微完善一些的宿舍管理系统的C代码示例,其中包括添加、删除、修改、查询宿舍信息和分配床位的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DORM_NUM 100
#define MAX_STUDENT_NUM 1000
struct dormitory {
int id; // 宿舍号
int capacity; // 容纳人数
int occupied; // 已住人数
int beds[MAX_STUDENT_NUM]; // 床位列表
};
struct dormitory dorms[MAX_DORM_NUM];
int dorm_num = 0;
struct student {
char name[20]; // 姓名
int dorm_id; // 所住宿舍号
int bed_num; // 床位号
};
struct student students[MAX_STUDENT_NUM];
int student_num = 0;
void add_dorm() {
int id, capacity;
printf("请输入宿舍号:");
scanf("%d", &id);
printf("请输入宿舍容纳人数:");
scanf("%d", &capacity);
dorms[dorm_num].id = id;
dorms[dorm_num].capacity = capacity;
dorms[dorm_num].occupied = 0;
memset(dorms[dorm_num].beds, 0, sizeof(dorms[dorm_num].beds));
dorm_num++;
printf("宿舍添加成功!\n");
}
void delete_dorm() {
int id, i, j;
printf("请输入要删除的宿舍号:");
scanf("%d", &id);
for (i = 0; i < dorm_num; i++) {
if (dorms[i].id == id) {
for (j = 0; j < student_num; j++) {
if (students[j].dorm_id == id) {
students[j].dorm_id = 0;
students[j].bed_num = 0;
}
}
dorms[i] = dorms[dorm_num - 1];
dorm_num--;
printf("宿舍删除成功!\n");
return;
}
}
printf("宿舍不存在!\n");
}
void modify_dorm() {
int id, i;
printf("请输入要修改的宿舍号:");
scanf("%d", &id);
for (i = 0; i < dorm_num; i++) {
if (dorms[i].id == id) {
printf("请输入修改后的容纳人数:");
scanf("%d", &dorms[i].capacity);
printf("宿舍信息修改成功!\n");
return;
}
}
printf("宿舍不存在!\n");
}
void query_dorm() {
int id, i;
printf("请输入要查询的宿舍号:");
scanf("%d", &id);
for (i = 0; i < dorm_num; i++) {
if (dorms[i].id == id) {
printf("宿舍号:%d,容纳人数:%d,已住人数:%d\n", dorms[i].id, dorms[i].capacity, dorms[i].occupied);
int j;
printf("床位列表:");
for (j = 0; j < dorms[i].capacity; j++) {
if (dorms[i].beds[j]) {
printf("%d ", j+1);
} else {
printf("空 ");
}
}
printf("\n");
return;
}
}
printf("宿舍不存在!\n");
}
void assign_bed() {
char name[20];
int dorm_id, bed_num, i, j;
printf("请输入学生姓名:");
scanf("%s", name);
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("该学生已分配床位!\n");
return;
}
}
printf("请输入宿舍号:");
scanf("%d", &dorm_id);
for (i = 0; i < dorm_num; i++) {
if (dorms[i].id == dorm_id) {
if (dorms[i].occupied >= dorms[i].capacity) {
printf("该宿舍已满!\n");
return;
}
printf("床位列表:");
for (j = 0; j < dorms[i].capacity; j++) {
if (!dorms[i].beds[j]) {
printf("%d ", j+1);
} else {
printf("占 ");
}
}
printf("\n");
printf("请输入床位号:");
scanf("%d", &bed_num);
if (bed_num < 1 || bed_num > dorms[i].capacity || dorms[i].beds[bed_num-1]) {
printf("床位号无效!\n");
return;
}
dorms[i].beds[bed_num-1] = 1;
dorms[i].occupied++;
students[student_num].dorm_id = dorm_id;
students[student_num].bed_num = bed_num;
strcpy(students[student_num].name, name);
student_num++;
printf("分配床位成功!\n");
return;
}
}
printf("宿舍不存在!\n");
}
int main() {
int choice;
while (1) {
printf("\n宿舍管理系统\n");
printf("1. 添加宿舍\n");
printf("2. 删除宿舍\n");
printf("3. 修改宿舍信息\n");
printf("4. 查询宿舍信息\n");
printf("5. 分配床位\n");
printf("6. 退出\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_dorm();
break;
case 2:
delete_dorm();
break;
case 3:
modify_dorm();
break;
case 4:
query_dorm();
break;
case 5:
assign_bed();
break;
case 6:
exit(0);
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
```
此代码是一个相对完善的宿舍管理系统示例,您可以根据实际需求进行修改和完善。
相关推荐
![cpp](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://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)