C++OJ电视机与遥控器(友元类)

题目描述

有如下的电视类和遥控器类,遥控器在电视开机的情况下可以控制电视。

 

 

要求如下:

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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荔枝啵啵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值