来实现一下简单的一个酒店管理系统
介绍:
1.要做一个规格为12*12的楼层管理
2.实现功能:显示房间信息,入住,退房,退出
效果如下:
数字:表示房间号 NUll:表示客户是否有人,NUll为空,没有住人。
实现入下
//主函数
#include <stdio.h> //头文件
#include <string.h>
#include <time.h>
#define Price 0.01 //房费 统计
struct Room
{
char userName[20]; //用户名字
int state; //房间状态 0:空闲 1:入住
clock_t inTime; //入住时间
clock_t outTime; //退房时间
} room[12][12]; // 房间 12*12
int flag = 1; //是为了让每次输入时只显示一次房间信息
int main()
{
printf("1:显示房间信息\n");
printf("2:开放\n");
printf("3:退房\n");
printf("4:退出\n");
int choose = 1;
while (1)
{
scanf("%d", &choose);
switch (choose)
{
case 1:
createFace(); //封装函数
break;
case 2:
inHotel();
break;
case 3:
leaveRoom();
break;
case 4:
break; //退出直接break
}
}
return 0;
}
显示房间函数封装createFace();:
void createFace()
{
for (int i = 0; i < 12; i++) //双循环打印出12*12
{
for (int j = 0; j < 12; j++)
{
if ((i + 1) < 10 && (j + 1) < 10)
{
printf("0%d0%d\t", (i + 1), (j + 1)); //0%d0%d 时当打印小于10前面要补零
}
else if ((i + 1) >= 10 && (j + 1) < 10)
{
printf("%d0%d\t", (i + 1), (j + 1));
}
else if ((i + 1) < 10 && (j + 1) >= 10)
{
printf("0%d%d\t", (i + 1), (j + 1));
}
else
{
printf("%d%d\t", (i + 1), (j + 1));
}
if (flag)
{
strcpy(room[i][j].userName, "NUll");
room[i][j].state = 0;
}
}
printf("\n"); //房间号下面显示房间客户信息所以换行
for (int k = 0; k < 12; k++)
{
printf("%s\t", room[i][k].userName);
}
printf("\n");
printf("\n\n");
}
flag = 0;
}
住房inHotel();
void inHotel()
{
int num = 0;
int floor = 0;
int roomNum = 0;
printf("***********欢迎入住*************\n");
printf("输入想住的房间:");
scanf("%d", &num); //房间0101
floor = num / 100 - 1; //层数
roomNum = num % 100 - 1; // 房间号
if (room[floor][roomNum].state != 0)
{
printf("房间已经住人\n");
return;
}
else
{
printf("入住成功,输入你的名字:");
scanf("%s", room[floor][roomNum].userName);
room[floor][roomNum].inTime = clock(); //入住时间
room[floor][roomNum].state = 1; //表示已经有人入住
printf("入住成功\n");
}
}
退房void leaveRoom():
void leaveRoom()
{
int num = 0;
int floor = 0;
int roomNum = 0;
printf("************************\n");
printf("输入退出的房间:");
scanf("%d", &num); //房间0101
floor = num / 100 - 1; //层数
roomNum = num % 100 - 1; // 房间号
if (room[floor][roomNum].state == 0) //退房时房间客户为0不需要退直接break
{
printf("房间空\n");
return;
}
else
{
room[floor][roomNum].outTime = clock();
printf("输入退出房间号:%d>>", num);
printf("客户名字:%s\n", room[floor][roomNum].userName);
printf("消费:%.2f\n", ((room[floor][roomNum].outTime - room[floor][roomNum].inTime) / 1000) * Price); //此处price 就是定义的宏函数,用来计算房费
room[floor][roomNum].state = 0;
strcpy(room[floor][roomNum].userName, "NUll");
}
}
结果显示:
运行时:
当输入1时:
输入2时:
再次输入1,看看入住情况:可以发现NULL变了,说明入住成功
输入3时,退房情况:
再次输入1,看看退房情况:可以发现变NULL了,说明退房成功