源码:
#include <iostream>
#include <cstring>
#include <string.h>
#include <windows.h>
#include <fstream>
using namespace std;
#define NUM 100 //最大客房数量
//客户结构体
struct Client
{
char Name[20]; //姓名
int Sex; //性别,0表示男,1表示女
int Age; //年龄
char ID_card[20];//居民身份证;
int year;
int month;
int date; //住店时间
};
//客房结构体
struct Hotel
{
int Room_ID; //房间号
char Tel[20]; //电话
int Price; //价格
bool Sign; //标记房间是否为空房间;
struct Client Client_list; //实现两个结构体的嵌套
}Room[NUM];
//文件写入
int infile() {
int cnt = 0;
ifstream in("期末69.txt");
while (!in.eof()) {
in >> Room[cnt].Room_ID >> Room[cnt].Client_list.Name
>> Room[cnt].Client_list.Sex >> Room[cnt].Client_list.Age
>> Room[cnt].Client_list.ID_card
>> Room[cnt].Client_list.year >> Room[cnt].Client_list.month >> Room[cnt].Client_list.date
>> Room[cnt].Tel >> Room[cnt].Price;
Room[cnt].Sign = true;
cnt++;
}
in.close();
return cnt;
}
void outfile(int cnt) {
ofstream out("期末69.txt");
int i;
for (i = 0; i < cnt; i++) {
out << Room[i].Room_ID << ' ' << Room[i].Client_list.Name << ' '
<< Room[i].Client_list.Sex << ' ' << Room[i].Client_list.Age << ' '
<< Room[i].Client_list.ID_card << ' '
<< Room[i].Client_list.year << ' ' << Room[i].Client_list.month << ' ' << Room[i].Client_list.date << ' '
<< Room[i].Tel << ' ' << Room[i].Price << endl;
}
out.close();
}
//用户登记模块
int registration(int cnt)
{
char choice;
system("cls");
cout << "-------------------------------------------------------------------------" << endl;
cout << "|------< 欢迎进入用户登记界面!!! >------|" << endl;
cout << "|----------------------------- ----------------------------|" << endl;
do
{
cout << "请输入姓名:";
cin >> Room[cnt].Client_list.Name;
cout << "请输入性别(0表示男,1表示女):";
cin >> Room[cnt].Client_list.Sex;
cout << "请输入年龄:";
cin >> Room[cnt].Client_list.Age;
cout << "请输入居民身份证号码:";
cin >> Room[cnt].Client_list.ID_card;
cout << "请输入房间号:";
cin >> Room[cnt].Room_ID;
cout << "请输入电话:";
cin >> Room[cnt].Tel;
cout << "请输入房间价格:";
cin >> Room[cnt].Price;
cout << "请输入入住时间(年、月、日,中间用空格隔开):";
cin >> Room[cnt].Client_list.year >> Room[cnt].Client_list.month >> Room[cnt].Client_list.date;
Room[cnt].Sign = true;
cnt++;
cout << "是否继续登记?(y/n):";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return cnt;
}
//统计模块
void statistics()
{
int total_num = 0; //总房客数
int day_num = 0; //某一天的房客数
int year, month, date;
system("cls");
cout << "-------------------------------------------------------------------------" << endl;
cout << "|------< 欢迎进入用户统计界面!!! >------|" << endl;
cout << "|----------------------------- ----------------------------|" << endl;
for (int i = 0; i < NUM; i++)
{
if (Room[i].Sign)
total_num++;
if (Room[i].Client_list.year != 0)
{
cout << "请输入要统计的日期(年、月、日,中间用空格隔开):";
cin >> year >> month >> date;
if (Room[i].Client_list.year == year && Room[i].Client_list.month == month && Room[i].Client_list.date == date)
day_num++;
}
}
cout << "总房客数:" << total_num << endl;
cout << "某一天的房客数:" << day_num << endl;
}
//查询模块
void search()
{
int choice; //选择功能
bool found = false;
char name[20]; //姓名
char id[20]; //身份证号
int price; //价格
int room_id; //房间号
system("cls");
cout << "-------------------------------------------------------------------------" << endl;
cout << "|------< 欢迎进入用户查询界面!!! >------|" << endl;
cout << "|------------------- 查询方式 -----------------|" << endl;
cout << "|1.按姓名查询 2.按居民身份证查询 3.按房间价格查询 4.按房间号查询|" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cin >> choice;
cout << "请输入查询方式:";
switch (choice)
{
case 1:
cout << "请输入要查询的姓名:";
cin >> name;
for (int i = 0; i < NUM; i++)
{
if (Room[i].Sign && strcmp(Room[i].Client_list.Name, name) == 0)
{
found = true;
cout << "姓名:" << Room[i].Client_list.Name << endl;
cout << "性别:" << Room[i].Client_list.Sex << endl;
cout << "年龄:" << Room[i].Client_list.Age << endl;
cout << "居民身份证号码:" << Room[i].Client_list.ID_card << endl;
cout << "房间号:" << Room[i].Room_ID << endl;
cout << "电话:" << Room[i].Tel << endl;
cout << "房间价格:" << Room[i].Price << endl;
cout << "入住时间:" << Room[i].Client_list.year << "年" << Room[i].Client_list.month << "月" << Room[i].Client_list.date << "日" << endl;
break;
}
}
break;
case 2:
cout << "请输入要查询的居民身份证号码:";
cin >> id;
for (int i = 0; i < NUM; i++)
{
if (Room[i].Sign && strcmp(Room[i].Client_list.ID_card, id) == 0)
{
found = true;
cout << "姓名:" << Room[i].Client_list.Name << endl;
cout << "性别:" << Room[i].Client_list.Sex << endl;
cout << "年龄:" << Room[i].Client_list.Age << endl;
cout << "居民身份证号码:" << Room[i].Client_list.ID_card << endl;
cout << "房间号:" << Room[i].Room_ID << endl;
cout << "电话:" << Room[i].Tel << endl;
cout << "房间价格:" << Room[i].Price << endl;
cout << "入住时间:" << Room[i].Client_list.year << "年" << Room[i].Client_list.month << "月" << Room[i].Client_list.date << "日" << endl;
break;
}
}
break;
case 3:
cout << "请输入要查询的房间价格:";
cin >> price;
for (int i = 0; i < NUM; i++)
{
if (Room[i].Sign && Room[i].Price == price)
{
found = true;
cout << "姓名:" << Room[i].Client_list.Name << endl;
cout << "性别:" << Room[i].Client_list.Sex << endl;
cout << "年龄:" << Room[i].Client_list.Age << endl;
cout << "居民身份证号码:" << Room[i].Client_list.ID_card << endl;
cout << "房间号:" << Room[i].Room_ID << endl;
cout << "电话:" << Room[i].Tel << endl;
cout << "房间价格:" << Room[i].Price << endl;
cout << "入住时间:" << Room[i].Client_list.year << "年" << Room[i].Client_list.month << "月" << Room[i].Client_list.date << "日" << endl;
break;
}
}
break;
case 4:
cout << "请输入要查询的房间号:";
cin >> room_id;
for (int i = 0; i < NUM; i++)
{
if (Room[i].Sign && Room[i].Room_ID == room_id)
{
found = true;
cout << "姓名:" << Room[i].Client_list.Name << endl;
cout << "性别:" << Room[i].Client_list.Sex << endl;
cout << "年龄:" << Room[i].Client_list.Age << endl;
cout << "居民身份证号码:" << Room[i].Client_list.ID_card << endl;
cout << "房间号:" << Room[i].Room_ID << endl;
cout << "电话:" << Room[i].Tel << endl;
cout << "房间价格:" << Room[i].Price << endl;
cout << "入住时间:" << Room[i].Client_list.year << "年" << Room[i].Client_list.month << "月" << Room[i].Client_list.date << "日" << endl;
break;
}
}
break;
}
if (!found)
cout << "未找到相关记录!" << endl;
}
//修改模块
void Modify() {
system("cls");
cout << "-------------------------------------------------------------------------" << endl;
cout << "|------< 欢迎进入用户修改界面!!! >------|" << endl;
cout << "|----------------------------- ----------------------------|" << endl;
cout << "请输入要修改的客户姓名:";
char name[20];
cin >> name;
int index = -1;
for (int i = 0; i < NUM; i++) {
if (strcmp(Room[i].Client_list.Name, name) == 0)
{
index = i;
}
}
if (index == -1)
{
cout << "未找到相关客户" << endl;
}
else
{
cout << "请输入客户新的信息:" << endl;
cout << "性别:"; cin >> Room[index].Client_list.Sex;
cout << "年龄:"; cin >> Room[index].Client_list.Age;
cout << "身份证号码:"; cin >> Room[index].Client_list.ID_card;
cout << "住店时间:"; cin >> Room[index].Client_list.year
>>Room[index].Client_list.month
>>Room[index].Client_list.date;
cout << "房间号:"; cin >> Room[index].Room_ID;
cout << "电话:"; cin >> Room[index].Tel;
cout << "价格:"; cin >> Room[index].Price;
}
}
//菜单
void Menu()
{
int cnt;
cnt = infile();
int flag = 1;
while (flag) {
cout << "-------------------------------------------------------------------------" << endl;
cout << "|------< 客房管理系统 >------|" << endl;
cout << "|------------------------------ -----------------------------|" << endl;
cout << "|<<< 1.用户登记 2.统计 3.查询 4.修改 5.退出>>>|" << endl;
cout << "-------------------------------------------------------------------------" << endl;
string choice;
cin >> choice;
if (choice == "1")
{
cnt=registration(cnt);
}
else if (choice == "2")
{
statistics();
}
else if (choice == "3")
{
search();
}
else if (choice == "4")
{
Modify();
}
else if (choice == "5")
{
cout << "已退出喵!欢迎下次使用本系统,谢谢喵~~~" << endl; flag = 0; break;
}
try {
if (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "5") {
throw runtime_error("输入错误!正在重新进入系统......");
}
}
catch (runtime_error& e) { // 捕获异常,并输出错误信息
cout << "错误信息:" << e.what() << endl;
Sleep(1000);
system("cls");
continue;
}
cout << "是否保存本次修改?(Y/N):";
char cont;
cin >> cont;
if (cont == 'Y' || cont == 'y')
{
outfile(cnt);
cout << "已保存本次修改,可到对应文件查看" << endl;
cout << "是否查看所有用户信息?(Y/N):";
char xcont;
cin >> xcont;
if (xcont == 'Y' || xcont == 'y')
{
int i;
for (i = 0; i < cnt; i++) {
cout << Room[i].Room_ID << ' ' << Room[i].Client_list.Name << ' '
<< Room[i].Client_list.Sex << ' ' << Room[i].Client_list.Age << ' '
<< Room[i].Client_list.ID_card << ' '
<< Room[i].Client_list.year << ' ' << Room[i].Client_list.month << ' ' << Room[i].Client_list.date << ' '
<< Room[i].Tel << ' ' << Room[i].Price << endl;
}
}
}
cout << "是否继续?(Y/N):";
char conti;
cin >> conti;
if (conti == 'N' || conti == 'n')
{
cout << "已退出喵!欢迎下次使用本系统,谢谢喵~~~" << endl;
flag = 0;
}
else {
system("cls");
}
}
}
int main() {
Menu();
return 0;
}