• C++ primer plus第十五章编程练习答案


    1.对Tv 和 Remote 类做如下修改:
    a,让它们互为友元;
    b.在 Remote类中添加一个状态变量成员,该成员描述遥控器是处于常规模式还是互动模式:
    c,在 Remote 中添加一个显示模式的方法
    d.在T类中添加一个对 Remote 中新成员进行切换的方法,该方法应仅当 TV 处于打开状态时才能运行。
    编写一个小程序来测试这些新特性。

    1. #include
    2. #include "tv.h"
    3. int main()
    4. {
    5. using std::cout;
    6. Tv s42;
    7. cout << "Initial settings for 42\" TV:\n";
    8. s42.settings();
    9. s42.onoff();
    10. s42.chanup();
    11. cout << "\nAdjusted settings for 42\" TV:\n";
    12. s42.settings();
    13. Remote grey;
    14. grey.set_chan(s42, 10);
    15. grey.volup(s42);
    16. grey.volup(s42);
    17. s42.set_status(grey);
    18. cout << "\n42\" settings after using remote:\n";
    19. s42.settings();
    20. grey.show_status();
    21. Tv s58(Tv::On);
    22. s58.set_mode();
    23. grey.set_chan(s58, 28);
    24. s58.set_status(grey);
    25. cout << "\n58\" settings:\n";
    26. s58.settings();
    27. grey.show_status();
    28. return 0;
    29. }
    1. #ifndef TV_H_
    2. #define TV_H_
    3. #include
    4. class Remote; //先声明Remote类然后再定义, 以便编译器有足够的信息编译使用Remote类的方法;
    5. class Tv
    6. {
    7. public:
    8. friend class Remote;
    9. enum { Off, On };
    10. enum { MinVal, MaxVal = 20 };
    11. enum { Antenna, Cable };
    12. enum { TV, DVD };
    13. Tv(int s = Off, int mc = 125) : state(s), volume(5),
    14. maxchannel(mc), channel(2), mode(Cable), input(TV) {}
    15. void onoff() { state = (state == On) ? Off : On; }
    16. bool ison() const { return state == On; }
    17. bool volup();
    18. bool voldown();
    19. void chanup();
    20. void chandown();
    21. void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
    22. void set_input() { input = (input == TV) ? DVD : TV; }
    23. void settings() const;
    24. void set_status(Remote &r); //对Remote类中状态成员进行切换的方法;
    25. private:
    26. int state;
    27. int volume;
    28. int maxchannel;
    29. int channel;
    30. int mode;
    31. int input;
    32. };
    33. class Remote
    34. {
    35. private:
    36. int mode;
    37. int status; //状态变量成员;
    38. public:
    39. friend class Tv;
    40. enum { Convention, Interaction }; //Convention表示常规模式, Interaction表示互动模式;
    41. Remote(int m = Tv::TV, int s = Convention) : mode(m), status(s) {} //默认为常规模式;
    42. bool volup(Tv &t) { return t.volup(); }
    43. bool voldown(Tv &t) { return t.voldown(); }
    44. void onoff(Tv &t) { t.onoff(); }
    45. void chanup(Tv &t) { t.chanup(); }
    46. void chandown(Tv &t) { t.chandown(); }
    47. void set_chan(Tv &t, int c) { t.channel = c; }
    48. void set_mode(Tv &t) { t.set_mode(); }
    49. void set_input(Tv &t) { t.set_input(); }
    50. void show_status() const;
    51. };
    52. inline void Tv::set_status(Remote &r)
    53. {
    54. if (ison())
    55. {
    56. r.status = Remote::Convention == r.status ? Remote::Interaction : Remote::Convention;
    57. }
    58. }
    59. inline void Remote::show_status() const
    60. {
    61. std::cout << "Status = " << (status == Convention ? "Convention" : "Interaction") << std::endl;
    62. }
    63. #endif
    1. #include
    2. #include "tv.h"
    3. bool Tv::volup()
    4. {
    5. if (volume < MaxVal)
    6. {
    7. volume++;
    8. return true;
    9. }
    10. return false;
    11. }
    12. bool Tv::voldown()
    13. {
    14. if (volume > MinVal)
    15. {
    16. volume--;
    17. return true;
    18. }
    19. return false;
    20. }
    21. void Tv::chanup()
    22. {
    23. channel = channel < maxchannel ? 1 + channel : 1;
    24. }
    25. void Tv::chandown()
    26. {
    27. channel = channel > 1 ? channel - 1 : maxchannel;
    28. }
    29. void Tv::settings() const
    30. {
    31. using std::cout;
    32. using std::endl;
    33. cout << "TV is " << (state == Off ? "Off" : "On") << endl;
    34. if (state == On)
    35. {
    36. cout << "Volume setting = " << volume << endl;
    37. cout << "Channel setting = " << channel << endl;
    38. cout << "Mode = ";
    39. cout << (mode == Antenna ? "antenna" : "cable") << endl;
    40. cout << "Input = ";
    41. cout << (input == TV ? "TV" : "DVD") << endl;
    42. }
    43. }

    2.修改程序清单 15.11,使两种异常类型都是从头文件stdexcepi>提供的 logic_error 类派生出来的类让每个 what()方法都报告函数名和问题的性质。异常对象不用存储错误的参数值,而只需支持 what()方法

    1. #include
    2. #include
    3. #include "exc_mean.h"
    4. double hmean(double a, double b);
    5. double gmean(double a, double b);
    6. int main()
    7. {
    8. using std::cin;
    9. using std::cout;
    10. using std::endl;
    11. double x, y, z;
    12. cout << "Enter two numbers: ";
    13. while (cin >> x >> y)
    14. {
    15. try
    16. {
    17. z = hmean(x, y);
    18. cout << "Harmonic mean of " << x << " and " << y;
    19. cout << " is " << z << endl;
    20. z = gmean(x, y);
    21. cout << "Geometric mean of " << x << " and " << y;
    22. cout << " is " << z << endl;
    23. cout << "Enter next set of numbers : ";
    24. }
    25. catch (bad_hmean &bg)
    26. {
    27. cout << bg.what(); //调用what方法打印hmean中的异常;
    28. cout << "Try again.\n";
    29. continue;
    30. }
    31. catch (bad_gmean &hg)
    32. {
    33. cout << hg.what(); //调用what方法打印gmean中的异常;
    34. cout << "Sorry, you don't get to play any more.\n";
    35. break;
    36. }
    37. }
    38. cout << "Bye!\n";
    39. return 0;
    40. }
    41. double hmean(double a, double b)
    42. {
    43. if (a == -b)
    44. {
    45. throw bad_hmean();
    46. }
    47. return 2.0 * a * b / (a + b);
    48. }
    49. double gmean(double a, double b)
    50. {
    51. if (a < 0 || b < 0)
    52. {
    53. throw bad_gmean();
    54. }
    55. return std::sqrt(a * b);
    56. }
    1. #ifndef EXC_MEAN_H_
    2. #define EXC_MEAN_H_
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. class bad_hmean : public logic_error
    8. {
    9. public:
    10. explicit bad_hmean(const string &s = "Error in bad_hmean object\nhmean() arguments should be a != -b\n") : logic_error(s) {}
    11. };
    12. class bad_gmean : public logic_error
    13. {
    14. public:
    15. explicit bad_gmean(const string &s = "Error in bad_hmean object\ngmean() arguments should be >= 0\n") : logic_error(s) {}
    16. };
    17. #endif

    3.这个练习与编程练习2相同,但异常类是从一个这样的基类派生而来的:它是从 logic_crror 派生而来的,并存储两个参数值。异常类应该有一个这样的方法:报告这些值以及函数名。程序使用一个 catch块来捕获基类异常,其中任何一种从该基类异常派生而来的异常都将导致循环结束。

    1. #include
    2. #include
    3. #include "exc_mean.h"
    4. double hmean(double a, double b);
    5. double gmean(double a, double b);
    6. int main()
    7. {
    8. using std::cin;
    9. using std::cout;
    10. using std::endl;
    11. double x, y, z;
    12. cout << "Enter two numbers: ";
    13. while (cin >> x >> y)
    14. {
    15. try
    16. {
    17. z = hmean(x, y);
    18. cout << "Harmonic mean of " << x << " and " << y;
    19. cout << " is " << z << endl;
    20. z = gmean(x, y);
    21. cout << "Geometric mean of " << x << " and " << y;
    22. cout << " is " << z << endl;
    23. cout << "Enter next set of numbers : ";
    24. }
    25. catch (mean &m) //捕获基类以及派生类的任何异常;
    26. {
    27. cout << "Sorry, you don't get to play any more.\n";
    28. break;
    29. }
    30. }
    31. cout << "Bye!\n";
    32. return 0;
    33. }
    34. double hmean(double a, double b)
    35. {
    36. if (a == -b)
    37. {
    38. bad_hmean temp("hmean", a, b);
    39. temp.mesg();
    40. throw temp;
    41. }
    42. return 2.0 * a * b / (a + b);
    43. }
    44. double gmean(double a, double b)
    45. {
    46. if (a < 0 || b < 0)
    47. {
    48. bad_gmean temp("gmean", a, b);
    49. temp.mesg();
    50. throw temp;
    51. }
    52. return std::sqrt(a * b);
    53. }
    1. #ifndef EXC_MEAN_H_
    2. #define EXC_MEAN_H_
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. class mean : public logic_error
    8. {
    9. private:
    10. double v1;
    11. double v2;
    12. public:
    13. mean(const string &s = "none", double a = 0.0, double b = 0.0) : logic_error(s), v1(a), v2(b) {}
    14. const double &val1() const { return v1; }
    15. const double &val2() const { return v2; }
    16. };
    17. class bad_hmean : public mean
    18. {
    19. public:
    20. bad_hmean(const string &s = "none", double a = 0.0, double b = 0.0) : mean(s, a, b) {}
    21. void mesg() const;
    22. };
    23. inline void bad_hmean::mesg() const
    24. {
    25. cout << "hmean(" << val1() << ", " << val2() << "):" << endl;
    26. cout << "hmean() arguments should be a != b" << endl;
    27. }
    28. class bad_gmean : public mean
    29. {
    30. public:
    31. bad_gmean(const string &s = "none", double a = 0.0, double b = 0.0) : mean(s, a, b) {}
    32. void mesg() const;
    33. };
    34. inline void bad_gmean::mesg() const
    35. {
    36. cout << "gmean(" << val1() << ", " << val2() << "):" << endl;
    37. cout << "gmean() arguments should be >= 0" << endl;
    38. }
    39. #endif

    4.程序清单 15.16 在每个 ty 后面都使用两个 catch 块,以确保 nbad index 异常导致方法 abel_val()被调用。请修改该程序在每个 ty 块后面只使用一个 catch 块并使用 RTTI 来确保合适时调用 abel_val()

    1. #include
    2. #include "sales.h"
    3. int main()
    4. {
    5. using std::cin;
    6. using std::cout;
    7. using std::endl;
    8. double vals1[12] =
    9. {
    10. 1220, 1100, 1122, 2212, 1232, 2334,
    11. 2884, 2393, 3302, 2922, 3002, 3544
    12. };
    13. double vals2[12] =
    14. {
    15. 12, 11, 22, 21, 32, 34,
    16. 28, 29, 33, 29, 32, 35
    17. };
    18. Sales sales1(2011, vals1, 12);
    19. LabeledSales sales2("Blogstar", 2012, vals2, 12);
    20. cout << "First try block:\n";
    21. try
    22. {
    23. int i;
    24. cout << "Year = " << sales1.Year() << endl;
    25. for (i = 0; i < 12; ++i)
    26. {
    27. cout << sales1[i] << ' ';
    28. if (i % 6 == 5)
    29. {
    30. cout << endl;
    31. }
    32. }
    33. cout << "Year = " << sales2.Year() << endl;
    34. cout << "Label = " << sales2.Label() << endl;
    35. for (i = 0; i <= 12; ++i)
    36. {
    37. cout << sales2[i] << ' ';
    38. if (i % 6 == 5)
    39. {
    40. cout << endl;
    41. }
    42. }
    43. cout << "End of try block 1.\n";
    44. }
    45. catch (Sales::bad_index &bad)
    46. {
    47. cout << bad.what();
    48. LabeledSales::nbad_index *ni;
    49. if (ni = dynamic_cast(&bad)) //RTTI识别类型;
    50. {
    51. cout << "Company: " << ni->label_val() << endl;
    52. cout << "bad index: " << ni->bi_val() << endl;
    53. }
    54. else
    55. {
    56. cout << "bad index: " << bad.bi_val() << endl;
    57. }
    58. }
    59. cout << "\nNext try block:\n";
    60. try
    61. {
    62. sales2[2] = 37.5;
    63. sales1[20] = 23345;
    64. cout << "End of try block 2.\n";
    65. }
    66. catch (Sales::bad_index &bad)
    67. {
    68. cout << bad.what();
    69. LabeledSales::nbad_index *ni;
    70. if (ni = dynamic_cast(&bad)) //RTTI识别类型;
    71. {
    72. cout << "Company: " << ni->label_val() << endl;
    73. cout << "bad index: " << ni->bi_val() << endl;
    74. }
    75. else
    76. {
    77. cout << "bad index: " << bad.bi_val() << endl;
    78. }
    79. }
    80. cout << "done\n";
    81. return 0;
    82. }

  • 相关阅读:
    Android studio 使用opencl库(realme 手机)
    数仓相关,总结
    Java 日期格式(yyyy-MM-dd 与YYYY-MM-dd 区别
    Mybatis第三天
    NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
    串的概念及操作
    pytorch-损失函数-分类和回归区别
    (02)Cartographer源码无死角解析-(22) 传感器数据分发→总体分析
    Docker容器搭建Hadoop集群(hadoop-3.1.3)
    【Gateway】基于ruoyi-cloud-plus项目,gateway局部过滤器和过滤返回以及集成nacos
  • 原文地址:https://blog.csdn.net/sakura0908/article/details/132722863