c语言酒店管理系统代码:登记入住信息,查询客房类型,查询客房信息 ,修改客房信息,删除客房信息,办理退房,退出系统
时间: 2024-03-10 22:46:29 浏览: 442
C语言实现酒店管理系统
好的,以下是一个简单的酒店管理系统的 C 语言代码实现,包括登记入住信息,查询客房类型,查询客房信息,修改客房信息,删除客房信息,办理退房和退出系统功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROOMS 100
struct room {
int room_num;
char room_type[20];
float price;
int occupied;
};
struct guest {
char name[20];
int room_num;
};
struct room rooms[MAX_ROOMS];
struct guest guests[MAX_ROOMS];
int num_rooms = 0, num_guests = 0;
void load_data();
void save_data();
void check_in();
void check_out();
void display_room_types();
void display_room_info();
void modify_room_info();
void delete_room();
int main() {
int choice;
load_data();
do {
printf("1. 登记入住信息\n");
printf("2. 办理退房\n");
printf("3. 查询客房类型\n");
printf("4. 查询客房信息\n");
printf("5. 修改客房信息\n");
printf("6. 删除客房信息\n");
printf("7. 退出系统\n");
printf("请选择您要进行的操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
check_in();
break;
case 2:
check_out();
break;
case 3:
display_room_types();
break;
case 4:
display_room_info();
break;
case 5:
modify_room_info();
break;
case 6:
delete_room();
break;
case 7:
save_data();
break;
default:
printf("输入有误,请重新输入。\n");
}
} while (choice != 7);
return 0;
}
void load_data() {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("无法打开数据文件。\n");
return;
}
fscanf(fp, "%d", &num_rooms);
for (int i = 0; i < num_rooms; i++) {
struct room r;
fscanf(fp, "%d %s %f %d", &r.room_num, r.room_type, &r.price, &r.occupied);
rooms[i] = r;
}
fscanf(fp, "%d", &num_guests);
for (int i = 0; i < num_guests; i++) {
struct guest g;
fscanf(fp, "%s %d", g.name, &g.room_num);
guests[i] = g;
}
fclose(fp);
}
void save_data() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("无法打开数据文件。\n");
return;
}
fprintf(fp, "%d\n", num_rooms);
for (int i = 0; i < num_rooms; i++) {
fprintf(fp, "%d %s %f %d\n", rooms[i].room_num, rooms[i].room_type, rooms[i].price, rooms[i].occupied);
}
fprintf(fp, "%d\n", num_guests);
for (int i = 0; i < num_guests; i++) {
fprintf(fp, "%s %d\n", guests[i].name, guests[i].room_num);
}
fclose(fp);
}
void check_in() {
int room_num;
printf("请输入您要入住的客房号:");
scanf("%d", &room_num);
int room_index = -1;
for (int i = 0; i < num_rooms; i++) {
if (rooms[i].room_num == room_num) {
room_index = i;
break;
}
}
if (room_index == -1) {
printf("客房号不存在。\n");
return;
}
if (rooms[room_index].occupied) {
printf("该客房已被占用。\n");
return;
}
rooms[room_index].occupied = 1;
printf("请输入您的姓名:");
scanf("%s", guests[num_guests].name);
guests[num_guests].room_num = room_num;
num_guests++;
printf("登记成功。\n");
}
void check_out() {
int room_num;
printf("请输入您要退房的客房号:");
scanf("%d", &room_num);
int room_index = -1, guest_index = -1;
for (int i = 0; i < num_rooms; i++) {
if (rooms[i].room_num == room_num) {
room_index = i;
break;
}
}
if (room_index == -1) {
printf("客房号不存在。\n");
return;
}
if (!rooms[room_index].occupied) {
printf("该客房未被占用。\n");
return;
}
rooms[room_index].occupied = 0;
for (int i = 0; i < num_guests; i++) {
if (strcmp(guests[i].name, "") != 0 && guests[i].room_num == room_num) {
guest_index = i;
break;
}
}
if (guest_index == -1) {
printf("没有找到对应的客人信息。\n");
return;
}
strcpy(guests[guest_index].name, "");
printf("退房成功。\n");
}
void display_room_types() {
printf("客房类型列表:\n");
printf("1. 标准单人间\n");
printf("2. 标准双人间\n");
printf("3. 豪华单人间\n");
printf("4. 豪华双人间\n");
}
void display_room_info() {
int room_num;
printf("请输入您要查询的客房号:");
scanf("%d", &room_num);
int room_index = -1;
for (int i = 0; i < num_rooms; i++) {
if (rooms[i].room_num == room_num) {
room_index = i;
break;
}
}
if (room_index == -1) {
printf("客房号不存在。\n");
return;
}
printf("客房号:%d\n", rooms[room_index].room_num);
printf("客房类型:%s\n", rooms[room_index].room_type);
printf("价格:%f\n", rooms[room_index].price);
printf("是否被占用:%s\n", rooms[room_index].occupied ? "是" : "否");
}
void modify_room_info() {
int room_num;
printf("请输入您要修改的客房号:");
scanf("%d", &room_num);
int room_index = -1;
for (int i = 0; i < num_rooms; i++) {
if (rooms[i].room_num == room_num) {
room_index = i;
break;
}
}
if (room_index == -1) {
printf("客房号不存在。\n");
return;
}
printf("请输入客房类型:");
scanf("%s", rooms[room_index].room_type);
printf("请输入价格:");
scanf("%f", &rooms[room_index].price);
printf("修改成功。\n");
}
void delete_room() {
int room_num;
printf("请输入您要删除的客房号:");
scanf("%d", &room_num);
int room_index = -1;
for (int i = 0; i < num_rooms; i++) {
if (rooms[i].room_num == room_num) {
room_index = i;
break;
}
}
if (room_index == -1) {
printf("客房号不存在。\n");
return;
}
for (int i = room_index; i < num_rooms - 1; i++) {
rooms[i] = rooms[i + 1];
}
num_rooms--;
printf("删除成功。\n");
}
```
注意:上述代码只是一个简单的示例,可能存在一些不完善的地方,仅供参考,具体实现需要根据实际需求进行改动。同时,为了方便数据的保存和读取,代码中使用了文件操作,需要确保当前目录下存在名为 "data.txt" 的文件。
阅读全文