📄 订房系统.cpp
字号:
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void menu();
void menu_work();
void menu_person();
class person
{
private:
int age,date;
char sex[6];
char name[10];
long time;
public:
void input(char n[],char s[],int a,int d,long t)
{
strcpy(name,n);
age=a;
strcpy(sex,s);
date=d;
time=t;
}
void output()
{
cout<<name<<" "<<sex<<" "<<age<<" "<<date<<" "<<time<<endl;
}
friend class room;
};
class room:public person
{
private:
int num;
unsigned int siz;
float pri;
person per;
bool book;
public:
room(void)
{
book=0;
}
room *next;
room *forward;
void input(int n,float p,unsigned int s)
{
num=n;
pri=p;
siz=s;
}
void output()
{
if (num!=0)
{
cout<<num<<" "<<pri<<" "<<siz<<" ";
if (book) cout<<"是"<<endl;
else cout<<"否"<<endl;
}
}
bool compare(char n[])
{
if(strcmp(n,per.name)==0) return 1;
else return 0;
}
bool compare(long t)
{
if(t==per.time) return 1;
else return 0;
}
bool compare(int n)
{
if(n==num) return 1;
else return 0;
}
bool compare(float p)
{
if(p==pri) return 1;
else return 0;
}
bool compare(unsigned int s)
{
if(s==siz) return 1;
else return 0;
}
friend void book();
friend void book_list();
friend void unbook_list_work();
friend void unbook_list_person();
friend int find (char x);
friend int find (int x);
friend int find (float x);
friend int find (long x);
friend int find (unsigned x);
};
room *head,*tail,*temp;
template<class T> int find (T x)
{
int y=0;
if(x!=0)
{
temp=head;
while(temp!=NULL)
{
if(temp->compare(x))
{
cout<<"房间号码 房间价格 房间规格 是否预定"<<endl;
temp->output();
if(temp->book)
{
cout<<"名字 性别 年龄 定房天数 定房时间"<<endl;
temp->per.output();
}
else cout<<"无人预定!"<<endl;
y++;
}
temp=temp->next;
}
}
return(y);
}
void find_num()
{
cout<<"请输入要查找的房间号码,按0返回:";
int x;
cin>>x;
if(find<int>(x)==0) cout<<"对不起,没有您需要的房间!"<<endl;
menu_work();
return;
}
void find_name()
{
cout<<"请输入您要查找的名字,按*返回:";
char x;
cin>>x;
if(find<char>(x)==0) cout<<"对不起,"<<x<<"没有预定房间"<<endl;
menu_work();
return;
}
void find_time()
{
cout<<"请输入要查找的定房日期,按0返回:";
long x;
cin>>x;
if(find<long>(x)==0) cout<<"对不起,没有您需要的信息!"<<endl;
menu_work();
return;
}
void find_price()
{
cout<<"请输入要查找的房间价格,按0返回:";
float x;
cin>>x;
if(find<float>(x)==0) cout<<"对不起,没有您需要的房间!"<<endl;
menu_person();
return;
}
void find_size()
{
cout<<"请输入要查找的房间规格,按0返回:";
unsigned x;
cin>>x;
if(find<unsigned>(x)==0) cout<<"对不起,没有您需要的房间!"<<endl;
menu_person();
return;
}
void book()
{
int ag,dat;
char se[6];
char nam[10];
long tim;
int x,y=0;
cout<<"请根据提示输入您的有关信息,按0返回:"<<endl;
cout<<"请输入你预定的房间号:";
cin>>x;
if(x!=0)
{
temp=head;
while(temp!=NULL)
{
if(temp->compare(x))
{
if(temp->book)
{
cout<<"对不起,预定失败,此房已经有人预定!"<<endl;
}
if(!temp->book)
{
cout<<"名字 性别 年龄 定房天数 定房时间"<<endl;
cin>>nam>>se>>ag>>dat>>tim;
cout<<"OK,预定成功"<<endl;
temp->per.input(nam,se,ag,dat,tim);
temp->book=1;
cout<<"您预定的房间信息是:"<<endl;
cout<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<" "<<"是否预定"<<endl;
temp->output();
cout<<"您一共需要付款"<<dat*temp->pri<<"元"<<endl;
}
y++;
}
temp=temp->next;
}
if(y==0) cout<<"对不起,预定失败,没有您需要的房间!"<<endl;
}
menu_person();
return;
}
void save_insert()
{
FILE *fp;
char file[10];
cout<<"请输入存档文件名:";
cin>>file;
fp=fopen(file,"w+");
if(head!=tail)
{
tail->next=new room;
temp=tail->next;
}
else cout<<"你现在未读取信息,所以重新建立酒店信息"<<endl;
cout<<"请输入房间信息,退出请按0:"<<endl;
cout<<" "<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<endl;
int number;
unsigned int size;
float price;
int i;
for(i=1;;i++)
{
cout<<endl<<"请输入第"<<i<<"间房的信息:";
cin>>number;
if(number!=0)
{
cin>>price>>size;
temp->input(number,price,size);
fprintf(fp,"%d %f %ud",number,price,size);
temp->next=NULL;
tail=temp;
}
else
{
number=0;
fprintf(fp,"%d",number);
fclose(fp);
delete temp;
tail->next=NULL;
cout<<"输入完毕!"<<endl;
break;
}
temp->next=new room;
temp->next->forward=temp;
temp=temp->next;
}
menu_work();
return;
}
void nosave_insert()
{
if(head!=tail)
{
tail->next=new room;
temp=tail->next;
}
cout<<"请输入房间信息,退出请按0:"<<endl;
cout<<" "<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<endl;
int number,size;
float price;
int i;
for(i=1;;i++)
{
cout<<endl<<"请输入第"<<i<<"间房的信息:";
cin>>number;
if(number!=0)
{
cin>>price>>size;
temp->input(number,price,size);
temp->next=NULL;
tail=temp;
}
if(number==0)
{
delete temp;
tail->next=NULL;
cout<<"输入完毕!"<<endl;
break;
}
temp->next=new room;
temp->next->forward=temp;
temp=temp->next;
}
menu_work();
return;
}
void list()
{
if(tail==head) cout<<"对不起,工作人员还没有输入房间信息!请先输入有关信息"<<endl;
else
{
cout<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<" "<<"是否预定"<<endl;
temp=head;
while(temp!=NULL)
{
temp->output();
temp=temp->next;
}
}
menu_work();
return;
}
void book_list()
{
if(tail==head) cout<<"对不起,还没有输入房间信息!请按2输入有关信息"<<endl;
else
{
cout<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<" "<<"是否预定"<<endl;
temp=head;
while(temp!=NULL)
{
if(temp->book)
{
temp->output();
}
temp=temp->next;
}
}
menu_work();
return;
}
void unbook_list_work()
{
if(tail==head) cout<<"对不起,还没有输入房间信息!请按2输入有关信息"<<endl;
else
{
cout<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<" "<<"是否预定"<<endl;
temp=head;
while(temp!=NULL)
{
if(!temp->book)
{
temp->output();
}
temp=temp->next;
}
}
menu_work();
return;
}
void unbook_list_person()
{
if(tail==head) cout<<"对不起,还没有输入房间信息!请按2输入有关信息"<<endl;
else
{
cout<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<" "<<"是否预定"<<endl;
temp=head;
while(temp!=NULL)
{
if(!temp->book)
{
temp->output();
}
temp=temp->next;
}
}
menu_person();
return;
}
void correct()
{
cout<<"请输入要修改的房间号码,按0返回:";
int x,number,size;
float price;
cin>>x;
if(x!=0)
{
temp=head;
while(temp!=NULL)
{
if(temp->compare(x))
{
cout<<"房间号码"<<" "<<"房间价格"<<" "<<"房间规格"<<" "<<"是否预定"<<endl;
temp->output();
cout<<"请输入新信息:"<<endl;
cin>>number>>price>>size;
temp->input(number,price,size);
cout<<"OK,修改成功!"<<endl;
break;
}
temp=temp->next;
}
}
menu_work();
return;
}
void load()
{
temp=head;
int number;
unsigned int size;
float price;
int i;
FILE *fp;
char file[10];
cout<<"请输入文件名:";
cin>>file;
if ((fp=fopen(file,"r"))!=NULL)
{
for(i=1;;i++)
{
fscanf(fp,"%d %f %ud",&number,&price,&size);
temp->input(number,price,size);
temp->next=NULL;
tail=temp;
temp->next=new room;
temp->next->forward=temp;
temp=temp->next;
if(number==0) break;
}
cout<<"输入完毕!"<<endl;
delete temp;
tail->next=NULL;
}
else cout<<"读曲错误,空文件!"<<endl;
fclose(fp);
menu();
return;
}
void menu_work()
{
cout<<"请选择"<<endl<<"1 房间列表"<<endl<<"2 按房号查找房间"<<endl<<"3 已定房间列表"
<<endl<<"4 未定房间列表"<<endl<<"5 查看某位旅客定房情况"<<endl<<"6 查询某日定房情况"
<<endl<<"7 修改房间信息"<<endl<<"8 加入房间(存档模式)"<<endl<<"9 加入房间(不存档模式)"
<<endl<<"0 返回上一个菜单"<<endl;
int c;
cin>>c;
switch(c)
{
case 1 : list();
case 2 : find_num();
case 3 : book_list();
case 4 : unbook_list_work();
case 5 : find_name();
case 6 : find_time();
case 7 : correct();
case 8 : save_insert();
case 9 : nosave_insert();
case 0 : {menu();return;}
default :
{
cout<<"输入错误,请重新输入!"<<endl;
menu_work();
}
}
}
void menu_person()
{
cout<<"欢迎光临,请选择"<<endl<<"1 房间列表"<<endl<<"2 按价格查找房间"<<endl
<<"3 按规格查找房间"<<endl<<"4 定房"<<endl<<"0 返回上一个菜单"<<endl;
int c;
cin>>c;
switch(c)
{
case 1 : unbook_list_person();
case 2 : find_price();
case 3 : find_size();
case 4 : book();
case 0 : {menu();return;}
default :
{
cout<<"输入错误,请重新输入!"<<endl;
menu_person();
}
}
}
void menu()
{
cout<<"请问您是旅客还是旅店工作人员?"<<endl;
cout<<"1 旅客"<<endl<<"2 工作人员"<<endl<<"3 读入文件"<<endl;
int c;
cin>>c;
switch(c)
{
case 1 : menu_person();
case 2 : menu_work();
case 3 : load();
default :
{
cout<<"输入错误,请重新输入!"<<endl;
menu();
}
}
}
void main()
{
cout<<"酒店定房系统!"<<endl<<"作者:曹军"<<endl;
temp=new room;
head=temp;
tail=head;
temp->forward=NULL;
menu();
cout<<"欢迎使用,GOODBYE!"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -