c语言实现宾馆客房管理
时间: 2024-09-11 11:07:32 浏览: 39
在 C 语言中实现宾馆客房管理系统通常会涉及结构体、数组、文件操作以及函数的使用。下面是一个简单的示例,描述了如何设计这个系统的基本框架:
1. 定义客房结构体 (Room):
```c
typedef struct {
int room_number;
char guest_name[50];
bool is_booked;
} Room;
```
2. 动态创建客房数组 (HotelRooms) 和分配内存:
```c
Room *HotelRooms = NULL;
int num_of_rooms;
```
3. 宾馆房间操作函数:
- 预订房间:
```c
void book_room(Room **rooms, int room_number) {
// 检查房间是否可用
if (!(*rooms)[room_number - 1].is_booked) {
(*rooms)[room_number - 1].is_booked = true;
printf("Room %d booked by %s.\n", room_number, (*rooms)[room_number - 1].guest_name);
} else {
printf("Room %d already booked.\n", room_number);
}
}
```
- 解除预订:
```c
void cancel_reservation(Room **rooms, int room_number) {
if ((*rooms)[room_number - 1].is_booked) {
(*rooms)[room_number - 1].is_booked = false;
printf("Room %d reservation canceled.\n", room_number);
} else {
printf("No reservation found for Room %d.\n", room_number);
}
}
```
4. 文件操作,如读取/保存房间信息:
```c
void save_rooms_to_file(Room *rooms, int num_of_rooms) {
FILE* file = fopen("hotel_rooms.txt", "w");
for (int i = 0; i < num_of_rooms; i++) {
fprintf(file, "%d - %s - %s\n", rooms[i].room_number, rooms[i].guest_name, rooms[i].is_booked ? "Booked" : "Available");
}
fclose(file);
}
void load_rooms_from_file(Room **rooms, int *num_of_rooms) {
FILE* file = fopen("hotel_rooms.txt", "r");
while (fgetsRooms从file中读取数据并更新结构体数组...
fclose(file);
}
```
阅读全文