习题1 (15.8.1) 细节上“状态成员”没有放在remote类里,切换状态成员的我放在remote里了,原理一样。
//标头.h
#pragma once
#ifndef INTERTV_H_
#define INTERTV_H_
#include <iostream>
using namespace std;
class Tv;
enum Tv_mode { Regular, Interact };
class Remote
{
friend class Tv;
private:
public:
void Show_mode(Tv & t);
void SET_MODE(Tv & t, Tv_mode form);
void Change_MODE(Tv & t);
};
class Tv
{
friend class Remote;
private:
Tv_mode Mode;
public:
Tv(Tv_mode M = Regular) {};
};
void Remote::SET_MODE(Tv & t, Tv_mode form )
{
t.Mode = form;
}
void Remote::Show_mode(Tv & t)
{
if (t.Mode == Regular) cout << "Now is Regular mode.\n";
else cout << "Now is interactive mode.\n";
}
void Remote::Change_MODE(Tv& t)
{
t.Mode = (t.Mode == Regular) ? Interact : Regular;
}
#endif
//15.8.1.cpp
#include "stdafx.h"
#include "标头.h"
int main()
{
Tv Tv1;
Remote Remote1;
Remote1.Show_mode(Tv1);
Remote1.Change_MODE(Tv1);
Remote1.Show_mode(Tv1);
Remote1.SET_MODE(Tv1, Interact);
Remote1.Show_mode(Tv1);
return 0;
}
- 习题2 15.8.2
- //标头.h
- #pragma once
- #include <iostream>
- #include <stdexcept>
- #include <exception>
- #include <string>
- class bad_hmean :public std::logic_error
- {
- private:
- double v1;
- double v2;
- public:
- bad_hmean(double a = 0, double b = 0) : v1(a), v2(b),logic_error("function name: bad_hmean. Logic Error.") {}
- void mesg();
- const char * what()
- {
- return "function name: bad_hmean. Logic Error.";
- }
- };
- inline void bad_hmean::mesg()
- {
- std::cout << "hmean(" << v1 << ", " << v2 << "): "
- << "invalid arguments: a = -b\n";
- }
- class bad_gmean :public std::logic_error
- {
- public:
- double v1;
- double v2;
- bad_gmean(double a = 0, double b = 0) : v1(a), v2(b), logic_error("function name: bad_hmean. Logic Error.") {}
- const char * mesg();
- const char * what()
- {
- return "function name: bad_gmean. Logic Error.";
- }
- };
- inline const char * bad_gmean::mesg()
- {
- return "gmean() arguments should be >= 0\n";
- }
- // 15.8.2.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath> // or math.h, unix users may need -lm flag
- #include "标头.h"
- // function prototypes
- double hmean(double a, double b);
- double gmean(double a, double b);
- int main()
- {
- using std::cout;
- using std::cin;
- using std::endl;
- double x, y, z;
- cout << "Enter two numbers: ";
- while (cin >> x >> y)
- {
- try { // start of try block
- z = hmean(x, y);
- cout << "Harmonic mean of " << x << " and " << y
- << " is " << z << endl;
- cout << "Geometric mean of " << x << " and " << y
- << " is " << gmean(x, y) << endl;
- cout << "Enter next set of numbers <q to quit>: ";
- }// end of try block
- catch (bad_hmean & bg) // start of catch block
- {
- bg.mesg();
- cout << "Try again.\n";
- cout << bg.what();
- continue;
- }
- catch (bad_gmean & hg)
- {
- cout << hg.mesg();
- cout << "Values used: " << hg.v1 << ", "
- << hg.v2 << endl;
- cout << hg.what();
- cout << "Sorry, you don't get to play any more.\n";
- break;
- } // end of catch block
- }
- cout << "Bye!\n";
- // cin.get();
- // cin.get();
- return 0;
- }
- double hmean(double a, double b)
- {
- if (a == -b)
- throw bad_hmean(a, b);
- return 2.0 * a * b / (a + b);
- }
- double gmean(double a, double b)
- {
- if (a < 0 || b < 0)
- throw bad_gmean(a, b);
- return std::sqrt(a * b);
- }
- 习题3. 15.8.3
- #pragma once
- //标头.h
- #ifndef STDEXCEPT_H_
- #define STDEXCEPT_H_
- #include "stdafx.h"
- #include <stdexcept>
- #include <iostream>
- using namespace std;
- class bad_hmean :public std::logic_error
- {
- private:
- double v1;
- double v2;
- public:
- bad_hmean(double a = 0, double b = 0) : v1(a), v2(b), logic_error("function name: bad_hmean. Logic Error.") {}
- void mesg();
- void just_report();
- void * what()
- {
- cout << "function name: bad_hmean.";
- cout << "Values: (" << v1 << "," << v2 << ")\n";
- }
- };
- void bad_hmean::just_report()
- {
- std::cout << "hmean(" << v1 << ", " << v2 << "): ";
- }
- inline void bad_hmean::mesg()
- {
- std::cout << "hmean(" << v1 << ", " << v2 << "): "
- << "invalid arguments: a = -b\n";
- }
- class bad_gmean :public std::logic_error
- {
- public:
- double v1;
- double v2;
- bad_gmean(double a = 0, double b = 0) : v1(a), v2(b), logic_error("function name: bad_hmean. Logic Error.") {}
- void just_report();
- const char * mesg();
- void * what()
- {
- cout << "function name: bad_hmean.";
- cout << "Values: (" << v1 << "," << v2 << ")\n";
- }
- };
- void bad_gmean::just_report()
- {
- std::cout << "gmean(" << v1 << ", " << v2 << "): ";
- }
- inline const char * bad_gmean::mesg()
- {
- return "gmean() arguments should be >= 0\n";
- }
- #endif
- // 15.8.3.cpp
- #include "stdafx.h"
- #include "标头.h"
- double hmean(double a, double b);
- double gmean(double a, double b);
- int main()
- {
- using std::cout;
- using std::cin;
- using std::endl;
- double x, y, z;
- cout << "Enter two numbers: ";
- while (cin >> x >> y)
- {
- try { // start of try block
- z = hmean(x, y);
- cout << "Harmonic mean of " << x << " and " << y
- << " is " << z << endl;
- cout << "Geometric mean of " << x << " and " << y
- << " is " << gmean(x, y) << endl;
- cout << "Enter next set of numbers <q to quit>: ";
- }// end of try block
- catch (std::logic_error & bg) // start of catch block
- {
- bg.what();
- cout << "End of the loop.";
- break;
- }
- }
- cout << "Bye!\n";
- // cin.get();
- // cin.get();
- return 0;
- }
- double hmean(double a, double b)
- {
- if (a == -b)
- throw bad_hmean(a, b);
- return 2.0 * a * b / (a + b);
- }
- double gmean(double a, double b)
- {
- if (a < 0 || b < 0)
- throw bad_gmean(a, b);
- return std::sqrt(a * b);
- }
习题4. 15.8.4
- //标头.h
- #pragma once
- #include <stdexcept>
- #include <string>
- class Sales
- {
- public:
- enum { MONTHS = 12 }; // could be a static const
- class bad_index : public std::logic_error
- {
- private:
- int bi; // bad index value
- public:
- explicit bad_index(int ix,
- const std::string & s = "Index error in Sales object\n");
- int bi_val() const { return bi; }
- virtual ~bad_index() throw() {}
- };
- explicit Sales(int yy = 0);
- Sales(int yy, const double * gr, int n);
- virtual ~Sales() { }
- int Year() const { return year; }
- virtual double operator[](int i) const;
- virtual double & operator[](int i);
- private:
- double gross[MONTHS];
- int year;
- };
- class LabeledSales : public Sales
- {
- public:
- class nbad_index : public Sales::bad_index
- {
- private:
- std::string lbl;
- public:
- nbad_index(const std::string & lb, int ix,
- const std::string & s = "Index error in LabeledSales object\n");
- const std::string & label_val() const { return lbl; }
- virtual ~nbad_index() throw() {}
- };
- explicit LabeledSales(const std::string & lb = "none", int yy = 0);
- LabeledSales(const std::string & lb, int yy, const double * gr, int n);
- virtual ~LabeledSales() { }
- const std::string & Label() const { return label; }
- virtual double operator[](int i) const;
- virtual double & operator[](int i);
- private:
- std::string label;
- };
- // 源.cpp -- Sales implementation
- #include "stdafx.h"
- #include "标头.h"
- using std::string;
- Sales::bad_index::bad_index(int ix, const string & s)
- : std::logic_error(s), bi(ix)
- {
- }
- Sales::Sales(int yy)
- {
- year = yy;
- for (int i = 0; i < MONTHS; ++i)
- gross[i] = 0;
- }
- Sales::Sales(int yy, const double * gr, int n)
- {
- year = yy;
- int lim = (n < MONTHS) ? n : MONTHS;
- int i;
- for (i = 0; i < lim; ++i)
- gross[i] = gr[i];
- // for i > n and i < MONTHS
- for (; i < MONTHS; ++i)
- gross[i] = 0;
- }
- double Sales::operator[](int i) const
- {
- if (i < 0 || i >= MONTHS)
- throw bad_index(i);
- return gross[i];
- }
- double & Sales::operator[](int i)
- {
- if (i < 0 || i >= MONTHS)
- throw bad_index(i);
- return gross[i];
- }
- LabeledSales::nbad_index::nbad_index(const string & lb, int ix,
- const string & s) : Sales::bad_index(ix, s)
- {
- lbl = lb;
- }
- LabeledSales::LabeledSales(const string & lb, int yy)
- : Sales(yy)
- {
- label = lb;
- }
- LabeledSales::LabeledSales(const string & lb, int yy, const double * gr, int n)
- : Sales(yy, gr, n)
- {
- label = lb;
- }
- double LabeledSales::operator[](int i) const
- {
- if (i < 0 || i >= MONTHS)
- throw nbad_index(Label(), i);
- return Sales::operator[](i);
- }
- double & LabeledSales::operator[](int i)
- {
- if (i < 0 || i >= MONTHS)
- throw nbad_index(Label(), i);
- return Sales::operator[](i);
- }
- // 15.8.4.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- // use_sales.cpp -- nested exceptions
- #include <iostream>
- #include "标头.h"
- int main()
- {
- using std::cout;
- using std::cin;
- using std::endl;
- double vals1[12] =
- {
- 1220, 1100, 1122, 2212, 1232, 2334,
- 2884, 2393, 3302, 2922, 3002, 3544
- };
- double vals2[12] =
- {
- 12, 11, 22, 21, 32, 34,
- 28, 29, 33, 29, 32, 35
- };
- Sales sales1(2011, vals1, 12);
- LabeledSales sales2("Blogstar", 2012, vals2, 12);
- cout << "First try block:\n";
- try
- {
- int i;
- cout << "Year = " << sales1.Year() << endl;
- for (i = 0; i < 12; ++i)
- {
- cout << sales1[i] << ' ';
- if (i % 6 == 5)
- cout << endl;
- }
- cout << "Year = " << sales2.Year() << endl;
- cout << "Label = " << sales2.Label() << endl;
- for (i = 0; i <= 12; ++i)
- {
- cout << sales2[i] << ' ';
- if (i % 6 == 5)
- cout << endl;
- }
- cout << "End of try block 1.\n";
- }
- catch (Sales::bad_index & bad)
- {
- Sales::bad_index * pt = &bad;
- if (LabeledSales::nbad_index * ptlb = dynamic_cast<LabeledSales::nbad_index *> (pt))
- cout << "Company: " << ptlb->label_val() << endl;
- cout << bad.what();
- cout << "bad index: " << bad.bi_val() << endl;
- }
- cout << "\nNext try block:\n";
- try
- {
- sales2[2] = 37.5;
- sales1[20] = 23345;
- cout << "End of try block 2.\n";
- }
- catch (LabeledSales::nbad_index & bad)
- {
- cout << bad.what();
- cout << "Company: " << bad.label_val() << endl;
- cout << "bad index: " << bad.bi_val() << endl;
- }
- catch (Sales::bad_index & bad)
- {
- cout << bad.what();
- cout << "bad index: " << bad.bi_val() << endl;
- }
- cout << "done\n";
- // std::cin.get();
- return 0;
- }