【【C++ Primer Plus】编程练习答案——第15章】1 // chapter15_1_tvremote.h23 #ifndef LEARN_CPP_CHAPTER15_1_TVREMOTE_H4 #define LEARN_CPP_CHAPTER15_1_TVREMOTE_H5 class Remote;6 class Tv {7 private:8int state;9int volume; 10int maxchannel; 11int channel; 12int mode; 13int input; 14 public: 15friend class Remote; 16enum {OFF, ON}; 17enum {MinVal, MaxVal = 20}; 18enum {Antenna, Cable}; 19enum {TV, DVD}; 2021Tv(int s = OFF, int mc = 125) : state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {} 22void onoff() {state = (state == ON)? OFF : ON;} 23bool ison() const {return state == ON;} 24bool volup(); 25bool voldown(); 26void chanup(); 27void chandown(); 28void set_mode() {mode = (mode == Antenna)? Cable : Antenna;} 29void set_input() {input = (input == TV)? DVD : TV;} 30void settings() const; 31void set_status(Remote & r); 32 }; 3334 class Remote { 35 private: 36int mode; 37int status; // 常规还是互动模式 38 public: 39friend class Tv; 40enum {Normal, Interacte}; 4142Remote(int m = Tv::TV) : mode(m), status(Normal){} 43bool volup(Tv & t) {return t.volup();} 44bool voldown(Tv & t) {return t.voldown();} 45void onoff(Tv & t) {t.onoff();} 46void chanup(Tv & t) {t.chanup();} 47void chandown(Tv & t) {t.chandown();} 48void set_chan(Tv & t, int c) {t.channel = c;} 49void set_input(Tv & t) {t.set_input();} 50void showstatus() const; 51 }; 525354 #endif //LEARN_CPP_CHAPTER15_1_TVREMOTE_H 555657 // chapter15_1_tvremote.cpp 5859 #include "chapter15_1_tvremote.h" 60 #include <iostream> 6162 bool Tv::volup() { 63if (volume < MaxVal) { 64++ volume; 65return true; 66} 67return false; 68 } 6970 bool Tv::voldown() { 71if (volume > MinVal) { 72-- volume; 73return true; 74} 75return false; 76 } 7778 void Tv::chanup() { 79if (channel < maxchannel) 80++ channel; 81else 82channel = 1; 83 } 8485 void Tv::chandown() { 86if (channel > 1) 87-- channel; 88else 89channel = maxchannel; 90 } 9192 void Tv::settings() const { 93using namespace std; 94cout << "TV is " << (state == OFF? "OFF" : "ON") << endl; 95if (state == ON) { 96cout << "volume setting = " << volume << endl; 97cout << "channel setting = " << channel << endl; 98cout << "mode = " << (mode == Antenna? "antenna" : "cable") << endl; 99cout << "input = " << (input == TV? "TV" : "DVD") << endl;100}101 }102 103 void Tv::set_status(Remote &r) {104if (state == ON)105r.status = r.status == Remote::Interacte? Remote::Normal : Remote::Interacte;106 }107 108 void Remote::showstatus() const {109using namespace std;110cout << "status = " << (status == Interacte? "Interacte" : "Normal") << endl;111 }112 113 // run114 115 void ch15_1() {116using namespace std;117Tv s42;118cout << "Initial settings for 42\" TV:\n";119s42.settings();120s42.onoff();121s42.chanup();122s42.chanup();123cout << "\nAdjusted settings for 42\" TV:\n";124s42.settings();125 126Remote grey;127grey.set_chan(s42, 10);128grey.volup(s42);129grey.volup(s42);130cout << "\n42\" settings after using remote:\n";131s42.settings();132 133Tv s58(Tv::ON);134s58.set_mode();135grey.set_chan(s58, 28);136cout << "\n58\" settings:\n";137s58.settings();138 139cout << "\n58\" status:\n";140grey.showstatus();141s58.set_status(grey);142grey.showstatus();143 } 1 // chapter15_2_stdexcept.h 23 #ifndef LEARN_CPP_CHAPTER15_2_STDEXCEPT_H 4 #define LEARN_CPP_CHAPTER15_2_STDEXCEPT_H 56 #include <stdexcept> 7 #include <cmath> 89 class meanlogicerr : public std::logic_error {10 public:11meanlogicerr() : logic_error("") {}12const char * what() {return "bad arguments to hmean() or gmean()";}13 };14 15 #endif //LEARN_CPP_CHAPTER15_2_STDEXCEPT_H16 17 // run18 19 double hmean(double a, double b) {20if (a == b)21throw meanlogicerr();22return 2.0 * a * b / (a + b);23 }24 25 double gmean(double a, double b) {26if (a < 0 || b < 0)27throw meanlogicerr();28return std::sqrt(a * b);29 }30 31 void ch15_2() {32using namespace std;33try {cout << "x = 1, y = 1, hmean = " << hmean(1, 1) << endl;}34catch (meanlogicerr & me) {cout << me.what() << endl;}35try {cout << "x = -1, y = 1, gmean = " << gmean(-1, 1) << endl;}36catch (meanlogicerr & me) {cout << me.what() << endl;}37try {cout << "x = 1, y = 2, hmean = " << hmean(1, 2) << endl;}38catch (meanlogicerr & me) {cout << me.what() << endl;}39try {cout << "x = 1, y = 1, gmean = " << gmean(1, 1) << endl;}40catch (meanlogicerr & me) {cout << me.what() << endl;}41 } 1 // chapter15_3_meanerr.h 23 #ifndef LEARN_CPP_CHAPTER15_3_MEANERR_H 4 #define LEARN_CPP_CHAPTER15_3_MEANERR_H 56 #include <stdexcept> 7 #include <iostream> 8 #include <string> 9 10 class twodouargserr : public std::logic_error {11 private:12double arg1;13double arg2;14std::string funcname;15 public:16twodouargserr(double a1 = 0, double a2 = 0, const char * f = "none")17: arg1(a1), arg2(a2), funcname(f), std::logic_error("") {}18virtual const char * what() {return "invalid args";}19virtual void msg(){std::cout << funcname << "() logicerr, arg1: " << arg1 << " arg2: " << arg2 << std::endl;}20 };21 22 class hmean_err : public twodouargserr {23 public:24hmean_err(double a1 = 0, double a2 = 0, const char * f = "none")25: twodouargserr(a1, a2, f) {}26virtual const char * what() {return "invalid args";}27virtual void msg(){twodouargserr::msg();}28 };29 30 class gmean_err : public twodouargserr {31 public:32gmean_err(double a1 = 0, double a2 = 0, const char * f = "none")33: twodouargserr(a1, a2, f) {}34virtual const char * what() {return "invalid args";}35virtual void msg(){twodouargserr::msg();}36 };37 38 39 #endif //LEARN_CPP_CHAPTER15_3_MEANERR_H40 41 // run42 43 double hmean2(double a, double b) {44if (a == b)45throw hmean_err(a, b, __func__ );46return 2.0 * a * b / (a + b);47 }48 49 double gmean2(double a, double b) {50if (a < 0 || b < 0)51throw gmean_err(a, b, __func__ );52return std::sqrt(a * b);53 }54 void ch15_3() {55using namespace std;56try {cout << "x = 1, y = 1, hmean = " << hmean2(1, 1) << endl;}57catch (hmean_err & he) {cout << he.what() << endl; he.msg();}58try {cout << "x = 1, y = 2, hmean = " << hmean2(1, 2) << endl;}59catch (hmean_err & he) {cout << he.what() << endl; he.msg();}60try {cout << "x = -1, y = 1, gmean = " << gmean2(-1, 1) << endl;}61catch (gmean_err & ge) {cout << ge.what() << endl; ge.msg();}62try {cout << "x = 1, y = 1, hmean = " << gmean2(1, 1) << endl;}63catch (gmean_err & ge) {cout << ge.what() << endl; ge.msg();}64 }剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。剩下的有空写 。。
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
