项目设计要求
1.房间信息录入,包括房间号,房间类型,入住1天房间费用,房间状态(空闲或者已入住),录入完成后自动保存room.txt中
2.入住信息办理,包括房间号,客人姓名,客人年龄,入住日期,入住几天,总费用,保存在checkin.txt:
3.退房,用户输入房间号,修改房间状态为空闲即可
4.输出所有账单信息,将2中的入住信息输入即可
5.退出系统
一、流程图
二、实现结果展示
三,详细代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROOMS 100
typedef struct {
int roomNumber;
char roomType[30];
double dailyRate;
char status[15]; // "Vacant" or "Occupied"
} Room;
typedef struct {
int roomNumber;
char guestName[50];
int guestAge;
char checkinDate[12];
int daysStaying;
double totalCost;
} CheckIn;
Room rooms[MAX_ROOMS];
int roomCount = 0;
CheckIn checkins[MAX_ROOMS];
int checkinCount = 0;
void loadRoomData() {
FILE *fp = fopen("room.txt", "r");
if (fp == NULL) {
return; // 如果文件不存在, 直接返回
}
while (fscanf(fp, "%d %29s %lf %14s", &rooms[roomCount].roomNumber, rooms[roomCount].roomType,
&rooms[roomCount].dailyRate, rooms[roomCount].status) == 4) {
if (roomCount < MAX_ROOMS - 1) {
roomCount++;
} else {
break;
}
}
fclose(fp);
}
void loadCheckInData() {
FILE *fp = fopen("checkin.txt", "r");
if (fp == NULL) {
return; // 如果文件不存在, 直接返回
}
while (fscanf(fp, "%d %49[^\n] %d %11s %d %lf", &checkins[checkinCount].roomNumber, checkins[checkinCount].guestName,
&checkins[checkinCount].guestAge, checkins[checkinCount].checkinDate, &checkins[checkinCount].daysStaying,
&checkins[checkinCount].totalCost) == 6) {
if (checkinCount < MAX_ROOMS - 1) {
checkinCount++;
} else {
break;
}
}
fclose(fp);
}
void enterRoomInfo() {
Room room;
printf("输入房间号 ");
scanf("%d", &room.roomNumber);
printf("输入房间类型(eg:大床房/双床房): ");
scanf("%s", room.roomType);
printf("输入每天房间的费用");
scanf("%lf", &room.dailyRate);
strcpy(room.status, "空闲");
rooms[roomCount++] = room;
FILE *fp = fopen("room.txt", "a");
fprintf(fp, "%d %s %.2f %s\n", room.roomNumber, room.roomType, room.dailyRate, room.status);
fclose(fp);
printf("房间信息已被录入!\n");
}
void checkIn() {
CheckIn checkin;
printf("输入房间号: ");
scanf("%d", &checkin.roomNumber);
int i;
for (i = 0; i < roomCount; i++) {
if (rooms[i].roomNumber == checkin.roomNumber && strcmp(rooms[i].status, "空闲") == 0) {
strcpy(rooms[i].status, "已入住");
printf("输入客人姓名 ");
scanf(" %[^\n]s", checkin.guestName);
printf("输入客人年龄 ");
scanf("%d", &checkin.guestAge);
printf("输入入住日期 (YYYY-MM-DD): ");
scanf("%s", checkin.checkinDate);
printf("输入入住几天 ");
scanf("%d", &checkin.daysStaying);
checkin.totalCost = rooms[i].dailyRate * checkin.daysStaying;
checkins[checkinCount++] = checkin;
FILE *fp = fopen("checkin.txt", "a");
fprintf(fp, "%d %s %d %s %d %.2f\n", checkin.roomNumber, checkin.guestName, checkin.guestAge, checkin.checkinDate, checkin.daysStaying, checkin.totalCost);
fclose(fp);
// 更新 room.txt
updateRoomFile();
printf("成功入住!入住总费用: $%.2f\n", checkin.totalCost);
return;
}
}
printf("房间不可用或不存在.\n");
}
void updateRoomFile() {
FILE *fp = fopen("room.txt", "w");
if (!fp) {
printf("无法打开 room.txt 文件进行更新!\n");
return;
}
int i;
for (i = 0; i < roomCount; i++) {
fprintf(fp, "%d %s %.2f %s\n", rooms[i].roomNumber, rooms[i].roomType, rooms[i].dailyRate, rooms[i].status);
}
fclose(fp);
}
void checkOut() {
int roomNumber;
printf("输入房号退房: ");
scanf("%d", &roomNumber);
int i;
int found = 0; // 标记找到对应已入住的房间
for (i = 0; i < roomCount; i++) {
if (rooms[i].roomNumber == roomNumber && strcmp(rooms[i].status, "已入住") == 0) {
strcpy(rooms[i].status, "空闲");
found = 1;
printf("退房成功! %d 号房间现在是空闲的.\n", roomNumber);
// 更新 room.txt 文件
updateRoomFile();
break; // 找到房间并处理完毕,无需继续循环
}
}
if (!found) {
printf("未找到该房间号或房间未被入住.\n");
}
}
void displayAllBills() {
printf("房间号-姓名-年龄-入住日期-入住天数-总费用\n");
printf("============================================\n");
FILE *fp = fopen("checkin.txt", "r");
if (!fp) {
printf("不能打开checkin.txt\n");
return;
}
char line[255];
while (fgets(line, sizeof(line), fp) != NULL) {
printf("%s", line);
}
fclose(fp);
printf("============================================\n");
}
int main() {
loadRoomData(); // 启动程序时加载房间信息
loadCheckInData(); // 启动程序时加载入住信息
int option;
do {
printf("\n%%%%%%%%%%%%%%%%% 酒店管理系统 %%%%%%%%%%%%%%%%%\n");
printf("1. 房间信息录入\n");
printf("2. 客户入住信息办理\n");
printf("3. 客户退房办理\n");
printf("4. 展示所有的账单信息\n");
printf("5. 退出\n");
printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
printf("选择你的操作: ");
scanf("%d", &option);
switch (option) {
case 1:
enterRoomInfo();
break;
case 2:
checkIn();
break;
case 3:
checkOut();
break;
case 4:
displayAllBills();
break;
case 5:
printf("退出系统...\n");
break;
default:
printf("无效操作,请重新选择.\n");
}
} while (option != 5);
return 0;
}