宿舍管理系统c语言
时间: 2023-07-03 07:07:53 浏览: 157
宿舍管理系统是一个比较复杂的系统,需要设计多个模块,包括学生信息管理、宿舍楼信息管理、宿舍房间信息管理、宿舍住宿情况管理、宿舍卫生情况管理等等。
在C语言中,可以使用结构体来定义学生信息、宿舍楼信息、宿舍房间信息等数据类型,同时也需要使用文件读写来进行数据的持久化存储。下面是一个简单宿舍管理系统的代码示例,供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 1000
#define MAX_BUILDING_NUM 20
#define MAX_ROOM_NUM 100
// 学生信息结构体
typedef struct {
char name[20]; // 姓名
char sex; // 性别:M-男,F-女
char id[20]; // 学号
int building; // 所在宿舍楼
int room; // 所在宿舍房间
} Student;
// 宿舍楼信息结构体
typedef struct {
int id; // 宿舍楼编号
char name[20]; // 宿舍楼名称
int floor_num; // 层数
int room_num_per_floor; // 每层房间数
} Building;
// 宿舍房间信息结构体
typedef struct {
int building; // 所在宿舍楼
int room; // 房间号
char type[10]; // 房间类型:single-单人间,double-双人间
int capacity; // 房间容纳人数
} Room;
// 全局变量
Student students[MAX_STUDENT_NUM]; // 学生信息数组
Building buildings[MAX_BUILDING_NUM]; // 宿舍楼信息数组
Room rooms[MAX_BUILDING_NUM][MAX_ROOM_NUM]; // 宿舍房间信息数组
int student_num = 0; // 学生总数
int building_num = 0; // 宿舍楼总数
int room_num[MAX_BUILDING_NUM] = {0}; // 每个宿舍楼的房间总数
// 读取学生信息
void read_students() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("File students.txt not found.\n");
return;
}
while (fscanf(fp, "%s %c %s %d %d", students[student_num].name, &students[student_num].sex, students[student_num].id, &students[student_num].building, &students[student_num].room) != EOF) {
student_num++;
}
fclose(fp);
}
// 保存学生信息
void save_students() {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("Failed to open file students.txt.\n");
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%s %c %s %d %d\n", students[i].name, students[i].sex, students[i].id, students[i].building, students[i].room);
}
fclose(fp);
}
// 读取宿舍楼信息
void read_buildings() {
FILE *fp = fopen("buildings.txt", "r");
if (fp == NULL) {
printf("File buildings.txt not found.\n");
return;
}
while (fscanf(fp, "%d %s %d %d", &buildings[building_num].id, buildings[building_num].name, &buildings[building_num].floor_num, &buildings[building_num].room_num_per_floor) != EOF) {
building_num++;
}
fclose(fp);
}
// 保存宿舍楼信息
void save_buildings() {
FILE *fp = fopen("buildings.txt", "w");
if (fp == NULL) {
printf("Failed to open file buildings.txt.\n");
return;
}
for (int i = 0; i < building_num; i++) {
fprintf(fp, "%d %s %d %d\n", buildings[i].id, buildings[i].name, buildings[i].floor_num, buildings[i].room_num_per_floor);
}
fclose(fp);
}
// 读取宿舍房间信息
void read_rooms() {
FILE *fp = fopen("rooms.txt", "r");
if (fp == NULL) {
printf("File rooms.txt not found.\n");
return;
}
int building_id, room_id;
char room_type[10];
int room_capacity;
while (fscanf(fp, "%d %d %s %d", &building_id, &room_id, room_type, &room_capacity) != EOF) {
Room *room = &rooms[building_id][room_id];
room->building = building_id;
room->room = room_id;
strcpy(room->type, room_type);
room->capacity = room_capacity;
room_num[building_id]++;
}
fclose(fp);
}
// 保存宿舍房间信息
void save_rooms() {
FILE *fp = fopen("rooms.txt", "w");
if (fp == NULL) {
printf("Failed to open file rooms.txt.\n");
return;
}
for (int i = 0; i < building_num; i++) {
for (int j = 0; j < room_num[i]; j++) {
Room *room = &rooms[i][j];
fprintf(fp, "%d %d %s %d\n", room->building, room->room, room->type, room->capacity);
}
}
fclose(fp);
}
// 添加学生
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("Maximum student number reached.\n");
return;
}
printf("Please input student name:\n");
scanf("%s", students[student_num].name);
printf("Please input student sex (M/F):\n");
scanf(" %c", &students[student_num].sex);
printf("Please input student id:\n");
scanf("%s", students[student_num].id);
printf("Please input building number:\n");
scanf("%d", &students[student_num].building);
printf("Please input room number:\n");
scanf("%d", &students[student_num].room);
student_num++;
save_students();
printf("Student added successfully.\n");
}
// 删除学生
void delete_student() {
char id[20];
printf("Please input student id:\n");
scanf("%s", id);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
found = 1;
for (int j = i + 1; j < student_num; j++) {
students[j - 1] = students[j];
}
student_num--;
break;
}
}
if (found) {
save_students();
printf("Student deleted successfully.\n");
} else {
printf("Student not found.\n");
}
}
// 显示学生信息
void show_students() {
printf("Total students: %d\n", student_num);
printf("Name\tSex\tID\tBuilding\tRoom\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%c\t%s\t%d\t%d\n", students[i].name, students[i].sex, students[i].id, students[i].building, students[i].room);
}
}
// 添加宿舍楼
void add_building() {
if (building_num >= MAX_BUILDING_NUM) {
printf("Maximum building number reached.\n");
return;
}
printf("Please input building id:\n");
scanf("%d", &buildings[building_num].id);
printf("Please input building name:\n");
scanf("%s", buildings[building_num].name);
printf("Please input floor number:\n");
scanf("%d", &buildings[building_num].floor_num);
printf("Please input room number per floor:\n");
scanf("%d", &buildings[building_num].room_num_per_floor);
building_num++;
save_buildings();
printf("Building added successfully.\n");
}
// 删除宿舍楼
void delete_building() {
int id;
printf("Please input building id:\n");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < building_num; i++) {
if (buildings[i].id == id) {
found = 1;
for (int j = i + 1; j < building_num; j++) {
buildings[j - 1] = buildings[j];
}
building_num--;
break;
}
}
if (found) {
save_buildings();
printf("Building deleted successfully.\n");
} else {
printf("Building not found.\n");
}
}
// 显示宿舍楼信息
void show_buildings() {
printf("Total buildings: %d\n", building_num);
printf("ID\tName\tFloor Num\tRoom Num per Floor\n");
for (int i = 0; i < building_num; i++) {
printf("%d\t%s\t%d\t\t%d\n", buildings[i].id, buildings[i].name, buildings[i].floor_num, buildings[i].room_num_per_floor);
}
}
// 添加宿舍房间
void add_room() {
int building_id;
printf("Please input building id:\n");
scanf("%d", &building_id);
if (room_num[building_id] >= MAX_ROOM_NUM) {
printf("Maximum room number reached.\n");
return;
}
Room *room = &rooms[building_id][room_num[building_id]];
room->building = building_id;
printf("Please input room id:\n");
scanf("%d", &room->room);
printf("Please input room type (single/double):\n");
scanf("%s", room->type);
printf("Please input room capacity:\n");
scanf("%d", &room->capacity);
room_num[building_id]++;
save_rooms();
printf("Room added successfully.\n");
}
// 删除宿舍房间
void delete_room() {
int building_id, room_id;
printf("Please input building id:\n");
scanf("%d", &building_id);
printf("Please input room id:\n");
scanf("%d", &room_id);
int found = 0;
for (int i = 0; i < room_num[building_id]; i++) {
Room *room = &rooms[building_id][i];
if (room->room == room_id) {
found = 1;
for (int j = i + 1; j < room_num[building_id]; j++) {
rooms[building_id][j - 1] = rooms[building_id][j];
}
room_num[building_id]--;
break;
}
}
if (found) {
save_rooms();
printf("Room deleted successfully.\n");
} else {
printf("Room not found.\n");
}
}
// 显示宿舍房间信息
void show_rooms() {
printf("Building\tRoom\tType\tCapacity\n");
for (int i = 0; i < building_num; i++) {
for (int j = 0; j < room_num[i]; j++) {
Room *room = &rooms[i][j];
printf("%d\t\t%d\t%s\t%d\n", room->building, room->room, room->type, room->capacity);
}
}
}
int main() {
read_students();
read_buildings();
read_rooms();
while (1) {
printf("Please select the operation:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Show students\n");
printf("4. Add building\n");
printf("5. Delete building\n");
printf("6. Show buildings\n");
printf("7. Add room\n");
printf("8. Delete room\n");
printf("9. Show rooms\n");
printf("0. Exit\n");
int option;
scanf("%d", &option);
switch (option) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
show_students();
break;
case 4:
add_building();
break;
case 5:
delete_building();
break;
case 6:
show_buildings();
break;
case 7:
add_room();
break;
case 8:
delete_room();
break;
case 9:
show_rooms();
break;
case 0:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid option.\n");
}
}
return 0;
}
```
以上代码只是一个简单的示例,实际情况中需要根据具体需求进行更改和扩展。
阅读全文