编程练习答案——第15章

【C++ Primer Plus】编程练习答案——第15章

  1 // chapter15_1_tvremote.h
  2 
  3 #ifndef LEARN_CPP_CHAPTER15_1_TVREMOTE_H
  4 #define LEARN_CPP_CHAPTER15_1_TVREMOTE_H
  5 class Remote;
  6 class Tv {
  7 private:
  8     int state;
  9     int volume;
 10     int maxchannel;
 11     int channel;
 12     int mode;
 13     int input;
 14 public:
 15     friend class Remote;
 16     enum {OFF, ON};
 17     enum {MinVal, MaxVal = 20};
 18     enum {Antenna, Cable};
 19     enum {TV, DVD};
 20 
 21     Tv(int s = OFF, int mc = 125) : state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {}
 22     void onoff() {state = (state == ON)? OFF : ON;}
 23     bool ison() const {return state == ON;}
 24     bool volup();
 25     bool voldown();
 26     void chanup();
 27     void chandown();
 28     void set_mode() {mode = (mode == Antenna)? Cable : Antenna;}
 29     void set_input() {input = (input == TV)? DVD : TV;}
 30     void settings() const;
 31     void set_status(Remote & r);
 32 };
 33 
 34 class Remote {
 35 private:
 36     int mode;
 37     int status; // 常规还是互动模式
 38 public:
 39     friend class Tv;
 40     enum {Normal, Interacte};
 41 
 42     Remote(int m = Tv::TV) : mode(m), status(Normal){}
 43     bool volup(Tv & t) {return t.volup();}
 44     bool voldown(Tv & t) {return t.voldown();}
 45     void onoff(Tv & t) {t.onoff();}
 46     void chanup(Tv & t) {t.chanup();}
 47     void chandown(Tv & t) {t.chandown();}
 48     void set_chan(Tv & t, int c) {t.channel = c;}
 49     void set_input(Tv & t) {t.set_input();}
 50     void showstatus() const;
 51 };
 52 
 53 
 54 #endif //LEARN_CPP_CHAPTER15_1_TVREMOTE_H
 55 
 56 
 57 // chapter15_1_tvremote.cpp
 58 
 59 #include "chapter15_1_tvremote.h"
 60 #include <iostream>
 61 
 62 bool Tv::volup() {
 63     if (volume < MaxVal) {
 64         ++ volume;
 65         return true;
 66     }
 67     return false;
 68 }
 69 
 70 bool Tv::voldown() {
 71     if (volume > MinVal) {
 72         -- volume;
 73         return true;
 74     }
 75     return false;
 76 }
 77 
 78 void Tv::chanup() {
 79     if (channel < maxchannel)
 80         ++ channel;
 81     else
 82         channel = 1;
 83 }
 84 
 85 void Tv::chandown() {
 86     if (channel > 1)
 87         -- channel;
 88     else
 89         channel = maxchannel;
 90 }
 91 
 92 void Tv::settings() const {
 93     using namespace std;
 94     cout << "TV is " << (state == OFF? "OFF" : "ON") << endl;
 95     if (state == ON) {
 96         cout << "volume setting = " << volume << endl;
 97         cout << "channel setting = " << channel << endl;
 98         cout << "mode = " << (mode == Antenna? "antenna" : "cable") << endl;
 99         cout << "input = " << (input == TV? "TV" : "DVD") << endl;
100     }
101 }
102 
103 void Tv::set_status(Remote &r) {
104     if (state == ON)
105         r.status = r.status == Remote::Interacte? Remote::Normal : Remote::Interacte;
106 }
107 
108 void Remote::showstatus() const {
109     using namespace std;
110     cout << "status = " << (status == Interacte? "Interacte" : "Normal") << endl;
111 }
112 
113 // run
114 
115 void ch15_1() {
116     using namespace std;
117     Tv s42;
118     cout << "Initial settings for 42" TV:
";
119     s42.settings();
120     s42.onoff();
121     s42.chanup();
122     s42.chanup();
123     cout << "
Adjusted settings for 42" TV:
";
124     s42.settings();
125 
126     Remote grey;
127     grey.set_chan(s42, 10);
128     grey.volup(s42);
129     grey.volup(s42);
130     cout << "
42" settings after using remote:
";
131     s42.settings();
132 
133     Tv s58(Tv::ON);
134     s58.set_mode();
135     grey.set_chan(s58, 28);
136     cout << "
58" settings:
";
137     s58.settings();
138 
139     cout << "
58" status:
";
140     grey.showstatus();
141     s58.set_status(grey);
142     grey.showstatus();
143 }
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 编程练习答案——第15章