Submit: 230 Solved: 116
[ Submit][ Status][ Web Board]
Description
Yang要下海创业,开一家量贩式KTV。现在需要你来帮他编写一个C++程序来实现点歌的功能。至少需要定义如下类及其成员:
1. Song类:歌曲类,具有:
(1)歌曲名属性:一个不含空白符的字符串。
(2)static int getCntOfSongs():获得创建的歌曲的总数。
2. Pop类:是Song类的子类,具有static int getCntOfPops()方法,返回该类型的歌曲的总数。
3. Folk类:是Song类的子类,具有static int getCntOfFolks()方法,返回该类型的歌曲的总数。
4. Bel类:是Song类的子类,具有static int getCntOfBels()方法,返回该类型的歌曲的总数。
5. Singer类:歌手类,具有:
(1)歌手名属性:一个不含空白符的字符串。
(2)所演唱的歌曲列表。
(3)void addASong(string s, int t):添加一首该歌手演唱的歌曲,其中s为歌曲名,t为歌曲类型(1、2、3分别表示Pop、Folk和Bel类的歌曲)。
(4)void singASong(string):根据指定的歌曲名,演唱这首歌。分为两种情况:如果歌手所演唱的歌曲列表中有参数指定的歌曲名,则输出:
$ sings % in # style.
其中:$是歌手名字,%是歌曲名字,#是歌曲类型(为:popular、folk或Bel Canto之一)。
如果指定歌曲名不存在,则输出:
$ doesn’t sing %.
$、%含义同前。
(5)static int getCntOfSingers():获得创建的歌手的人数。
6.SingerList类:点歌单类,具有:
(1)歌手列表。
(2)重载的输入输出运算符。其中重载的输入运算符按照如下格式输入数据:
第1行N>0,表示有N名歌手。之后有N行。
之后的N行,每行是一个歌手及其演唱的歌曲,是一系列用空格分开的数据。每行的第一个字符串(不含空白符)是歌手的名字。第2个是整数M>0,表示该歌手演唱了M首歌曲。M后面有M组输入,每组输入包括1个整数K和1个不含空白符的字符串S,其中K=1、2或者3,分别对应于pop、folk和Bel Canto三种歌曲类型,S是歌曲名。
重载的输出运算符按照如下格式输出歌手及歌曲列表:
每个歌手及其演唱的歌曲占一行,每行的格式为:
$ : %1 %2 …
其中$为歌手名,%1、%2等为该歌手演唱的歌曲名列表,两两之间用1个空格隔开。
(3)void Choose(string s1, string s2)方法:根据参数指定的歌手名s1、歌曲名s2输出。分三种情况:
如果s1指定的歌手不存在,则输出:
Singer $ doesn't exist.
如果s1存在,但是该歌手没有演唱s2指定的歌曲,则输出:
$ doesn’t sing %.
如果s1、s2能够确定某首歌曲,则输出:
$ sings % in # style.
上述$、#和%的含义同前。
Input
输入分为2部分。
第1部分是按照SingerList类的要求输入的歌手及歌曲信息。
第2部分也有多行,每行包括2个字符串,分别是指定的歌手名和歌曲名。
假定所有的歌手名、歌曲名均不相同。
Output
见样例。
Sample Input
3Tom 4 1 ChangJiu 2 DuanZan 3 Main 1 ObjectJack 1 1 OutputMary 2 1 Input 3 GCCTom DuanZanJack InputMary GCCCUI BIGDATA
Sample Output
In beginning, there are 0 singers, and 0 songs.LiuHuan sings XiongDi in popular style.LiuHuan sings SanGuo in folk style.LiuHuan sings SongBie in Bel Canto style.LiuHuan doesn't sing MeiYou.Tom : ChangJiu DuanZan Main ObjectJack : OutputMary : Input GCCNow, there are 4 singers, and 10 songs. Including 5 pop songs, 2 folk songs, and 3 Bel Canto songs.Tom sings DuanZan in folk style.Jack doesn't sing Input.Mary sings GCC in Bel Canto style.Singer CUI doesn't exist.
HINT
Append Code
int
main()
{
cout<<
"In beginning, there are "
;
cout<<Singer::getCntOfSingers()<<
" singers, and "
;
cout<<Song::getCntOfSongs()<<
" songs."
<<endl;
Singer liuh(
"LiuHuan"
);
liuh.addASong(
"XiongDi"
, 1);
liuh.addASong(
"SanGuo"
, 2);
liuh.addASong(
"SongBie"
, 3);
liuh.singASong(
"XiongDi"
);
liuh.singASong(
"SanGuo"
);
liuh.singASong(
"SongBie"
);
liuh.singASong(
"MeiYou"
);
SingerList lst;
string s1, s2;
cin>>lst;
cout<<lst;
cout<<
"Now, there are "
;
cout<<Singer::getCntOfSingers()<<
" singers, and "
;
cout<<Song::getCntOfSongs()<<
" songs. Including "
;
cout<<Pop::getCntOfPops()<<
" pop songs, "
;
cout<<Folk::getCntOfFolks()<<
" folk songs, and "
;
cout<<Bel::getCntOfBels()<<
" Bel Canto songs."
<<endl;
while
(cin>>s1>>s2)
{
lst.Choose(s1, s2);
}
return
0;
}
|
#include <bits/stdc++.h>
using namespace std;
class Song
{
public :
string song_name;
string style;//
int num;
static int song_num;
Song(string s):song_name(s){song_num++;}
static int getCntOfSongs(){return song_num;}
};
class Pop :public Song//继承不用Song s 了;
{
private :
static int pop_num;
public :
Pop(string s):Song(s){pop_num++;num=1;style="popular";}
static int getCntOfPops(){return pop_num;}
};
class Folk :public Song
{
private :
static int folk_num;
public :
Folk(string s):Song(s){folk_num++;num=2;style="folk";}
static int getCntOfFolks(){return folk_num;}
};
class Bel :public Song
{
private :
static int bel_num;
public :
Bel(string s):Song(s){bel_num++;num=3;style="Bel Canto";}
static int getCntOfBels(){return bel_num;}
};
class Singer
{
public :
string singer;
vector<Song> v;
static int singer_num;
Singer(string s):singer(s){singer_num++;}
static int getCntOfSingers(){return singer_num;}
void addASong(string s, int t)
{
switch(t)
{
case 1:
{
Pop song(s);
v.push_back(song);
break;
}
case 2:
{
Folk song(s);
v.push_back(song);
break;
}
case 3:
{
Bel song(s);
v.push_back(song);
break;
}
}
}
void singASong(string s)
{
vector<Song>::iterator it;
for(it = v.begin();it != v.end();it++)
{
if((*it).song_name == s)
{
cout<<singer<<" sings "<<s<<" in "<<(*it).style<<" style."<<endl;
return ;
}
}
cout<<singer<<" doesn't sing "<<s<<"."<<endl;
}
};
class SingerList// public Singer
{
public :
vector<Singer> sing;
SingerList(){}
friend istream& operator >> (istream& is,SingerList &SL)
{
int n,m;
int a,b,c;
string singer,song;
is>>n;
for(int i = 0; i < n; i++)
{
is>>singer;
Singer Sin(singer);//构建歌手
is>>m;
for(int i = 0;i < m; i++)
{
is>>a;
switch(a)
{
case 1:
is>>song;
Sin.addASong(song,1);
break;
case 2:
is>>song;
Sin.addASong(song,2);
break;
case 3:
is>>song;
Sin.addASong(song,3);
break;
}
}
SL.sing.push_back(Sin);//加一个数据;
}
return is;
}
friend ostream& operator << (ostream& os,SingerList &SL)
{
vector<Singer>::iterator it1;
vector<Song>::iterator it2;
for(it1 = SL.sing.begin(); it1 != SL.sing.end(); it1++)//歌单的歌曲
{
os<<(*it1).singer<<" :";
for(it2 = (*it1).v.begin();it2 != (*it1).v.end(); it2++)
{
os<<" "<<(*it2).song_name;//每一个Singer都有一个vector song 属性 ;
}
os<<endl;
}
return os;
}
void Choose(string s1, string s2)
{
vector<Singer>::iterator it1;
vector<Song>::iterator it2;
for(it1 = sing.begin(); it1 != sing.end();it1++){
if((*it1).singer == s1){
for(it2 = (*it1).v.begin(); it2 != (*it1).v.end(); it2++)
{
if((*it2).song_name == s2)
{
cout<<s1<<" sings "<<s2<<" in "<<(*it2).style<<" style."<<endl;
return;
}
else continue;
}
cout<<s1<<" doesn't sing "<<s2<<"."<<endl;
return ;
}
else continue;
}
cout<<"Singer "<<s1<<" doesn't exist."<<endl;
}
};
int Song::song_num = 0;
int Pop::pop_num = 0;
int Folk::folk_num = 0;
int Bel::bel_num = 0;
int Singer::singer_num = 0;
int main()
{
cout<<"In beginning, there are ";
cout<<Singer::getCntOfSingers()<<" singers, and ";
cout<<Song::getCntOfSongs()<<" songs."<<endl;
Singer liuh("LiuHuan");
liuh.addASong("XiongDi", 1);
liuh.addASong("SanGuo", 2);
liuh.addASong("SongBie", 3);
liuh.singASong("XiongDi");
liuh.singASong("SanGuo");
liuh.singASong("SongBie");
liuh.singASong("MeiYou");
SingerList lst;
string s1, s2;
cin>>lst;
cout<<lst;
cout<<"Now, there are ";
cout<<Singer::getCntOfSingers()<<" singers, and ";
cout<<Song::getCntOfSongs()<<" songs. Including ";
cout<<Pop::getCntOfPops()<<" pop songs, ";
cout<<Folk::getCntOfFolks()<<" folk songs, and ";
cout<<Bel::getCntOfBels()<<" Bel Canto songs."<<endl;
while(cin>>s1>>s2)
{
lst.Choose(s1, s2);
}
return 0;
}