C++体育俱乐部积分管理
一个俱乐部有篮球、足球和排球队。
下面给出基类的框架:
class Ball
{
protected:
string opponent;//对手
public:
void display();//显示比赛成绩
}
以Ball
为基类,构建Basketball
, Football
和Volleyball
三个类。
生成上述类并编写主函数,要求主函数中有一个基类Ball指针数组,数组元素不超过20个。
Ball *pb[20]
;
主函数根据输入的信息,相应建立Basketball, Football, Volleyball
类对象。
输入格式:每个测试用例占一行,第一项为类型,1为Basketball
,2为Football
,3为Volleyball
, 第二项是对手名称,第二项是比分。输入0表示输入的结束。
输出时,依次打印对手名称和该场积分数(以万元为单位):Basketball
胜一场得2分,负一场得1分;Football
胜一场得3分,平一场得1分;排球以3:0或3:1胜时,得3分,以3:2胜时,得2分,以2:3负时,得1分,其它情况得0分。
输入样例
1 AAA 108:106
2 BB 2:1
3 CCC 3:2
2 BB 2:2
3 EEE 3:1
3 FFF 2:3
3 CCC 0:3
1 AAA 95:99
0
输出样例
AAA 2
BB 3
CCC 2
BB 1
EEE 3
FFF 1
CCC 0
AAA 1
此题根本就是欺负书呆子,有些同学会误以为它单纯就是只讲排球的其他情况是0分,实际上它还算上了足球0分的情况,至于篮球是没有平局的说话的,所以你懂的。
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
class Ball
{
protected:
string opponent;//对手
int win1;
int win2;
public:
Ball(string n):opponent(n){}
virtual ~Ball(){}
virtual void display()=0;
};
class Basketball:public Ball{
int score;
public:
Basketball(string n,int s):Ball(n),score(s){}
~Basketball(){}
void display()
{
if(score>0)
cout<<opponent<<" "<<2<<endl;
if(score<0)
cout<<opponent<<" "<<1<<endl;
}
};
class Football:public Ball{
int score;
public:
Football(string n,int s):Ball(n),score(s){}
~Football(){}
void display()
{
if(score>0)
cout<<opponent<<" "<<3<<endl;
if(score==0)
cout<<opponent<<" "<<1<<endl;
if(score<0)
cout<<opponent<<" "<<0<<endl;
}
};
class Volleyball:public Ball{
int score;
public:
Volleyball(string n,int s):Ball(n),score(s){}
~Volleyball(){}
void display()
{
if(score==2)
cout<<opponent<<" "<<3<<endl;
else if(score==3)
cout<<opponent<<" "<<3<<endl;
else if(score==1)
cout<<opponent<<" "<<2<<endl;
else if(score==-1)
cout<<opponent<<" "<<1<<endl;
else
cout<<opponent<<" "<<0<<endl;
}
};
int main()
{
Ball *pb[20];
int i=0;
while(1)
{int ch,score=0;
string opponent;
int win1,win2;
cin>>skipws;
cin>>ch;
if(ch==0)break;
cin>>opponent;
if(ch==1)
{
scanf("%d:%d",&win1,&win2);
score+=win1-win2;
pb[i++]=new Basketball(opponent,score);
}
else if(ch==2)
{
scanf("%d:%d",&win1,&win2);
score+=win1-win2;
pb[i++]=new Football(opponent,score);
}
else if(ch==3)
{
scanf("%d:%d",&win1,&win2);
score+=win1-win2;
pb[i++]=new Volleyball(opponent,score);
}
}
int gnum=i;
for(i=0;i<gnum;++i)
{
pb[i]->display();
delete pb[i];
}
return 0;
}