目录
前言
我们之前做了一版通讯录:
初步实现了增删查改,趁热打铁,再写个客房管理系统来练练手吧~
题目要求
全部代码
废话不多说,直接上代码。
函数的声明及类型的定义部分
#define _CRT_SECURE_NO_WARNINGS
//头文件
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//常量定义
#define MAX_ROOM_NUMBER 100//最大房间号长度
#define MAX_ROOM_TELEPHONE_NUMBER 100//最大房间电话号码长度
#define MAX_TIME_ENTER 100//最大入住时间长度
#define MAX_ROOM_STATE 100//最大房间状态长度
#define Initial_space 100//信息列表初始大小
#define Increase_Capacity 2//每次扩容的大小
#define MAX_PERSON_ID 100 // 最大住户身份证号长度
//客房信息
typedef struct Room_Message
{
char room_number[MAX_ROOM_NUMBER];
char room_telephone_number[MAX_ROOM_TELEPHONE_NUMBER];
char room_state[MAX_ROOM_STATE];
double price;
}Room_Message;
//入住信息
typedef struct Hotel_Message
{
char Person_Id[MAX_PERSON_ID];
int Time_enter;
int Time_quit;
double fee;
}Hotel_Message;
//入住信息列表
typedef struct Hotel_List
{
Hotel_Message* hotel;
int sz;
int capacity;
}Hotel_List;
//客房信息列表
typedef struct Room_List
{
Room_Message* room;
int sz;
int capacity;
}Room_List;
//入住信息列表
//菜单枚举
enum OPTION
{
EXIT,
ADD,
SEEK,
MODIFY,
DEL,
DISPLAY_ROOM_LIST,
ROOM_ENTER,
DISPLAY_HOTEL_LIST,
SEEK_HOTEL_MESSAGE,
ROOM_QUIT
};
//修改菜单枚举
enum MODIFY_OPTION
{
EXIT1,
ROOM_NUMBER,
ROOM_TELEPHONE_NUMBER,
ROOM_STATE,
ROOM_PRICE
};
//声明初始化客房信息列表函数
void Initial_Room_List(Room_List* prl);
//声明扩容函数
void capacity_room_up(Room_List* prl);
//声明打印菜单函数
void printf_menu();
//声明房间信息添加函数
void Add_room_message(Room_List* prl);
//声明查找房间信息函数
void Seek_room_message(Room_List* prl);
//声明删除房间信息函数
void Del_room_message(Room_List* prl);
//声明修改房间信息函数
void Modify_room_message(Room_List* prl);
//声明修改菜单函数
void menu_modify();
//声明显示房间信息列表函数
void Display_room_list(Room_List* prl);
//声明房间信息列表保存函数
void room_list_save(Room_List* prl);
//声明入住登记函数
void Room_enter(Room_List* prl, Hotel_List* phl);
//声明入住信息列表扩容函数
void capacity_hotel_up(Hotel_List* phl);
//声明入住信息列表初始化函数
void Initial_Hotel_List(Hotel_List* phl);
//声明显示住房信息列表函数
void Display_hotel_list(Room_List* prl, Hotel_List* phl);
//声明查找住房信息函数
void seek_hotel_message(Room_List* prl, Hotel_List* phl);
//声明入住信息列表保存函数
void hotel_list_save(Hotel_List* phl);
//声明退房函数
void Room_quit(Room_List* prl, Hotel_List* phl);
//声明支付费用计算函数
int fee_calculation(int time_enter, int time_quit);
主函数部分
#include"hotel.h"
int main()
{
//定义部分
Room_List room_list;
Hotel_List hotel_list;
int input = 0;
//初始化部分
Initial_Room_List(&room_list);
setbuf(stdin, NULL);//清空缓冲区,要不然缓冲区会溢出
Initial_Hotel_List(&hotel_list);
setbuf(stdin, NULL);
//选项实现部分
do
{
printf_menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case ADD:
system("cls");
Add_room_message(&room_list);
break;
case SEEK:
system("cls");
Seek_room_message(&room_list);
break;
case DEL:
system("cls");
Display_room_list(&room_list);
Del_room_message(&room_list);
break;
case MODIFY:
system("cls");
Modify_room_message(&room_list);
break;
case DISPLAY_ROOM_LIST:
system("cls");
Display_room_list(&room_list);
break;
case ROOM_ENTER:
system("cls");
Room_enter(&room_list,&hotel_list);
break;
case DISPLAY_HOTEL_LIST:
system("cls");
Display_hotel_list(&room_list, &hotel_list);
break;
case SEEK_HOTEL_MESSAGE:
system("cls");
seek_hotel_message(&room_list, &hotel_list);
break;
case ROOM_QUIT:
system("cls");
Room_quit(&room_list, &hotel_list);
break;
case EXIT:
system("cls");
room_list_save(&room_list);
hotel_list_save(&hotel_list);
printf("退出成功\n");
break;
default:
system("cls");
printf("请输入正确的选项\n");
break;
}
} while (input);
return 0;
}
函数的实现部分
#include"hotel.h"
//房间信息列表初始化函数的定义
void Initial_Room_List(Room_List* prl)
{
prl->room =(Room_Message*)malloc(sizeof(Room_Message) * Initial_space);
if (prl->room == NULL)
{
printf("信息列表初始空间开辟失败\n");
return;
}
else
{
prl->capacity = Initial_space;
prl->sz = 0;
}
FILE* fprl = fopen("room_message.txt", "r");
Room_Message tmp;
if (fprl == NULL)
{
perror("fopen");
return;
}
else
{
while (fread(&tmp, sizeof(Room_Message), 1, fprl))
{
if (&tmp == NULL)
{
perror("fread");
}
else
{
capacity_room_up(prl);
prl->room[prl->sz] = tmp;
prl->sz++;
}
}
}
fclose(fprl);
fprl = NULL;
}
//入住信息列表初始化函数
void Initial_Hotel_List(Hotel_List* phl)
{
phl->hotel = (Hotel_Message*)malloc(sizeof(Hotel_Message*) * Initial_space);
if (phl->hotel == NULL)
{
printf("信息列表初始空间开辟失败\n");
return;
}
else
{
phl->capacity = Initial_space;
phl->sz = 0;
}
FILE* fphl = fopen("hotel_message.txt", "r");
Hotel_Message tmp;
if (fphl == NULL)
{
perror("fopen");
return;
}
else
{
while (fread(&tmp, sizeof(Hotel_Message), 1, fphl))
{
if (&tmp == NULL)
{
perror("fread");
}
else
{
capacity_hotel_up(phl);
phl->hotel[phl->sz] = tmp;
phl->sz++;
}
}
}
fclose(fphl);
fphl = NULL;
}
//房间列表扩容函数的定义
void capacity_room_up(Room_List* prl)
{
Room_Message* tmp;
if (prl->sz == prl->capacity)
{
tmp=(Room_Message*)realloc(prl->room, sizeof(Room_Message) * (Increase_Capacity + prl->capacity));
if (tmp == NULL)
{
perror("realloc\n");
return;
}
else
{
printf("扩容成功\n");
prl->room = tmp;
prl->capacity = Increase_Capacity+prl->capacity;
}
}
}
//定义入住信息列表扩容函数
void capacity_hotel_up(Hotel_List* phl)
{
Hotel_Message* tmp;
if (phl->sz == phl->capacity)
{
tmp = (Hotel_Message*)realloc(phl->hotel, sizeof(Hotel_Message) * (Increase_Capacity + phl->capacity));
if (tmp == NULL)
{
perror("realloc\n");
return;
}
else
{
printf("扩容成功\n");
phl->hotel = tmp;
phl->capacity = Increase_Capacity + phl->capacity;
}
}
}
//菜单
void printf_menu()
{
printf("+++++++++++++++++++++++++++++++\n");
printf("-------------------------------\n");
printf("======= 1.增加房间信息 =======\n");
printf("======= 2.查找房间信息 =======\n");
printf("======= 3.修改房间信息 =======\n");
printf("======= 4.删除房间信息 =======\n");
printf("======= 5.显示房间列表 =======\n");
printf("======= 6.入住信息登记 =======\n");
printf("======= 7.显示入住信息列表 ====\n");
printf("======= 8.查询入住信息 =======\n");
printf("======= 9.退房信息登记 =======\n");
printf("======= 0.退出 =======\n");
printf("-------------------------------\n");
printf("+++++++++++++++++++++++++++++++\n");
}
//定义入住登记函数
void Room_enter(Room_List* prl, Hotel_List* phl);
//定义房间信息添加函数
void Add_room_message(Room_List* prl)
{
//判断是否需要扩容
capacity_room_up(prl);
//添加部分
printf("请输入需要添加的房间号\n");
scanf("%s", prl->room[prl->sz].room_number);
printf("请输入需要需要添加的房间电话号码\n");
scanf("%s", prl->room[prl->sz].room_telephone_number);
printf("请输入需要添加的房间状态\n");
scanf("%s", prl->room[prl->sz].room_state);
printf("请输入需要添加的房间价格\n");
scanf("%lf", &prl->room[prl->sz].price);
//反馈
printf("添加成功!\n");
prl->sz++;
}
//定义查找房间函数
void Seek_room_message(Room_List* prl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
printf("请输入要查找的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
flag = 1;
printf("%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格");
printf("%-30s\t%-30s\t%-30s\t%-30lf\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price);
return;
}
}
if (flag != 1)
{
printf("找不到\n");
return;
}
}
//定义删除房间信息函数
void Del_room_message(Room_List* prl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
if (0 == prl->sz)
{
printf("当前房间信息列表为空,无法执行删除操作\n");
}
else
{
printf("请输入要删除的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
flag = 1;
memmove(&prl->room[i],&prl->room[i+1],(prl->sz-i+1)*sizeof(Room_Message));
prl->sz--;
printf("删除成功\n");
return;
}
}
if (flag != 1)
{
printf("找不到,无法删除\n");
return;
}
}
}
//定义修改房间信息函数
void Modify_room_message(Room_List* prl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
printf("请输入要修改的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
flag = 1;
int option = 0;
menu_modify();
printf("请选择要修改的选项\n");
scanf("%d", &option);
switch (option)
{
case ROOM_NUMBER:
printf("请输入新修改的房间号\n");
scanf("%s", prl->room[i].room_number);
printf("修改成功\n");
break;
case ROOM_TELEPHONE_NUMBER:
printf("请输入新修改的房间电话号码\n");
scanf("%s", prl->room[i].room_telephone_number);
printf("修改成功\n");
break;
case ROOM_STATE:
printf("请输入新修改的房间状态\n");
scanf("%s", prl->room[i].room_state);
printf("修改成功\n");
break;
case ROOM_PRICE:
printf("请输入新修改的房间价格\n");
scanf("%lf", &prl->room[i].price);
printf("修改成功\n");
break;
case EXIT1:
printf("退出修改\n");
default:
printf("请输入正确的选项\n");
break;
}
return;
}
}
if (flag != 1)
{
printf("找不到,无法修改\n");
return;
}
}
//定义修改菜单函数
void menu_modify()
{
printf("+++++++++++++++++++++++++++++++\n");
printf("-------------------------------\n");
printf("======= 1.修改房间号 =======\n");
printf("======= 2.修改房间电话号码=====\n");
printf("======= 3.修改房间状态 =======\n");
printf("======= 4.修改房间价格 =======\n");
printf("======= 0.退出 =======\n");
printf("-------------------------------\n");
printf("+++++++++++++++++++++++++++++++\n");
}
//定义显示房间信息列表函数
void Display_room_list(Room_List* prl)
{
int i = 0;
printf("%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格");
for (i = 0; i < prl->sz; i++)
{
printf("%-30s\t%-30s\t%-30s\t%-30lf\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price);
}
}
//定义显示住房信息列表函数
void Display_hotel_list(Room_List* prl, Hotel_List* phl)
{
int i = 0;
printf("%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格",
"入住时间",
"退房时间",
"住户身份证号");
for (i = 0; i < phl->sz; i++)
{
printf("%-30s\t%-30s\t%-30s\t%-30lf\t%-30d\t%-30d\t%-30s\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price,
phl->hotel[i].Time_enter,
phl->hotel[i].Time_quit,
phl->hotel[i].Person_Id);
}
}
//房间信息列表保存函数
void room_list_save(Room_List* prl)
{
int i = 0;
FILE* fprl = fopen("room_message.txt", "w");
if (fprl == NULL)
{
perror("fopen");
return;
}
else
{
for (i = 0; i < prl->sz; i++)
{
fwrite(&prl->room[i],sizeof(Room_Message),1,fprl);
}
printf("保存成功\n");
}
fclose(fprl);
fprl = NULL;
prl->sz = 0;
prl->capacity = 0;
prl->room = NULL;
free(prl->room);
}
//入住信息列表保存函数
void hotel_list_save(Hotel_List* phl)
{
int i = 0;
FILE* fprl = fopen("hotel_message.txt", "w");
if (fprl == NULL)
{
perror("fopen");
return;
}
else
{
for (i = 0; i < phl->sz; i++)
{
fwrite(&phl->hotel[i], sizeof(Hotel_Message), 1, fprl);
}
printf("保存成功\n");
}
fclose(fprl);
fprl = NULL;
phl->sz = 0;
phl->capacity = 0;
phl->hotel = NULL;
free(phl->hotel);
}
//定义入住登记函数
void Room_enter(Room_List* prl, Hotel_List* phl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
Display_room_list(prl);
printf("请输入入住的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
printf("请输入入住人身份证号\n");
scanf("%s", phl->hotel[i].Person_Id);
printf("请输入入住时间(输入样例20220101,一定要不能留空格)\n");
scanf("%d", &phl->hotel[i].Time_enter);
printf("请将退房时间设置为0\n");
scanf("%d", &phl->hotel[i].Time_quit);
printf("请将支付费用设置为0\n");
scanf("%lf", &phl->hotel[i].fee);
printf("请将房间状态改为有人\n");
scanf("%s", prl->room[i].room_state);
phl->sz++;
return;
}
}
if (flag != 1)
{
printf("找不到,无法修改\n");
return;
}
}
//查找住房信息函数
void seek_hotel_message(Room_List* prl, Hotel_List* phl)
{
char tmp[MAX_PERSON_ID] = { 0 };
int i = 0;
int flag = 0;
printf("请输入住户的身份证号\n");
scanf("%s", tmp);
for (i = 0; i < phl->sz; i++)
{
if (!strcmp(tmp, phl->hotel[i].Person_Id))
{
flag = 1;
printf("%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格",
"入住时间",
"退房时间",
"住户身份证号");
printf("%-30s\t%-30s\t%-30s\t%-30lf\t%-30d\t%-30d\t%-30s\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price,
phl->hotel[i].Time_enter,
phl->hotel[i].Time_quit,
phl->hotel[i].Person_Id);
return;
}
}
if (flag != 1)
{
printf("找不到\n");
return;
}
}
//退房登记系统
void Room_quit(Room_List* prl, Hotel_List* phl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
Display_room_list(prl);
printf("请输入退房的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
printf("请输入退房时间(输入样例20220101,一定要不能留空格)\n");
scanf("%d", &phl->hotel[i].Time_quit);
printf("需支付的费用为:%lf", (prl->room[i].price) * fee_calculation(phl->hotel[i].Time_enter,phl->hotel[i].Time_quit));
printf("请输入屏幕上显示的支付费用\n");
scanf("%lf", &phl->hotel[i].fee);
printf("请输入空房间\n");
scanf("%s", prl->room[i].room_state);
return;
}
}
}
//定义支付费用计算函数
int fee_calculation(int time_enter, int time_quit)
{
int day_e = 0;//今年已经过去天数
int day_q = 0;
int days = 0;//距离今年结束剩余天数
int year_enter = time_enter / 10000;
int month_enter = (time_enter / 100) % 100;
int day_enter = time_enter % 100;
int year_quit = time_quit / 10000;
int month_quit = (time_quit / 100) % 100;
int day_quit = time_quit % 100;
if (year_enter == year_quit)
{
if ((year_quit % 4 == 0 && year_quit % 100 != 0) || (year_quit) % 400 == 0)
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 60;
break;
case 4:
day_q = 91;
break;
case 5:
day_q = 121;
break;
case 6:
day_q = 152;
break;
case 7:
day_q = 182;
break;
case 8:
day_q = 213;
break;
case 9:
day_q = 244;
break;
case 10:
day_q = 274;
break;
case 11:
day_q = 305;
break;
case 12:
day_q = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
else
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 59;
break;
case 4:
day_q = 90;
break;
case 5:
day_q = 120;
break;
case 6:
day_q = 151;
break;
case 7:
day_q = 181;
break;
case 8:
day_q = 212;
break;
case 9:
day_q = 243;
break;
case 10:
day_q = 273;
break;
case 11:
day_q = 304;
break;
case 12:
day_q = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
if ((year_enter % 4 == 0 && year_enter % 100 != 0) || (year_enter) % 400 == 0)
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 60;
break;
case 4:
day_e = 91;
break;
case 5:
day_e = 121;
break;
case 6:
day_e = 152;
break;
case 7:
day_e = 182;
break;
case 8:
day_e = 213;
break;
case 9:
day_e = 244;
break;
case 10:
day_e = 274;
break;
case 11:
day_e = 305;
break;
case 12:
day_e = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
}
else
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 59;
break;
case 4:
day_e = 90;
break;
case 5:
day_e = 120;
break;
case 6:
day_e = 151;
break;
case 7:
day_e = 181;
break;
case 8:
day_e = 212;
break;
case 9:
day_e = 243;
break;
case 10:
day_e = 273;
break;
case 11:
day_e = 304;
break;
case 12:
day_e = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
}
return day_q - day_e;
}
else
{
//计算在入住日期这一年中,还剩下多少天
if ((year_enter % 4 == 0 && year_enter % 100 != 0) || (year_enter) % 400 == 0)
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 60;
break;
case 4:
day_e = 91;
break;
case 5:
day_e = 121;
break;
case 6:
day_e = 152;
break;
case 7:
day_e = 182;
break;
case 8:
day_e = 213;
break;
case 9:
day_e = 244;
break;
case 10:
day_e = 274;
break;
case 11:
day_e = 305;
break;
case 12:
day_e = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
days = 366 - day_e;
}
else
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 59;
break;
case 4:
day_e = 90;
break;
case 5:
day_e = 120;
break;
case 6:
day_e = 151;
break;
case 7:
day_e = 181;
break;
case 8:
day_e = 212;
break;
case 9:
day_e = 243;
break;
case 10:
day_e = 273;
break;
case 11:
day_e = 304;
break;
case 12:
day_e = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
days = 365 - day_e;
}
//计算在退房时间这一年中,经过了多少天
if ((year_quit % 4 == 0 && year_quit % 100 != 0) || (year_quit) % 400 == 0)
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 60;
break;
case 4:
day_q = 91;
break;
case 5:
day_q = 121;
break;
case 6:
day_q = 152;
break;
case 7:
day_q = 182;
break;
case 8:
day_q = 213;
break;
case 9:
day_q = 244;
break;
case 10:
day_q = 274;
break;
case 11:
day_q = 305;
break;
case 12:
day_q = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
else
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 59;
break;
case 4:
day_q = 90;
break;
case 5:
day_q = 120;
break;
case 6:
day_q = 151;
break;
case 7:
day_q = 181;
break;
case 8:
day_q = 212;
break;
case 9:
day_q = 243;
break;
case 10:
day_q = 273;
break;
case 11:
day_q = 304;
break;
case 12:
day_q = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
//返回两个日期中间隔得天数
return day_q + days;
}
}
实现步骤
定义常量
为了方便后期的修改,我们将要用的且用时不会改变的值定义为常量
//常量定义
#define MAX_ROOM_NUMBER 100//最大房间号长度
#define MAX_ROOM_TELEPHONE_NUMBER 100//最大房间电话号码长度
#define MAX_TIME_ENTER 100//最大入住时间长度
#define MAX_ROOM_STATE 100//最大房间状态长度
#define Initial_space 100//信息列表初始大小
#define Increase_Capacity 2//每次扩容的大小
#define MAX_PERSON_ID 100 // 最大住户身份证号长度
定义类型
由题意可知,我们需要两种结构体类型——客房类型、入住信息类型。
客房类型
内容分别是房间号、房间电话号码、房间状态、房间价格。
为了方便使用,我们重命名为Room_Message
//客房信息
typedef struct Room_Message
{
char room_number[MAX_ROOM_NUMBER];
char room_telephone_number[MAX_ROOM_TELEPHONE_NUMBER];
char room_state[MAX_ROOM_STATE];
double price;
}Room_Message;
入住信息类型
内容分别是住户身份证号、入住时间、退房时间、需要支付的费用
为了方便使用,我们重命名为Hotel_Message。
//入住信息
typedef struct Hotel_Message
{
char Person_Id[MAX_PERSON_ID];
int Time_enter;
int Time_quit;
double fee;
}Hotel_Message;
客房信息列表类型
内容分别是客房信息成员,列表成员个数,列表容量
//客房信息列表
typedef struct Room_List
{
Room_Message* room;
int sz;
int capacity;
}Room_List;
入住信息列表类型
内容分别是入住信息成员,列表成员个数,列表容量。
//入住信息列表
typedef struct Hotel_List
{
Hotel_Message* hotel;
int sz;
int capacity;
}Hotel_List;
对客房信息列表和入住信息列表初始化
创建完类型之后,我们需要对所创建的类型成员初始化,来让它们发挥作用。
在主函数定义入住信息列表变量和客房信息列表变量
Room_List room_list;
Hotel_List hotel_list;
定义入住信息列表扩容函数
由于入住信息成员的存放空间是动态开辟的,并且是由入住信息指针来进行维护的,我们要关注到容量的问题。
//定义入住信息列表扩容函数
void capacity_hotel_up(Hotel_List* phl)
{
Hotel_Message* tmp;
if (phl->sz == phl->capacity)
{
tmp = (Hotel_Message*)realloc(phl->hotel, sizeof(Hotel_Message) * (Increase_Capacity + phl->capacity));
if (tmp == NULL)
{
perror("realloc\n");
return;
}
else
{
printf("扩容成功\n");
phl->hotel = tmp;
phl->capacity = Increase_Capacity + phl->capacity;
}
}
}
定义入住信息列表初始化函数
先动态开辟空间,用来存放入住信息成员,打开入住信息列表文件,为以后从文件中读取信息做准备,开始往phl->hotel所维护的空间读内容,读之前先检查是否需要扩容,最后关闭文件,重置文件指针。
//入住信息列表初始化函数
void Initial_Hotel_List(Hotel_List* phl)
{
phl->hotel = (Hotel_Message*)malloc(sizeof(Hotel_Message*) * Initial_space);
if (phl->hotel == NULL)
{
printf("信息列表初始空间开辟失败\n");
return;
}
else
{
phl->capacity = Initial_space;
phl->sz = 0;
}
FILE* fphl = fopen("hotel_message.txt", "r");
Hotel_Message tmp;
if (fphl == NULL)
{
perror("fopen");
return;
}
else
{
while (fread(&tmp, sizeof(Hotel_Message), 1, fphl))
{
if (&tmp == NULL)
{
perror("fread");
}
else
{
capacity_hotel_up(phl);
phl->hotel[phl->sz] = tmp;
phl->sz++;
}
}
}
fclose(fphl);
fphl = NULL;
}
定义客房信息列表扩容函数
由于客房信息成员的存放空间是动态开辟的,并且是由客房信息指针来进行维护的,我们要关注到容量的问题。
//房间列表扩容函数的定义
void capacity_room_up(Room_List* prl)
{
Room_Message* tmp;
if (prl->sz == prl->capacity)
{
tmp=(Room_Message*)realloc(prl->room, sizeof(Room_Message) * (Increase_Capacity + prl->capacity));
if (tmp == NULL)
{
perror("realloc\n");
return;
}
else
{
printf("扩容成功\n");
prl->room = tmp;
prl->capacity = Increase_Capacity+prl->capacity;
}
}
}
定义客房信息列表初始化函数
先动态开辟空间,用来存放客房信息成员,打开客房信息列表文件,为以后从文件中读取信息做准备,开始往prl->room所维护的空间读内容,读之前先检查是否需要扩容,最后关闭文件,重置文件指针。
//房间信息列表初始化函数的定义
void Initial_Room_List(Room_List* prl)
{
prl->room =(Room_Message*)malloc(sizeof(Room_Message) * Initial_space);
if (prl->room == NULL)
{
printf("信息列表初始空间开辟失败\n");
return;
}
else
{
prl->capacity = Initial_space;
prl->sz = 0;
}
FILE* fprl = fopen("room_message.txt", "r");
Room_Message tmp;
if (fprl == NULL)
{
perror("fopen");
return;
}
else
{
while (fread(&tmp, sizeof(Room_Message), 1, fprl))
{
if (&tmp == NULL)
{
perror("fread");
}
else
{
capacity_room_up(prl);
prl->room[prl->sz] = tmp;
prl->sz++;
}
}
}
fclose(fprl);
fprl = NULL;
}
实现选择功能
定义input变量
利用这个变量来接受用户的选择
int input = 0;
定义选项实现部分
//选项实现部分
do
{
printf_menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case ADD:
system("cls");
Add_room_message(&room_list);
break;
case SEEK:
system("cls");
Seek_room_message(&room_list);
break;
case DEL:
system("cls");
Display_room_list(&room_list);
Del_room_message(&room_list);
break;
case MODIFY:
system("cls");
Modify_room_message(&room_list);
break;
case DISPLAY_ROOM_LIST:
system("cls");
Display_room_list(&room_list);
break;
case ROOM_ENTER:
system("cls");
Room_enter(&room_list,&hotel_list);
break;
case DISPLAY_HOTEL_LIST:
system("cls");
Display_hotel_list(&room_list, &hotel_list);
break;
case SEEK_HOTEL_MESSAGE:
system("cls");
seek_hotel_message(&room_list, &hotel_list);
break;
case ROOM_QUIT:
system("cls");
Room_quit(&room_list, &hotel_list);
break;
case EXIT:
system("cls");
room_list_save(&room_list);
hotel_list_save(&hotel_list);
printf("退出成功\n");
break;
default:
system("cls");
printf("请输入正确的选项\n");
break;
}
} while (input);
菜单枚举类型的定义
为了方便使用switch语句,同时增加代码的可读性,我们将数字换成了枚举成员
//菜单枚举
enum OPTION
{
EXIT,
ADD,
SEEK,
MODIFY,
DEL,
DISPLAY_ROOM_LIST,
ROOM_ENTER,
DISPLAY_HOTEL_LIST,
SEEK_HOTEL_MESSAGE,
ROOM_QUIT
};
显示菜单的实现
//菜单
void printf_menu()
{
printf("+++++++++++++++++++++++++++++++\n");
printf("-------------------------------\n");
printf("======= 1.增加房间信息 =======\n");
printf("======= 2.查找房间信息 =======\n");
printf("======= 3.修改房间信息 =======\n");
printf("======= 4.删除房间信息 =======\n");
printf("======= 5.显示房间列表 =======\n");
printf("======= 6.入住信息登记 =======\n");
printf("======= 7.显示入住信息列表 ====\n");
printf("======= 8.查询入住信息 =======\n");
printf("======= 9.退房信息登记 =======\n");
printf("======= 0.退出 =======\n");
printf("-------------------------------\n");
printf("+++++++++++++++++++++++++++++++\n");
}
输入部分的实现
printf("请选择\n");
scanf("%d", &input);
添加客房信息功能的实现
//定义房间信息添加函数
void Add_room_message(Room_List* prl)
{
//判断是否需要扩容
capacity_room_up(prl);
//添加部分
printf("请输入需要添加的房间号\n");
scanf("%s", prl->room[prl->sz].room_number);
printf("请输入需要需要添加的房间电话号码\n");
scanf("%s", prl->room[prl->sz].room_telephone_number);
printf("请输入需要添加的房间状态\n");
scanf("%s", prl->room[prl->sz].room_state);
printf("请输入需要添加的房间价格\n");
scanf("%lf", &prl->room[prl->sz].price);
//反馈
printf("添加成功!\n");
prl->sz++;
}
查找客房信息功能的实现
//定义查找房间函数
void Seek_room_message(Room_List* prl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
printf("请输入要查找的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
flag = 1;
printf("%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格");
printf("%-30s\t%-30s\t%-30s\t%-30lf\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price);
return;
}
}
if (flag != 1)
{
printf("找不到\n");
return;
}
}
显示整个客房信息列表的实现
//定义显示房间信息列表函数
void Display_room_list(Room_List* prl)
{
int i = 0;
printf("%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格");
for (i = 0; i < prl->sz; i++)
{
printf("%-30s\t%-30s\t%-30s\t%-30lf\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price);
}
}
删除客房信息功能的实现
先显示整个客房信息列表,然后再进行删除操作
case DEL:
system("cls");
Display_room_list(&room_list);
Del_room_message(&room_list);
break;
删除操作的实现
利用memmove来实现删除的操作
//定义删除房间信息函数
void Del_room_message(Room_List* prl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
if (0 == prl->sz)
{
printf("当前房间信息列表为空,无法执行删除操作\n");
}
else
{
printf("请输入要删除的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
flag = 1;
memmove(&prl->room[i],&prl->room[i+1],(prl->sz-i+1)*sizeof(Room_Message));
prl->sz--;
printf("删除成功\n");
return;
}
}
if (flag != 1)
{
printf("找不到,无法删除\n");
return;
}
}
}
修改信息功能的实现
先显示整个客房信息列表,再进行修改操作
case MODIFY:
system("cls");
Display_room_list(&room_list);
Modify_room_message(&room_list);
修改菜单的实现
//定义修改菜单函数
void menu_modify()
{
printf("+++++++++++++++++++++++++++++++\n");
printf("-------------------------------\n");
printf("======= 1.修改房间号 =======\n");
printf("======= 2.修改房间电话号码=====\n");
printf("======= 3.修改房间状态 =======\n");
printf("======= 4.修改房间价格 =======\n");
printf("======= 0.退出 =======\n");
printf("-------------------------------\n");
printf("+++++++++++++++++++++++++++++++\n");
}
修改操作的实现
//定义修改房间信息函数
void Modify_room_message(Room_List* prl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
printf("请输入要修改的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
flag = 1;
int option = 0;
menu_modify();
printf("请选择要修改的选项\n");
scanf("%d", &option);
switch (option)
{
case ROOM_NUMBER:
printf("请输入新修改的房间号\n");
scanf("%s", prl->room[i].room_number);
printf("修改成功\n");
break;
case ROOM_TELEPHONE_NUMBER:
printf("请输入新修改的房间电话号码\n");
scanf("%s", prl->room[i].room_telephone_number);
printf("修改成功\n");
break;
case ROOM_STATE:
printf("请输入新修改的房间状态\n");
scanf("%s", prl->room[i].room_state);
printf("修改成功\n");
break;
case ROOM_PRICE:
printf("请输入新修改的房间价格\n");
scanf("%lf", &prl->room[i].price);
printf("修改成功\n");
break;
case EXIT1:
printf("退出修改\n");
default:
printf("请输入正确的选项\n");
break;
}
return;
}
}
if (flag != 1)
{
printf("找不到,无法修改\n");
return;
}
}
入住登记功能的实现
先显示整个客房信息列表,再进行登记操作,将退房时间、支付费用通通初始化成0,防止乱码。
//定义入住登记函数
void Room_enter(Room_List* prl, Hotel_List* phl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
Display_room_list(prl);
printf("请输入入住的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
printf("请输入入住人身份证号\n");
scanf("%s", phl->hotel[i].Person_Id);
printf("请输入入住时间(输入样例20220101,一定要不能留空格)\n");
scanf("%d", &phl->hotel[i].Time_enter);
printf("请将退房时间设置为0\n");
scanf("%d", &phl->hotel[i].Time_quit);
printf("请将支付费用设置为0\n");
scanf("%lf", &phl->hotel[i].fee);
printf("请将房间状态改为有人\n");
scanf("%s", prl->room[i].room_state);
phl->sz++;
return;
}
}
if (flag != 1)
{
printf("找不到,无法修改\n");
return;
}
}
显示整个入住信息列表功能的实现
先打印题头,更为美观
//定义显示住房信息列表函数
void Display_hotel_list(Room_List* prl, Hotel_List* phl)
{
int i = 0;
printf("%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格",
"入住时间",
"退房时间",
"住户身份证号");
for (i = 0; i < phl->sz; i++)
{
printf("%-30s\t%-30s\t%-30s\t%-30lf\t%-30d\t%-30d\t%-30s\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price,
phl->hotel[i].Time_enter,
phl->hotel[i].Time_quit,
phl->hotel[i].Person_Id);
}
}
查找入住信息功能的实现(按身份证号查找)
//查找住房信息函数
void seek_hotel_message(Room_List* prl, Hotel_List* phl)
{
char tmp[MAX_PERSON_ID] = { 0 };
int i = 0;
int flag = 0;
printf("请输入住户的身份证号\n");
scanf("%s", tmp);
for (i = 0; i < phl->sz; i++)
{
if (!strcmp(tmp, phl->hotel[i].Person_Id))
{
flag = 1;
printf("%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\t%-30s\n",
"房间名",
"房间电话号码",
"房间状态",
"房间价格",
"入住时间",
"退房时间",
"住户身份证号");
printf("%-30s\t%-30s\t%-30s\t%-30lf\t%-30d\t%-30d\t%-30s\n",
prl->room[i].room_number,
prl->room[i].room_telephone_number,
prl->room[i].room_state,
prl->room[i].price,
phl->hotel[i].Time_enter,
phl->hotel[i].Time_quit,
phl->hotel[i].Person_Id);
return;
}
}
if (flag != 1)
{
printf("找不到\n");
return;
}
}
退房登记功能的实现
实现计算房费、修改房间状态、修改退房时间的功能
//退房登记系统
void Room_quit(Room_List* prl, Hotel_List* phl)
{
char tmp[MAX_ROOM_NUMBER] = { 0 };
int i = 0;
int flag = 0;
Display_room_list(prl);
printf("请输入退房的房间号\n");
scanf("%s", tmp);
for (i = 0; i < prl->sz; i++)
{
if (!strcmp(tmp, prl->room[i].room_number))
{
printf("请输入退房时间(输入样例20220101,一定要不能留空格)\n");
scanf("%d", &phl->hotel[i].Time_quit);
printf("需支付的费用为:%lf", (prl->room[i].price) * fee_calculation(phl->hotel[i].Time_enter,phl->hotel[i].Time_quit));
printf("请输入屏幕上显示的支付费用\n");
scanf("%lf", &phl->hotel[i].fee);
printf("请输入空房间\n");
scanf("%s", prl->room[i].room_state);
return;
}
}
}
房费计算功能的实现
//定义支付费用计算函数
int fee_calculation(int time_enter, int time_quit)
{
int day_e = 0;//今年已经过去天数
int day_q = 0;
int days = 0;//距离今年结束剩余天数
int year_enter = time_enter / 10000;
int month_enter = (time_enter / 100) % 100;
int day_enter = time_enter % 100;
int year_quit = time_quit / 10000;
int month_quit = (time_quit / 100) % 100;
int day_quit = time_quit % 100;
if (year_enter == year_quit)
{
if ((year_quit % 4 == 0 && year_quit % 100 != 0) || (year_quit) % 400 == 0)
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 60;
break;
case 4:
day_q = 91;
break;
case 5:
day_q = 121;
break;
case 6:
day_q = 152;
break;
case 7:
day_q = 182;
break;
case 8:
day_q = 213;
break;
case 9:
day_q = 244;
break;
case 10:
day_q = 274;
break;
case 11:
day_q = 305;
break;
case 12:
day_q = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
else
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 59;
break;
case 4:
day_q = 90;
break;
case 5:
day_q = 120;
break;
case 6:
day_q = 151;
break;
case 7:
day_q = 181;
break;
case 8:
day_q = 212;
break;
case 9:
day_q = 243;
break;
case 10:
day_q = 273;
break;
case 11:
day_q = 304;
break;
case 12:
day_q = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
if ((year_enter % 4 == 0 && year_enter % 100 != 0) || (year_enter) % 400 == 0)
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 60;
break;
case 4:
day_e = 91;
break;
case 5:
day_e = 121;
break;
case 6:
day_e = 152;
break;
case 7:
day_e = 182;
break;
case 8:
day_e = 213;
break;
case 9:
day_e = 244;
break;
case 10:
day_e = 274;
break;
case 11:
day_e = 305;
break;
case 12:
day_e = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
}
else
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 59;
break;
case 4:
day_e = 90;
break;
case 5:
day_e = 120;
break;
case 6:
day_e = 151;
break;
case 7:
day_e = 181;
break;
case 8:
day_e = 212;
break;
case 9:
day_e = 243;
break;
case 10:
day_e = 273;
break;
case 11:
day_e = 304;
break;
case 12:
day_e = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
}
return day_q - day_e;
}
else
{
//计算在入住日期这一年中,还剩下多少天
if ((year_enter % 4 == 0 && year_enter % 100 != 0) || (year_enter) % 400 == 0)
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 60;
break;
case 4:
day_e = 91;
break;
case 5:
day_e = 121;
break;
case 6:
day_e = 152;
break;
case 7:
day_e = 182;
break;
case 8:
day_e = 213;
break;
case 9:
day_e = 244;
break;
case 10:
day_e = 274;
break;
case 11:
day_e = 305;
break;
case 12:
day_e = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
days = 366 - day_e;
}
else
{
switch (month_enter)
{
case 1:
day_e = 0;
break;
case 2:
day_e = 31;
break;
case 3:
day_e = 59;
break;
case 4:
day_e = 90;
break;
case 5:
day_e = 120;
break;
case 6:
day_e = 151;
break;
case 7:
day_e = 181;
break;
case 8:
day_e = 212;
break;
case 9:
day_e = 243;
break;
case 10:
day_e = 273;
break;
case 11:
day_e = 304;
break;
case 12:
day_e = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_e += day_enter;
days = 365 - day_e;
}
//计算在退房时间这一年中,经过了多少天
if ((year_quit % 4 == 0 && year_quit % 100 != 0) || (year_quit) % 400 == 0)
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 60;
break;
case 4:
day_q = 91;
break;
case 5:
day_q = 121;
break;
case 6:
day_q = 152;
break;
case 7:
day_q = 182;
break;
case 8:
day_q = 213;
break;
case 9:
day_q = 244;
break;
case 10:
day_q = 274;
break;
case 11:
day_q = 305;
break;
case 12:
day_q = 335;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
else
{
switch (month_quit)
{
case 1:
day_q = 0;
break;
case 2:
day_q = 31;
break;
case 3:
day_q = 59;
break;
case 4:
day_q = 90;
break;
case 5:
day_q = 120;
break;
case 6:
day_q = 151;
break;
case 7:
day_q = 181;
break;
case 8:
day_q = 212;
break;
case 9:
day_q = 243;
break;
case 10:
day_q = 273;
break;
case 11:
day_q = 304;
break;
case 12:
day_q = 334;
break;
default:
printf("请输入正确的月份\n");
return 0;
}
day_q += day_quit;
}
//返回两个日期中间隔得天数
return day_q + days;
}
}
退出功能的实现
保存客房信息列表,入住信息列表
case EXIT:
system("cls");
room_list_save(&room_list);
hotel_list_save(&hotel_list);
printf("退出成功\n");
break;
保存客房信息列表的实现
//房间信息列表保存函数
void room_list_save(Room_List* prl)
{
int i = 0;
FILE* fprl = fopen("room_message.txt", "w");
if (fprl == NULL)
{
perror("fopen");
return;
}
else
{
for (i = 0; i < prl->sz; i++)
{
fwrite(&prl->room[i],sizeof(Room_Message),1,fprl);
}
printf("保存成功\n");
}
fclose(fprl);
fprl = NULL;
prl->sz = 0;
prl->capacity = 0;
prl->room = NULL;
free(prl->room);
}
保存入住信息列表的实现
//入住信息列表保存函数
void hotel_list_save(Hotel_List* phl)
{
int i = 0;
FILE* fprl = fopen("hotel_message.txt", "w");
if (fprl == NULL)
{
perror("fopen");
return;
}
else
{
for (i = 0; i < phl->sz; i++)
{
fwrite(&phl->hotel[i], sizeof(Hotel_Message), 1, fprl);
}
printf("保存成功\n");
}
fclose(fprl);
fprl = NULL;
phl->sz = 0;
phl->capacity = 0;
phl->hotel = NULL;
free(phl->hotel);
}
总结
思路定义类型-》初始化-》依次实现要求的功能-》保存到文件
依然是实现增删查改,与通讯录相比,增加了一些功能,本质还是一样的。希望这篇文章能帮助到同志们,希望这个问题和我的思路能够让同志们得到提升,希望对于增删查改之类的题目同志们越写越轻松。