题目描述
有如下的电视类和遥控器类,遥控器在电视开机的情况下可以控制电视。
要求如下:
1.实现并完善Tv类;其中构造函数需修改和完善。另:最大频道为100;
2.将Remote设为Tv的友元类,以支持在Remote类中对Tv方法的调用。
3.在main函数中,通过Remote实例对TV实例进行操作。
输入
第一行,电视初始状态,依次为state,volume,channel,mode,input的初始值。
第二行,利用遥控器对上述状态的操作指令,用对应的函数名表示,如增加音量为volup
输出
第一行,执行遥控器操作后的状态。
输入样例1
off 10 19 Cable VCR
onoff volup chanup set_mode set_input
输出样例1
on 11 20 Antenna TV
AC代码如下
#include<iostream>
#include<string>
using namespace std;
class Tv
{ int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
string sstate[2]={"off","on"};
string smode[2]={"Cable","Antenna"};
string sinput[2]={"VCR","TV"};
public:
Tv():state(0),volume(0),maxchannel(100),channel(0),mode(0),input(0){}
Tv(int s, int v,int c,int m,int i):state(s),maxchannel(100),volume(v),channel(c),mode(m),input(i){}
void onoff()
{
state=1-state;
}
bool ison()const
{
return state?true:false;
}
bool volup()
{
if(volume<20)
return ++volume;
}
bool voldown()
{
if(volume>0)
return --volume;
}
void chanup()
{
channel++;
}
void chandown()
{
channel--;
}
void set_mode()
{
mode=1-mode;
}
void set_input()
{
input=1-input;
}
void settings()const
{
cout << sstate[state] << " "<<volume << " " << channel << " "<< smode[mode] << " "<< sinput[input] <<endl;
}
friend class Remote;
};
class Remote
{
int mode;
public:
Remote(int m):mode(m){}
bool volup(Tv &t)
{ return t.volup(); }
bool voldown(Tv &t)
{ return t.voldown(); }
void onoff(Tv &t)
{ t.onoff(); }
void chanup(Tv &t)
{ t.chanup(); }
void chandown(Tv &t)
{ t.chandown(); }
void set_chan(Tv &t,int c)
{ t.channel = c; }
void set_mode(Tv &t)
{ t.set_mode(); }
void set_input(Tv &t)
{ t.set_input(); }
};
int main()
{
string s, m, i;
int s1, m1, i1;
int v,c;
cin >> s >> v >> c >> m >> i;
s1=(s == "off")?0:1;
m1=(m == "Cable")?0:1;
i1=(i == "VCR")?0:1;
Remote tv(m1);
Tv t(s1, v, c, m1, i1);
string s2, v2, c2, m2, i2;
cin >> s2 >> v2 >> c2 >> m2 >> i2;
if (s2 == "onoff")
tv.onoff(t);
if (!t.ison()) { //电视处于关机状态,则直接输出信息,然后结束
t.settings();
return 0;
}
if (v2 == "volup")
tv.volup(t);
else
tv.voldown(t);
if (c2 == "chanup")
tv.chanup(t);
else
tv.chandown(t);
if (m2 == "set_mode")
tv.set_mode(t);
if (i2 == "set_input")
tv.set_input(t);
t.settings();
return 0;
}