C++ Primer Plus 第十五章 课后编程练习题1-

//1题////////////////////////////////////////////////////////////////////////////////////////////
//类定义
#ifndef tv_remote_hpp
#define tv_remote_hpp
class Remote;
class Tv
{
public:
friend class Remote;
enum State{Off, On}; //开关
enum {MinVal, MaxVal = 20}; //频道
enum {Antenna, Cable}; //有线, 无线
enum {TV, DVD}; //电视, DVD
enum {Routine, Interaction}; //遥控器 常规状态, 互动状态
Tv(int s = Off, int mc = 125) : state(s), volume(5),
maxchannel(mc), channel(2), mode(Cable), input(TV) {}
void onoff() {state ^= 1;} //切换开关
bool ison() const {return state == On;}; //返回开关
bool volup(); //增加音量
bool voldown(); //减小音量
void chanup(); //频道+1
void chandown(); //频道-1
void set_mode() {mode ^= 1;} //切换有线,无线
void set_input() {input ^= 1;} //切换TV, DVD
void settings() const; //显示当前设置
void set_pattern(Remote & r); //遥控器常规状态, 互动状态切换
private:
int state; //开关
int volume; //音量
int maxchannel; //最大频道数
int channel; //当前频道
int mode; //有线, 无线
int input; //电视, DVD
};
class Remote
{
public:
friend class Tv;
Remote(int m = Tv::TV, int p = Tv::Routine) :mode(m), pattern§ {}
bool volup(Tv & t) {return t.volup();} //音量+1
bool voldown(Tv & t) {return t.voldown();} //音量-1
void onoff(Tv & t) {t.onoff();} //开关
void chanup(Tv & t) {t.chanup();} //频道+1
void chandown(Tv & t) {t.chandown();} //频道-1
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();} //切换TV,DVD
void set_pattern() ; //切换 状态
void show_pattern(); //显示
private:
int mode; //TV,DVD
int pattern; //常规模式, 互动模式
};

#endif /* tv_remote_hpp */

//类函数定义
#include “tv_remote.hpp”
#include
using std::cout;
using std::endl;
bool Tv::volup()
{
if (volume < MaxVal)
{
volume++;
return true;
}
else
return false;
}

bool Tv::voldown()
{
if (volume > MinVal)
{
volume–;
return true;
}
else
return false;
}

void Tv::chanup()
{
if (channel < maxchannel)
channel++;
else
channel = 1;
}

void Tv::chandown()
{
if (channel > 1)
channel–;
else
channel = maxchannel;
}

void Tv::set_pattern(Remote & r)
{
if (input == TV)
r.set_pattern();
}

void Tv::settings()const
{

cout <<"TV is " << (state == Off? "Off" : "On") << endl;
if (state == On)
{
    cout <<"Volume setting = " << volume <<endl;
    cout <<"Channel setting = " << channel << endl;
    cout <<"Mode = "
    << (mode == Antenna? "antenna" :"cable") << endl;
    cout <<"Input = "
    <<(input == TV? "TV" : "DVD")<<endl;
}

}

void Remote::set_pattern()
{
if (mode == Tv::TV)
pattern ^= 1;
}

void Remote::show_pattern()
{

if (mode == Tv::TV)
    cout <<"Remote Pattern = "<< (pattern == 0? "Routine" :"Interaction")
    << endl;

}
//测试小程序
#include
#include “tv_remote.hpp”
int main()
{
using std::cout;
Tv s42;
cout <<“Initial settings for 42” TV:\n";
s42.settings();
s42.onoff();
s42.chanup();
cout <<“Adjusted settings for 42” TV:\n";
s42.settings();

Remote grey;

grey.set_chan(s42, 10);
grey.volup(s42);
grey.volup(s42);
cout <<"\n42\" settings after using remote:\n";
grey.show_pattern();
s42.settings();

Tv s58(Tv::On);
s58.set_pattern(grey);
s58.set_mode();
grey.set_chan(s58, 28);
cout <<"\n58\" settings:\n";
grey.show_pattern();
s58.settings();
return 0;

}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/105448360
今日推荐