56 C++ Primer Plus(第六版)第十五章 编程练习答案
-
- 1
-
- tv2.h
- exercise01_tv2.cpp
- exercise01_use_tv2.cpp
- 2
-
- exc_mean2.h
- exercise02.cpp
- 3
-
- exc_mean3.h
- exercise03.cpp
- 4
-
- sales.h
- exercise04_sales.cpp
- exercise04_use_sales.cpp
1
tv2.h
// tv2.h -- Tv and Remote classes
#ifndef TV_H_
#define TV_H_
class Remote;
class Tv
{
public:
friend class Remote; // Remote can access Tv private parts
enum {Off, On};
enum {MinVal,MaxVal = 20};
enum {Antenna, Cable};
enum {TV, DVD};
Tv(int s = Off, int mc = 125) : state(s), volume(5),
maxchannel(mc), channel(2), mode(Cable), input(TV) {}
void onoff() {state = (state == On)? Off : On;}
bool ison() const {return state == On;}
bool volup();
bool voldown();
void chanup();
void chandown();
void set_mode() {mode = (mode == Antenna)? Cable : Antenna;}
void set_input() {input = (input == TV)? DVD : TV;}
void settings() const; // display all settings
void set_smode(Remote& r);
private:
int state; // on or off
int volume; // 音量
int maxchannel; // maximum number of channels
int channel; // current channel setting
int mode; // 广播或有线电视
int input; // TV or DVD
};
class Remote
{
private:
int mode; // controls TV or DVD
int state_mode;
enum{ Regular, Interact };
public:
friend class Tv;
Remote(int m = Tv::TV) : mode(m) {}
bool volup(Tv & t) { return t.volup();}
bool voldown(Tv & t) { return t.voldown();}
void onoff(Tv & t) { t.onoff(); }
void chanup(Tv & t) {t.chanup();}
void chandown(Tv & t) {t.chandown();}
void set_chan(Tv & t, int c) {t.channel = c;}
void set_mode(Tv & t) {t.set_mode();}
void set_input(Tv & t) {t.set_input();}
void show_smode() const
{
std::cout << "Now the state mode is:"
<< (state_mode == Regular ? "Regular" : "Interact") << std::endl;
}
void set_smode() { state_mode =
(state_mode == Regular ? Interact : Regular); }
};
inline void Tv::set_smode(Remote& r)
{
if (state == On)
{
r.set_smode();
//r.show_smode();
}
else
{
std::cout << "The Tv is off!\n";
}
}
#endif
exercise01_tv2.cpp
// tv.cpp -- methods for the Tv class (Remote methods are inline)
#include <iostream>
#include "tv2.h"
bool Tv::volup()
{
if (volume < MaxVal)
{
volume++;
return true;
}
else
return false;
}
bool Tv::voldown()
{
if (volume > MinVal)
{
volume--;
return true;
}
else
return false;
}
void Tv::chanup()
{
if (channel < maxchannel)
channel++;
else
channel = 1;
}
void Tv::chandown()
{
if (channel > 1)
channel--;
else
channel = maxchannel;
}
void Tv::settings() const
{
using std::cout;
using std::endl;
cout << "TV is " << (state == Off? "Off" : "On") << endl;
if (state == On)
{
cout << "Volume setting = " << volume << endl;
cout << "Channel setting = " << channel << endl;
cout << "Mode = "
<< (mode == Antenna? "antenna" : "cable") << endl;
cout << "Input = "
<< (input == TV? "TV" : "DVD") << endl;
}
}
exercise01_use_tv2.cpp
//use_tv.cpp -- using the Tv and Remote classes
#include <iostream>
#include "tv2.h"
int main()
{
using std::cout;
Tv s42;
cout << "Initial settings for 42\" TV:\n";
s42.settings();
s42.onoff();
s42.chanup();
cout << "\nAdjusted settings for 42\" TV:\n";
s42.settings();
Remote grey;
grey.set_chan(s42, 10);
cout << "--------------------\n";
grey.show_smode();
s42.set_smode(grey);
grey.show_smode();
cout << "--------------------\n";
grey.volup(s42);
grey.volup(s42);
cout << "\n42\" settings after using remote:\n";
s42.settings();
Tv s58(Tv::On);
s58.set_mode();
grey.set_chan(s58,28);
cout << "\n58\" settings:\n";
s58.settings();
// std::cin.get();
return 0;
}
2
exc_mean2.h
// exc_mean2.h -- exception classes for hmean(), gmean()
#pragma once
#include <iostream>
#include<stdexcept>
class bad_hmean : public std::logic_error
{
private:
double v1;
double v2;
public:
bad_hmean(const std::string& s = "Error in bad_hmean()!",double a = 0,
double b = 0) :logic_error(s), v1(a), v2(b){ }
void mesg();
const char* what() { return "Error in bad_hmean()!\n"; }
virtual ~bad_hmean() throw() { }
};
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(const std::string& s = "Error in bad_gmean()!", double a = 0,
double b = 0) :logic_error(s), v1(a), v2(b){ }
const char * mesg();
const char* what() { return "Error in bad_gmean()!\n"; }
virtual ~bad_gmean() throw() { }
};
inline const char * bad_gmean::mesg()
{
return "gmean() arguments should be >= 0\n";
}
exercise02.cpp
//error4.cpp using exception classes
#include <iostream>
#include <cmath> // or math.h, unix users may need -lm flag
#include "exc_mean2.h"
//#include<stdexcept>
// 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
{
cout << bg.what();
bg.mesg();
cout << "Try again.\n";
continue;
}
catch (bad_gmean & hg)
{
cout << hg.what();
cout << hg.mesg();
cout << "Values used: " << hg.v1 << ", "
<< hg.v2 << endl;
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("Error in 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("Error in bad_gmean()!", a,b);
return std::sqrt(a * b);
}
3
exc_mean3.h
// exc_mean3.h -- exception classes for hmean(), gmean()
#pragma once
#include <iostream>
#include<stdexcept>
class Logic_error:public std::logic_error
{
private:
double v1;
double v2;
std::string s;
public:
Logic_error(const std::string& s = "Error!",double d1 = 0,double d2 = 0)
:logic_error(s),v1(d1),v2(d2) { }
int return_v1() { return v1; }
int return_v2() { return v2; }
virtual void mesg() = 0;
virtual ~Logic_error() throw() { }
};
class bad_hmean : public Logic_error
{
public:
bad_hmean(const std::string& s = "Error in bad_hmean()!",double a = 0,
double b = 0) :Logic_error(s, a, b){ }
void mesg();
const char* what() { return "Error in bad_hmean()!\n"; }
virtual ~bad_hmean() throw() { }
};
inline void bad_hmean::mesg()
{
std::cout << "hmean(" << return_v1() << ", " << return_v2() <<"): "
<< "invalid arguments: a = -b\n";
}
class bad_gmean : public Logic_error
{
public:
bad_gmean(const std::string& s = "Error in bad_gmean()!", double a = 0,
double b = 0) :Logic_error(s, a, b){ }
void mesg();
const char* what() { return "Error in bad_gmean()!\n"; }
virtual ~bad_gmean() throw() { }
};
inline void bad_gmean::mesg()
{
std::cout<< "gmean() arguments should be >= 0\n";
}
exercise03.cpp
//error4.cpp using exception classes
#include <iostream>
#include <cmath> // or math.h, unix users may need -lm flag
#include "exc_mean3.h"
//#include<stdexcept>
// 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
{
cout << bg.what();
bg.mesg();
cout << "Try again.\n";
continue;
}
catch (bad_gmean & hg)
{
cout << hg.what();
hg.mesg();
cout << "Values used: " << hg.return_v1() << ", "
<< hg.return_v2() << endl;
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("Error in 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("Error in bad_gmean()!", a,b);
return std::sqrt(a * b);
}
4
sales.h
// sales.h -- exceptions and inheritance
#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;
};
exercise04_sales.cpp
// sales.cpp -- Sales implementation
#include "sales.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);
}
exercise04_use_sales.cpp
// use_sales.cpp -- nested exceptions
#include <iostream>
#include "sales.h"
int main()
{
using namespace std;
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 );
Sales::bad_index* sb;
LabeledSales::nbad_index* ln;
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(logic_error& bad)
{
cout << bad.what();
if (ln = dynamic_cast<LabeledSales::nbad_index *>(&bad))
{
cout << "Company: " << ln->label_val() << endl;
cout << "bad index: " << ln->bi_val() << endl;
}
else if (sb = dynamic_cast<Sales::bad_index *>(&bad))
{
cout << "bad index: " << sb->bi_val() << endl;
}
}
cout << "\nNext try block:\n";
try
{
sales2[2] = 37.5;
sales1[20] = 23345;
cout << "End of try block 2.\n";
}
catch(logic_error& bad)
{
cout << bad.what();
if (ln = dynamic_cast<LabeledSales::nbad_index *>(&bad))
{
cout << "Company: " << ln->label_val() << endl;
cout << "bad index: " << ln->bi_val() << endl;
}
else if (sb = dynamic_cast<Sales::bad_index*>(&bad))
{
cout << "bad index: " << sb->bi_val() << endl;
}
}
cout << "done\n";
// std::cin.get();
return 0;
}