C++ Primer Plus 第十五章 (15章) 编程题答案

习题1 (15.8.1) 细节上“状态成员”没有放在remote类里,切换状态成员的我放在remote里了,原理一样。

  1. //标头.h

  2. #pragma once

  3. #ifndef INTERTV_H_

  4. #define INTERTV_H_

  5. #include <iostream>

  6. using namespace std;

  7. class Tv;

  8. enum Tv_mode { Regular, Interact };

  9. class Remote

  10. {

  11.     friend class Tv;

  12. private:

  13.     

  14. public:

  15.     

  16.     void Show_mode(Tv & t);

  17.     void SET_MODE(Tv & t, Tv_mode form);

  18.     void Change_MODE(Tv & t);

  19. };

  20. class Tv

  21. {

  22.     friend class Remote;

  23. private:

  24.     Tv_mode Mode;

  25. public:

  26.     Tv(Tv_mode M = Regular) {};

  27.     

  28. };   

  29. void Remote::SET_MODE(Tv & t, Tv_mode form )

  30. {

  31.     t.Mode = form;    

  32. }

  33. void Remote::Show_mode(Tv & t)

  34. {

  35.     if (t.Mode == Regular) cout << "Now is Regular mode.\n";

  36.     else cout << "Now is interactive mode.\n";

  37. }

  38. void Remote::Change_MODE(Tv& t)

  39. {

  40.     t.Mode = (t.Mode == Regular) ? Interact : Regular;

  41. }

  42. #endif

  43. //15.8.1.cpp

  44. #include "stdafx.h"

  45. #include "标头.h"

  46. int main()

  47. {

  48.     Tv Tv1;

  49.     Remote Remote1;

  50.     Remote1.Show_mode(Tv1);

  51.     Remote1.Change_MODE(Tv1);

  52.     Remote1.Show_mode(Tv1);

  53.     Remote1.SET_MODE(Tv1, Interact);

  54.     Remote1.Show_mode(Tv1);

  55.     return 0;

  56. }

  1. 习题2   15.8.2
  2. //标头.h
  3. #pragma once
  4. #include <iostream>
  5. #include <stdexcept>
  6. #include <exception>
  7. #include <string>
  8. class bad_hmean :public std::logic_error
  9. {
  10. private:
  11.     double v1;
  12.     double v2;
  13. public:
  14.     bad_hmean(double a = 0, double b = 0) : v1(a), v2(b),logic_error("function name: bad_hmean. Logic Error.") {}
  15.     void mesg();
  16.     const char * what()
  17.     {
  18.         return "function name: bad_hmean. Logic Error.";
  19.     }
  20. };
  21. inline void bad_hmean::mesg()
  22. {
  23.     std::cout << "hmean(" << v1 << ", " << v2 << "): "
  24.         << "invalid arguments: a = -b\n";
  25. }
  26. class bad_gmean :public std::logic_error
  27. {
  28. public:
  29.     double v1;
  30.     double v2;
  31.     bad_gmean(double a = 0, double b = 0) : v1(a), v2(b), logic_error("function name: bad_hmean. Logic Error.") {}
  32.     const char * mesg();
  33.     const char * what()
  34.     {
  35.         return "function name: bad_gmean. Logic Error.";
  36.     }
  37. };
  38. inline const char * bad_gmean::mesg()
  39. {
  40.     return "gmean() arguments should be >= 0\n";
  41. }
  42. // 15.8.2.cpp: 定义控制台应用程序的入口点。
  43. //
  44. #include "stdafx.h"
  45. #include <iostream>
  46. #include <cmath> // or math.h, unix users may need -lm flag
  47. #include "标头.h"
  48. // function prototypes
  49. double hmean(double a, double b);
  50. double gmean(double a, double b);
  51. int main()
  52. {
  53.     using std::cout;
  54.     using std::cin;
  55.     using std::endl;
  56.     double x, y, z;
  57.     cout << "Enter two numbers: ";
  58.     while (cin >> x >> y)
  59.     {
  60.         try {                  // start of try block
  61.             z = hmean(x, y);
  62.             cout << "Harmonic mean of " << x << " and " << y
  63.                 << " is " << z << endl;
  64.             cout << "Geometric mean of " << x << " and " << y
  65.                 << " is " << gmean(x, y) << endl;
  66.             cout << "Enter next set of numbers <q to quit>: ";
  67.         }// end of try block
  68.         catch (bad_hmean & bg)    // start of catch block
  69.         {
  70.             bg.mesg();
  71.             cout << "Try again.\n";
  72.             cout << bg.what();
  73.             continue;
  74.         }
  75.         catch (bad_gmean & hg)
  76.         {
  77.             cout << hg.mesg();
  78.             cout << "Values used: " << hg.v1 << ", "
  79.                 << hg.v2 << endl;
  80.             cout << hg.what();
  81.             cout << "Sorry, you don't get to play any more.\n";
  82.             break;
  83.         } // end of catch block
  84.     }
  85.     cout << "Bye!\n";
  86.     // cin.get();
  87.     // cin.get();
  88.     return 0;
  89. }
  90. double hmean(double a, double b)
  91. {
  92.     if (a == -b)
  93.         throw bad_hmean(a, b);
  94.     return 2.0 * a * b / (a + b);
  95. }
  96. double gmean(double a, double b)
  97. {
  98.     if (a < 0 || b < 0)
  99.         throw bad_gmean(a, b);
  100.     return std::sqrt(a * b);
  101. }
  1. 习题3.  15.8.3
  2. #pragma once
  3. //标头.h
  4. #ifndef STDEXCEPT_H_
  5. #define STDEXCEPT_H_
  6. #include "stdafx.h"
  7. #include <stdexcept>
  8. #include <iostream>
  9. using namespace std;
  10. class bad_hmean :public std::logic_error
  11. {
  12. private:
  13.     double v1;
  14.     double v2;
  15. public:
  16.     bad_hmean(double a = 0, double b = 0) : v1(a), v2(b), logic_error("function name: bad_hmean. Logic Error.") {}
  17.     void mesg();
  18.     void just_report();
  19.     void * what()
  20.     {    
  21.         cout << "function name: bad_hmean.";
  22.         cout << "Values:  (" << v1 << "," << v2 << ")\n";        
  23.     }
  24. };
  25. void bad_hmean::just_report()
  26. {
  27.     std::cout << "hmean(" << v1 << ", " << v2 << "): ";
  28. }
  29. inline void bad_hmean::mesg()
  30. {
  31.     std::cout << "hmean(" << v1 << ", " << v2 << "): "
  32.         << "invalid arguments: a = -b\n";
  33. }
  34. class bad_gmean :public std::logic_error
  35. {
  36. public:
  37.     double v1;
  38.     double v2;
  39.     bad_gmean(double a = 0, double b = 0) : v1(a), v2(b), logic_error("function name: bad_hmean. Logic Error.") {}
  40.     void just_report();
  41.     const char * mesg();
  42.     void * what()
  43.     {
  44.         cout << "function name: bad_hmean.";
  45.         cout << "Values:  (" << v1 << "," << v2 << ")\n";
  46.     }
  47. };
  48. void bad_gmean::just_report()
  49. {
  50.     std::cout << "gmean(" << v1 << ", " << v2 << "): ";
  51. }
  52. inline const char * bad_gmean::mesg()
  53. {
  54.     return "gmean() arguments should be >= 0\n";
  55. }
  56. #endif
  57. // 15.8.3.cpp
  58. #include "stdafx.h"
  59. #include "标头.h"
  60. double hmean(double a, double b);
  61. double gmean(double a, double b);
  62. int main()
  63. {
  64.     using std::cout;
  65.     using std::cin;
  66.     using std::endl;
  67.     double x, y, z;
  68.     cout << "Enter two numbers: ";
  69.     while (cin >> x >> y)
  70.     {
  71.         try {                  // start of try block
  72.             z = hmean(x, y);
  73.             cout << "Harmonic mean of " << x << " and " << y
  74.                 << " is " << z << endl;
  75.             cout << "Geometric mean of " << x << " and " << y
  76.                 << " is " << gmean(x, y) << endl;
  77.             cout << "Enter next set of numbers <q to quit>: ";
  78.         }// end of try block
  79.         catch (std::logic_error & bg)    // start of catch block
  80.         {
  81.             bg.what();
  82.             cout << "End of the loop.";
  83.             
  84.             break;
  85.         }
  86.         
  87.     }
  88.     cout << "Bye!\n";
  89.     // cin.get();
  90.     // cin.get();
  91.     return 0;
  92. }
  93. double hmean(double a, double b)
  94. {
  95.     if (a == -b)
  96.         throw bad_hmean(a, b);
  97.     return 2.0 * a * b / (a + b);
  98. }
  99. double gmean(double a, double b)
  100. {
  101.     if (a < 0 || b < 0)
  102.         throw bad_gmean(a, b);
  103.     return std::sqrt(a * b);
  104. }
  105.  

习题4. 15.8.4

  1. //标头.h
  2. #pragma once
  3. #include <stdexcept>
  4. #include <string>
  5. class Sales
  6. {
  7. public:
  8.     enum { MONTHS = 12 };   // could be a static const
  9.     class bad_index : public std::logic_error
  10.     {
  11.     private:
  12.         int bi;  // bad index value
  13.     public:
  14.         explicit bad_index(int ix,
  15.             const std::string & s = "Index error in Sales object\n");
  16.         int bi_val() const { return bi; }
  17.         virtual ~bad_index() throw() {}
  18.     };
  19.     explicit Sales(int yy = 0);
  20.     Sales(int yy, const double * gr, int n);
  21.     virtual ~Sales() { }
  22.     int Year() const { return year; }
  23.     virtual double operator[](int i) const;
  24.     virtual double & operator[](int i);
  25. private:
  26.     double gross[MONTHS];
  27.     int year;
  28. };
  29. class LabeledSales : public Sales
  30. {
  31. public:
  32.     class nbad_index : public Sales::bad_index
  33.     {
  34.     private:
  35.         std::string lbl;
  36.     public:
  37.         nbad_index(const std::string & lb, int ix,
  38.             const std::string & s = "Index error in LabeledSales object\n");
  39.         const std::string & label_val() const { return lbl; }
  40.         virtual ~nbad_index() throw() {}
  41.     };
  42.     explicit LabeledSales(const std::string & lb = "none", int yy = 0);
  43.     LabeledSales(const std::string & lb, int yy, const double * gr, int n);
  44.     virtual ~LabeledSales() { }
  45.     const std::string & Label() const { return label; }
  46.     virtual double operator[](int i) const;
  47.     virtual double & operator[](int i);
  48. private:
  49.     std::string label;
  50. };
  1. // 源.cpp -- Sales implementation
  2. #include "stdafx.h"
  3. #include "标头.h"
  4. using std::string;
  5. Sales::bad_index::bad_index(int ix, const string & s)
  6.     : std::logic_error(s), bi(ix)
  7. {
  8. }
  9. Sales::Sales(int yy)
  10. {
  11.     year = yy;
  12.     for (int i = 0; i < MONTHS; ++i)
  13.         gross[i] = 0;
  14. }
  15. Sales::Sales(int yy, const double * gr, int n)
  16. {
  17.     year = yy;
  18.     int lim = (n < MONTHS) ? n : MONTHS;
  19.     int i;
  20.     for (i = 0; i < lim; ++i)
  21.         gross[i] = gr[i];
  22.     // for i > n and i < MONTHS
  23.     for (; i < MONTHS; ++i)
  24.         gross[i] = 0;
  25. }
  26. double Sales::operator[](int i) const
  27. {
  28.     if (i < 0 || i >= MONTHS)
  29.         throw bad_index(i);
  30.     return gross[i];
  31. }
  32. double & Sales::operator[](int i)
  33. {
  34.     if (i < 0 || i >= MONTHS)
  35.         throw bad_index(i);
  36.     return gross[i];
  37. }
  38. LabeledSales::nbad_index::nbad_index(const string & lb, int ix,
  39.     const string & s) : Sales::bad_index(ix, s)
  40. {
  41.     lbl = lb;
  42. }
  43. LabeledSales::LabeledSales(const string & lb, int yy)
  44.     : Sales(yy)
  45. {
  46.     label = lb;
  47. }
  48. LabeledSales::LabeledSales(const string & lb, int yy, const double * gr, int n)
  49.     : Sales(yy, gr, n)
  50. {
  51.     label = lb;
  52. }
  53. double LabeledSales::operator[](int i) const
  54. {
  55.     if (i < 0 || i >= MONTHS)
  56.         throw nbad_index(Label(), i);
  57.     return Sales::operator[](i);
  58. }
  59. double & LabeledSales::operator[](int i)
  60. {
  61.     if (i < 0 || i >= MONTHS)
  62.         throw nbad_index(Label(), i);
  63.     return Sales::operator[](i);
  64. }
  1. // 15.8.4.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. // use_sales.cpp  -- nested exceptions
  5. #include <iostream>
  6. #include "标头.h"
  7. int main()
  8. {
  9.     using std::cout;
  10.     using std::cin;
  11.     using std::endl;
  12.     double vals1[12] =
  13.     {
  14.         1220, 1100, 1122, 2212, 1232, 2334,
  15.         2884, 2393, 3302, 2922, 3002, 3544
  16.     };
  17.     double vals2[12] =
  18.     {
  19.         12, 11, 22, 21, 32, 34,
  20.         28, 29, 33, 29, 32, 35
  21.     };
  22.     Sales sales1(2011, vals1, 12);
  23.     LabeledSales sales2("Blogstar", 2012, vals2, 12);
  24.     cout << "First try block:\n";
  25.     try
  26.     {
  27.         int i;
  28.         cout << "Year = " << sales1.Year() << endl;
  29.         for (i = 0; i < 12; ++i)
  30.         {
  31.             cout << sales1[i] << ' ';
  32.             if (i % 6 == 5)
  33.                 cout << endl;
  34.         }
  35.         cout << "Year = " << sales2.Year() << endl;
  36.         cout << "Label = " << sales2.Label() << endl;
  37.         for (i = 0; i <= 12; ++i)
  38.         {
  39.             cout << sales2[i] << ' ';
  40.             if (i % 6 == 5)
  41.                 cout << endl;
  42.         }
  43.         cout << "End of try block 1.\n";
  44.     }
  45.  
  46.     catch (Sales::bad_index & bad)
  47.     {
  48.         Sales::bad_index * pt = &bad;
  49.         if (LabeledSales::nbad_index * ptlb = dynamic_cast<LabeledSales::nbad_index *> (pt))
  50.             cout << "Company: " << ptlb->label_val() << endl;
  51.         cout << bad.what();
  52.         cout << "bad index: " << bad.bi_val() << endl;
  53.     }
  54.     cout << "\nNext try block:\n";
  55.     try
  56.     {
  57.         sales2[2] = 37.5;
  58.         sales1[20] = 23345;
  59.         cout << "End of try block 2.\n";
  60.     }
  61.     catch (LabeledSales::nbad_index & bad)
  62.     {
  63.         cout << bad.what();
  64.         cout << "Company: " << bad.label_val() << endl;
  65.         cout << "bad index: " << bad.bi_val() << endl;
  66.     }
  67.     catch (Sales::bad_index & bad)
  68.     {
  69.         cout << bad.what();
  70.         cout << "bad index: " << bad.bi_val() << endl;
  71.     }
  72.     cout << "done\n";
  73.     // std::cin.get();
  74.     return 0;
  75. }

猜你喜欢

转载自blog.csdn.net/shengda_mao1118/article/details/81511124