// AirTicket.h
/*
#include "stdafx.h"
#include
#include
using namespace std;
*/
class AirTicket
{
private:
static const int CONST_ECONOMY = 4; //经济舱坐席数
static const int CONST_FIRST = 2; //头等舱坐席数
struct
{
int SeatStatus; // 0 空位,1 头等舱,2 经济舱
string PassengerName;
}m_AirSeat[CONST_ECONOMY + CONST_FIRST];
int m_economy = 0; //经济舱坐席使用情况
int m_first = 0; //头等舱坐席使用情况
protected:
bool BookEconomyClass(string PassengerName, int n); //预定经济舱坐席,返回可用的坐席数
bool BookFirstClass(string PassengerName, int n); //预定头等舱坐席,返回可用的坐席数
public:
int AvailableEconomy() //查询是否有空的经济舱坐席,返回可用的坐席数
{
return CONST_ECONOMY - m_economy;
}
int AvailableFirst() //查询是否有空的头等舱坐席,返回可用的坐席数
{
return CONST_FIRST - m_first;
}
bool BoolTicket(string PassengerName, int n = 1, int TicketType = 1) //订票
{
switch (TicketType)
{
case 1:
return BookEconomyClass(PassengerName, n);
break;
case 2:
return BookFirstClass(PassengerName, n);
break;
default:
return false;
break;
}
}
AirTicket()
{
int i;
for (i = 0; i < CONST_ECONOMY + CONST_FIRST; i++)
{
m_AirSeat[i].SeatStatus = 0;
m_AirSeat[i].PassengerName = "";
}
}
~AirTicket(){}
};
// AirTicket.cpp
/*
#include "AirTicket.h"
*/
//预定经济舱坐席,返回可用的坐席数
bool AirTicket::BookEconomyClass(string PassengerName, int n)
{
if (AvailableEconomy() < n) return false;
int i, k;
for (i = 1; i <= n; i++)
{
//将数据记录在坐席表 m_AirSeat 中
k = 0;
while (m_AirSeat[k].SeatStatus)
k++;
m_AirSeat[k].PassengerName = PassengerName;
m_AirSeat[k].SeatStatus = 1; //经济舱
}
m_economy += n; //登记坐席使用情况
return true;
}
//预定头等舱坐席,返回可用的坐席数
bool AirTicket::BookFirstClass(string PassengerName, int n)
{
if (AvailableFirst() < n) return false;
int i, k;
for (i = 1; i <= n; i++)
{
//将数据记录在坐席表 m_AirSeat 中
k = 0;
while (m_AirSeat[k].SeatStatus)
k++;
m_AirSeat[k].PassengerName = PassengerName;
m_AirSeat[k].SeatStatus = 2; //头等舱
}
m_first += n; //登记坐席使用情况
return true;
}
//main.cpp
/*
#include "stdafx.h"
#include
#include
using namespace std;
*/
int _tmain(int argc, _TCHAR* argv[])
{
AirTicket a;
char ch;
int i, x;
string s;
for (;;)
{
system("cls");
cout << "\n\n 小航空公司刚刚购买了一台计算机的新的自动订票系统\n\n";
cout << "******************************************************************\n";
cout << "\n 预约请输入 0\n";
cout << "\n 查询请输入 1\n";
cout << "\n 退出请输入 2\n";
cout << "\n******************************************************************\n\n";
cout << "请选择:";
cin >> ch;
switch (ch)
{
case '0':
system("cls");
cout << "当前坐席情况(可预定):经济舱 " << a.AvailableEconomy() << " 坐,头等舱 " << a.AvailableFirst() << " 坐\n";
cout << "请输入姓名:";
cin >> s;
cout << "\n请输入数目:";
cin >> i;
cout << "\n请输入坐席类型(1 经济舱, 2 头等舱):";
cin >> x;
/*
if (x == 1)
{
if (a.AvailableEconomy < i)
{
cout << "";
}
}
else
{
}
*/
//时间原因,当第一类部分满了,你的程序应该问的人,如果它是可以接受的放在经济区(反之亦然)。等功能就不实现了
//利用已经写好的类,实现起来不是很难。
//这里假设输入的数据是合理的
//订票成功
a.BoolTicket(s, i, x);
break;
case '1':
//查询是否有空的经济舱坐席,时间原因,抱歉,请自行完成
break;
case '2':
return 0;
break;
default:
break;
}
}
system("pause"); printf("\n\n");
return 0;
}
#include
#include
#include
using namespace std;
class Seat
{
int ordered;
char name[30];
public:
int type; //1 first 0 economy
Seat()
{
ordered=0;
name[0]='\0';
}
Seat(int type)
{
this->type=type;
ordered=0;
name[0]='\0';
}
void setName(char * ch)
{
strcpy(name,ch);
}
void setType(int t)
{
type=t;
}
void setOrder(int o)
{
ordered=o;
}
int getOrder()
{
return ordered;
}
char* getName()
{
return name;
}
};
void Reservation(Seat *seat,int n)
{
char name[30];
int type;
int num;
cout<<"Please input the fllowing infomation"<
fflush(stdin);
gets(name);
cout<<"First class (press '1') or Enconomy class (press '0')?";
cin>>type;
cout<<"Number of seats:";
cin>>num;
int fNum=0,eNum=0,order=0;
for(int i=0;i
if(seat[i].getOrder()==0) order++;
if(seat[i].type==1&&seat[i].getOrder()==0) fNum++;
if(seat[i].type==0&&seat[i].getOrder()==0) eNum++;
}
cout<<"order:"<
if(type==1&&num<=fNum)
{
for(int i=0;i
if(seat[i].type==1&&seat[i].getOrder()==0)
{
seat[i].setName(name);
seat[i].setOrder(1);
num--;
}
}
cout<<"Reserved. "<
else if(type==1&&num>fNum)
{
int ch;
cout<<"Not enough seats! Do you accept some seats are economy class?"<
cin>>ch;
if(ch==0) return;
else
{
for(int i=0;i
if(seat[i].type==1)
{
seat[i].setName(name);
seat[i].setOrder(1);
}
}
int temp=num-fNum;
for(int i=0;i
if(seat[i].type==0)
{
seat[i].setName(name);
seat[i].setOrder(1);
temp--;
}
}
cout<<"Reserved."<
}
else if(type==0&&num
for(int i=0;i
if(seat[i].type==0)
{
seat[i].setName(name);
seat[i].setOrder(1);
num--;
}
}
}
else if(type==1&&num>eNum)
{
int ch;
cout<<"Not enough seats! Do you accept some seats are first class?"<
cin>>ch;
if(ch==0) return;
else
{
for(int i=0;i
if(seat[i].type==0)
{
seat[i].setName(name);
seat[i].setOrder(1);
}
}
int temp=num-eNum;
for(int i=0;i
if(seat[i].type==0)
{
seat[i].setName(name);
seat[i].setOrder(1);
temp--;
}
}
cout<<"Reserved."<
}
}
void Check(Seat* seat,int n)
{
int num=1;
cout<<"Fist class:"<
if(seat[i].getOrder()==1&&seat[i].type==1)
cout<<"Seat "<
cout<<"Economy class:"<
if(seat[i].getOrder()==1&&seat[i].type==0)
cout<<"Seat "<
}
void init(Seat seat[],int n)
{
for(int i=0;i
if(i
}
}
int main()
{
int service;
cout<<"Reservation (press '0'), Check (press '1'), or Exit (press '2')"<
init(seat,6);
while(cin>>service)
{
if(service==2) break;
else if(service==0)
{
Reservation(seat,6);
}
else if(service==1) Check(seat,6);
else
{
cout<
continue;
}
cout<
}
return 0;
}