【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:\n";
119     s42.settings();
120     s42.onoff();
121     s42.chanup();
122     s42.chanup();
123     cout << "\nAdjusted settings for 42\" TV:\n";
124     s42.settings();
125 
126     Remote grey;
127     grey.set_chan(s42, 10);
128     grey.volup(s42);
129     grey.volup(s42);
130     cout << "\n42\" settings after using remote:\n";
131     s42.settings();
132 
133     Tv s58(Tv::ON);
134     s58.set_mode();
135     grey.set_chan(s58, 28);
136     cout << "\n58\" settings:\n";
137     s58.settings();
138 
139     cout << "\n58\" status:\n";
140     grey.showstatus();
141     s58.set_status(grey);
142     grey.showstatus();
143 }
 1 // chapter15_2_stdexcept.h
 2 
 3 #ifndef LEARN_CPP_CHAPTER15_2_STDEXCEPT_H
 4 #define LEARN_CPP_CHAPTER15_2_STDEXCEPT_H
 5 
 6 #include <stdexcept>
 7 #include <cmath>
 8 
 9 class meanlogicerr : public std::logic_error {
10 public:
11     meanlogicerr() : logic_error("") {}
12     const char * what() {return "bad arguments to hmean() or gmean()";}
13 };
14 
15 #endif //LEARN_CPP_CHAPTER15_2_STDEXCEPT_H
16 
17 // run
18 
19 double hmean(double a, double b) {
20     if (a == b)
21         throw meanlogicerr();
22     return 2.0 * a * b / (a + b);
23 }
24 
25 double gmean(double a, double b) {
26     if (a < 0 || b < 0)
27         throw meanlogicerr();
28     return std::sqrt(a * b);
29 }
30 
31 void ch15_2() {
32     using namespace std;
33     try {cout << "x = 1, y = 1, hmean = " << hmean(1, 1) << endl;}
34     catch (meanlogicerr & me) {cout << me.what() << endl;}
35     try {cout << "x = -1, y = 1, gmean = " << gmean(-1, 1) << endl;}
36     catch (meanlogicerr & me) {cout << me.what() << endl;}
37     try {cout << "x = 1, y = 2, hmean = " << hmean(1, 2) << endl;}
38     catch (meanlogicerr & me) {cout << me.what() << endl;}
39     try {cout << "x = 1, y = 1, gmean = " << gmean(1, 1) << endl;}
40     catch (meanlogicerr & me) {cout << me.what() << endl;}
41 }
 1 // chapter15_3_meanerr.h
 2 
 3 #ifndef LEARN_CPP_CHAPTER15_3_MEANERR_H
 4 #define LEARN_CPP_CHAPTER15_3_MEANERR_H
 5 
 6 #include <stdexcept>
 7 #include <iostream>
 8 #include <string>
 9 
10 class twodouargserr : public std::logic_error {
11 private:
12     double arg1;
13     double arg2;
14     std::string funcname;
15 public:
16     twodouargserr(double a1 = 0, double a2 = 0, const char * f = "none")
17         : arg1(a1), arg2(a2), funcname(f), std::logic_error("") {}
18     virtual const char * what() {return "invalid args";}
19     virtual void msg(){std::cout << funcname << "() logicerr, arg1: " << arg1 << " arg2: " << arg2 << std::endl;}
20 };
21 
22 class hmean_err : public twodouargserr {
23 public:
24     hmean_err(double a1 = 0, double a2 = 0, const char * f = "none")
25             : twodouargserr(a1, a2, f) {}
26     virtual const char * what() {return "invalid args";}
27     virtual void msg(){twodouargserr::msg();}
28 };
29 
30 class gmean_err : public twodouargserr {
31 public:
32     gmean_err(double a1 = 0, double a2 = 0, const char * f = "none")
33             : twodouargserr(a1, a2, f) {}
34     virtual const char * what() {return "invalid args";}
35     virtual void msg(){twodouargserr::msg();}
36 };
37 
38 
39 #endif //LEARN_CPP_CHAPTER15_3_MEANERR_H
40 
41 // run
42 
43 double hmean2(double a, double b) {
44     if (a == b)
45         throw hmean_err(a, b, __func__ );
46     return 2.0 * a * b / (a + b);
47 }
48 
49 double gmean2(double a, double b) {
50     if (a < 0 || b < 0)
51         throw gmean_err(a, b, __func__ );
52     return std::sqrt(a * b);
53 }
54 void ch15_3() {
55     using namespace std;
56     try {cout << "x = 1, y = 1, hmean = " << hmean2(1, 1) << endl;}
57     catch (hmean_err & he) {cout << he.what() << endl; he.msg();}
58     try {cout << "x = 1, y = 2, hmean = " << hmean2(1, 2) << endl;}
59     catch (hmean_err & he) {cout << he.what() << endl; he.msg();}
60     try {cout << "x = -1, y = 1, gmean = " << gmean2(-1, 1) << endl;}
61     catch (gmean_err & ge) {cout << ge.what() << endl; ge.msg();}
62     try {cout << "x = 1, y = 1, hmean = " << gmean2(1, 1) << endl;}
63     catch (gmean_err & ge) {cout << ge.what() << endl; ge.msg();}
64 }

剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。剩下的有空写。。

posted @ 2021-10-22 17:35  开心果壳好硬  阅读(184)  评论(0编辑  收藏  举报