创建一个较为复杂的c语言的酒店管理系统代码
时间: 2023-06-13 11:08:37 浏览: 116
大一原创C语言学生信息管理系统1.0项目源代码压缩包
以下是一个简单的C语言酒店管理系统代码,包含了房间预定、入住、退房等功能。请注意,这只是一个简单的示例,实际的酒店管理系统可能需要更多的功能和数据结构。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROOMS 100 // 最大房间数
// 房间结构体
typedef struct {
int room_number; // 房间号
int is_booked; // 是否已预定
int is_occupied; // 是否已入住
char guest_name[50]; // 入住客人姓名
} Room;
// 初始化所有房间为未预定和未入住状态
void init_rooms(Room rooms[]) {
for (int i = 0; i < MAX_ROOMS; i++) {
rooms[i].room_number = i + 1;
rooms[i].is_booked = 0;
rooms[i].is_occupied = 0;
strcpy(rooms[i].guest_name, "");
}
}
// 显示所有房间的状态
void show_room_status(Room rooms[]) {
printf("房间号\t状态\t\t客人姓名\n");
for (int i = 0; i < MAX_ROOMS; i++) {
printf("%d\t", rooms[i].room_number);
if (rooms[i].is_booked) {
printf("已预定\t\t");
} else if (rooms[i].is_occupied) {
printf("已入住\t\t%s\n", rooms[i].guest_name);
} else {
printf("未预定未入住\n");
}
}
}
// 预定房间
void book_room(Room rooms[]) {
int room_number;
printf("请输入要预定的房间号:");
scanf("%d", &room_number);
if (room_number < 1 || room_number > MAX_ROOMS) {
printf("房间号无效!\n");
return;
}
if (rooms[room_number - 1].is_booked || rooms[room_number - 1].is_occupied) {
printf("该房间已被预定或入住!\n");
return;
}
rooms[room_number - 1].is_booked = 1;
printf("预定成功!\n");
}
// 入住房间
void check_in(Room rooms[]) {
int room_number;
printf("请输入要入住的房间号:");
scanf("%d", &room_number);
if (room_number < 1 || room_number > MAX_ROOMS) {
printf("房间号无效!\n");
return;
}
if (rooms[room_number - 1].is_occupied) {
printf("该房间已被入住!\n");
return;
}
if (!rooms[room_number - 1].is_booked) {
char answer;
printf("该房间未被预定,是否继续入住?(y/n)");
scanf(" %c", &answer);
if (answer != 'y') {
return;
}
}
char guest_name[50];
printf("请输入入住客人的姓名:");
scanf("%s", guest_name);
strcpy(rooms[room_number - 1].guest_name, guest_name);
rooms[room_number - 1].is_booked = 0;
rooms[room_number - 1].is_occupied = 1;
printf("入住成功!\n");
}
// 退房
void check_out(Room rooms[]) {
int room_number;
printf("请输入要退房的房间号:");
scanf("%d", &room_number);
if (room_number < 1 || room_number > MAX_ROOMS) {
printf("房间号无效!\n");
return;
}
if (!rooms[room_number - 1].is_occupied) {
printf("该房间未被入住!\n");
return;
}
printf("客人%s已退房,房间已清空!\n", rooms[room_number - 1].guest_name);
rooms[room_number - 1].is_occupied = 0;
strcpy(rooms[room_number - 1].guest_name, "");
}
int main() {
Room rooms[MAX_ROOMS];
init_rooms(rooms);
int choice;
while (1) {
printf("\n请选择操作:\n");
printf("1. 显示所有房间状态\n");
printf("2. 预定房间\n");
printf("3. 入住房间\n");
printf("4. 退房\n");
printf("5. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
show_room_status(rooms);
break;
case 2:
book_room(rooms);
break;
case 3:
check_in(rooms);
break;
case 4:
check_out(rooms);
break;
case 5:
exit(0);
default:
printf("无效的选项!\n");
}
}
return 0;
}
```
阅读全文