【15分】F. 电视机与遥控器(友元类)

题目描述
有如下的电视类和遥控器类,遥控器在电视开机的情况下可以控制电视。
在这里插入图片描述
要求如下:

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我胡萝北

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值