题目描述
有如下的电视类和遥控器类,遥控器在电视开机的情况下可以控制电视。
要求如下:
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
代码
#include <iostream>
using namespace std;
class Tv
{
private:
int state, volume, maxchannel, channel, mode, input;
public:
Tv(int s, int vl, int mc, int chn, int md, int ip)
{state = s; volume = vl; maxchannel = mc; channel = chn; mode = md; input = ip;}
void onoff() {state ^= 1;}
bool ison()const {return state;}
bool volup()
{
if(volume < 20) return volume ++;
else return volume;
}
bool voldown()
{
if(volume > 0) return volume --;
else return volume;
}
void chanup() {if(channel < maxchannel) channel ++;}
void chandown() {if(channel > 0) channel --;}
void set_mode() {mode ^= 1;}
void set_input() {input ^= 1;}
void settings() const
{
string State[2]={"off","on"};
string Mode[2]={"Cable","Antenna"};
string Input[2]={"VCR","TV"};
cout << State[state] << " " << volume << " " << channel << " " << Mode[mode] << " " << Input[input] << endl;
}
friend class Remote;
};
class Remote
{
private:
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 state, mode, input;
int volumn, channel;
int s, md, ip;
cin >> state >> volumn >> channel >> mode >> input;
if(state == "off") s = 0;
else s = 1;
if(mode == "Cable") md = 0;
else md = 1;
if(input == "VCR") ip = 0;
else ip = 1;
Tv mytv(s, volumn, 100, channel, md, ip);
Remote myre(md);
string command;
while(cin >> command)
{
if(command == "onoff") myre.onoff(mytv);
else if(mytv.ison())
{
if(command == "volup") myre.volup(mytv);
else if(command == "voldown") myre.voldown(mytv);
else if(command == "chanup") myre.chanup(mytv);
else if(command == "chandown") myre.chandown(mytv);
else if(command == "set_mode") myre.set_mode(mytv);
else if(command == "set_input") myre.set_input(mytv);
}
}
mytv.settings();
return 0;
}