C++ 简单的飞机乘客选座系统

部署运行你感兴趣的模型镜像

功能:本飞机共有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);
		}
	}
}

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值