【程序设计】
---------------------------------------------------------题目:编写一个戏剧类,类说明如下,请实现该类。
运行结果见样张。输出示例语句如下,请复制粘贴使用
booking函数 "余票(",")不足,购买失败"
~opera()函数 "的票房如下:"
样例:
---------------戏曲名: 未命名---------------
包厢 一等座 二等座 三等座
票价 0 0 0 0
总数 20 100 240 300
可售 20 100 240 300
---------------总收入: 0---------------
---------------戏曲名: 群英会---------------
包厢 一等座 二等座 三等座
票价 700 380 180 80
总数 20 100 240 300
可售 12 93 240 285
---------------总收入: 9460---------------
余票(12)不足,购买失败
---------------戏曲名: 群英会---------------
包厢 一等座 二等座 三等座
票价 700 380 180 80
总数 20 100 240 300
可售 12 93 237 287
---------------总收入: 9840---------------
群英会的票房如下:
包厢 一等座 二等座 三等座
售出 8 7 3 13
请按任意键继续. . .
代码如下:
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class opera
{
char name[30];//戏剧名
//有4类座位,包厢/一等座/二等座/三等座,编号为0-3
int ts[4]; // ts[0]:包厢的总数,ts[1]:一等座的总座位数
//ts[2]:二等座的总座位数,ts[3]:三等座的总座位数
int es[4];// es[0]:空包厢的数量,es[1]:一等座的空闲座位数
//es[2]:二等座的空闲座位数,es[3]:三等座的空闲座位数
int ps[4];// ps[0]:包厢的票价,ps[1]:一等座的票价
//ps[2]:二等座的票价,,ps[3]:三等座的票价
int income;//总收入
public:
opera();
void set_name(char *);//更改剧名
void set_ps(int []);//更改座位价格
void booking(int,int);//售票,如果余票小于订票数,
//终止该次售票,并输出购票失败提示信息
void refund(int,int);//退票,不收手续费
void print();
~opera(); //戏剧下线时统计戏剧的票房
};
/**********Program**********/
opera::opera()
{
char ch[30]="未命名";
strcpy(name,ch);
ts[0]=es[0]=20;
ts[1]=es[1]=100;
ts[2]=es[2]=240;
ts[3]=es[3]=300;
for(int j=0;j<4;j++)
{
ps[j]=0;
}
income=0;
}
void opera::set_name(char *ch)
{
strcpy(name,ch);
}
void opera::set_ps(int a[])
{
for(int i=0;i<4;i++)
{
ps[i]=a[i];
}
}
void opera::refund(int a,int b)
{
income-=b*ps[a];
es[a]+=b;
}
void opera::booking(int a,int b)
{
if(b>es[a])
{
cout<<"余票("<<es[a]<<")不足,购买失败"<<endl;
}
else
{
income+=b*ps[a];
es[a]-=b;
}
}
opera::~opera()
{
cout<<name<<"的票房如下:"<<endl;;
cout<<setw(20)<<"包厢"<<setw(10)<<"一等座"
<<setw(10)<<"二等座"<<setw(10)<<"三等座"<<endl;
cout<<setw(10)<<"售出";
for( int i=0;i<4;i++) cout<<setw(10)<<(ts[i]-es[i]);
cout<<endl;
}
/********** End **********/
void opera::print()
{
cout<<"---------------戏曲名:"<<setw(30)<<name
<<"---------------"<<endl;
cout<<setw(20)<<"包厢"<<setw(10)<<"一等座"
<<setw(10)<<"二等座"<<setw(10)<<"三等座"<<endl;
int i;
cout<<setw(10)<<"票价";
for( i=0;i<4;i++) cout<<setw(10)<<ps[i];
cout<<endl;
cout<<setw(10)<<"总数";
for(i=0;i<4;i++) cout<<setw(10)<<ts[i];
cout<<endl;
cout<<setw(10)<<"可售";
for( i=0;i<4;i++) cout<<setw(10)<<es[i];
cout<<endl;
cout<<"---------------总收入:"<<setw(30)<<income
<<"---------------"<<endl;
}
int main()
{
opera p1;
p1.print();
p1.set_name("群英会");//void set_name(char *),更改剧名
int p[]={700,380,180,80};
p1.set_ps(p);//void set_ps(int []),更改座位价格
p1.booking(0,8);//购买8张包厢票
p1.booking(1,7);//购买7张一等票
p1.booking(3,15);//购买15张3等票
p1.print();
p1.booking(0,13);//购买13张包厢票
p1.booking(2,3);//购买3张二等票
p1.refund(3,2);//退2张3等票
p1.print();
return 0;
}