功能:本飞机共有100个座位,分20排,每排5个位子,编号为A、B、C、D、E,
如10A表示10排A座,A和E靠窗,本系统可让乘客自己选择座号,直到乘客满意为止,
无法满足的话,只能改乘另一个航班。定上票的乘客需给出姓名和身份证号,
最后要打印出乘客清单。
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
//定义一个乘客类
class CPassanger
{
private:
string sId;//身份证
string sName;//姓名
public:
CPassanger()
{
sId="";
sName="";
}
CPassanger(string id,string name)
{
sId = id;
sName = name;
}
string GetID() {return sId;}
string GetName() {return sName;}
};
class CPassangerMan //乘客管理类
{
private:
CPassanger *pPass[20][5];//所有座位的乘客情况
public:
CPassangerMan()
{
//初始化乘客情况
int i,j;
for(i=0;i<20;i++)
for(j=0;j<5;j++)
pPass[i][j] = NULL;
}
~CPassangerMan()
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<5;j++)
if(pPass[i][j] != NULL)
delete pPass[i][j];
}
bool SelectSeat(int nRow,char chCol,string sID,string sName)
{
if(!isFreeSeat(nRow,chCol))
return false;
CPassanger *pPassanger = new CPassanger(sID,sName);
pPass[nRow-1][chCol-'A'] = pPassanger;
return true;
}
bool CancelSeat(string sID)
{
for(int i=0;i<20;i++)
for(int j=0;j<5;j++)
{
if(pPass[i][j] != NULL && pPass[i][j]->GetID() == sID)
{
delete pPass[i][j];
pPass[i][j] = NULL;
return true;
}
}
return false;
}
bool QuerySeat(string sID,int &nRow,char &chCol)
{
for(int i=0;i<20;i++)
for(int j=0;j<5;j++)
{
if(pPass[i][j] != NULL && pPass[i][j]->GetID() == sID)
{
nRow = i+1;
chCol = 'A' + j;
return true;
}
}
return false;
}
void DisplaySeats()
{
for(int i=0;i<20;i++)
for(int j=0;j<5;j++)
{
int nRow = i+1;
char chRol = 'A'+j;
cout<<nRow<<chRol<<'\t';
if(pPass[i][j] == NULL)
cout<<"_____"<<'\t'<<"_____"<<'\t';
else
cout<<pPass[i][j]->GetID()<<'\t'<<pPass[i][j]->GetName();
}
cout<<endl;
}
bool getFreeSeat()
{
for(int i=0;i<20;i++)
for(int j=0;j<5;j++)
{
if(pPass[i][j] == NULL)
{
return true;
}
}
return false;
}
bool isFreeSeat(int nRow,char chCol)
{
if(pPass[nRow-1][chCol - 'A'] == NULL)
return true;
return false;
}
bool isPassangerIDUsed(string sID)
{
for(int i=0;i<20;i++)
for(int j=0;j<5;j++)
{
if(pPass[i][j] != NULL && pPass[i][j]->GetID() == sID)
return true;
}
return false;
}
};
int main()
{
CPassangerMan passMan;
string sID,sName;
int nRow;
char chCol;
while(true)
{
cout<<"|------------ 乘客选座系统 -----------|"<<endl
<<"|------------ 请输入选项编号(0-4) -----------|"<<endl
<<" | 1-选择飞机座位 | "<<endl
<<" | 2-取消飞机座位 | "<<endl
<<" | 3-查询乘客座位 | "<<endl
<<" | 4-显示乘客座位 | "<<endl
<<" | 0-退出! | "<<endl
<<"|------------------------------------------------|"<<endl;
int nChoice = 0;
cin>>nChoice;
switch(nChoice)
{
case 1:
{
if(!passMan.getFreeSeat())
{
cout<<"本次飞机已满客!"<<endl;
break ;
}
while(true)
{
cout << "请输入座位号(数字1~20 字母A~E): ";
cin>>nRow>>chCol;
if ( (nRow < 1 || nRow > 20) ||
(chCol < 'A' || chCol > 'E'))
{
cout << "请输入正确的座位信息!" << endl;
continue;
}
if (!passMan.isFreeSeat(nRow,chCol))
{
cout << "座位已被预定,请重新选择!" << endl;
continue;
}
else
{
cout << "座位有效,请输入身份证号和姓名:";
cin>>sID>>sName;
while(passMan.isPassangerIDUsed(sID)) //身份证号码不能重复
{
cout << "身份证号码已经被使用,请重新输入:";
cin>>sID;
}
if(passMan.SelectSeat(nRow,chCol,sID,sName))
{
cout << "座位预定成功!" <<endl;
break;
}
else
continue;
}
}
}
break;
case 2://取消座位
cout << "请输入需要取消座位的乘客身份证:";
cin>>sID;
if(!passMan.CancelSeat(sID))
{
cout<<"指定的身份证号码并没有选座。"<<endl;
}
break;
case 3://查询座位
cout << "请输入查询座位号的乘客身份证:";
cin>>sID;
if(!passMan.QuerySeat(sID,nRow,chCol))
{
cout<<"指定的身份证号码并没有选座。"<<endl;
}
else
{
cout<<"身份证号为"<<sID<<"的乘客座位号是"<<nRow<<chCol<<endl;
}
break;
case 4:
passMan.DisplaySeats(); //显示座位情况
break;
default:
exit(1);
}
}
}

被折叠的 条评论
为什么被折叠?



