C++ Primer Plus第六版 第十五章 编程练习答案

//第一题
//main.cpp
#include "TV.h"

int main()
{
	Tv s42;
	std::cout << "Initial settings for 42\" TV:\n";
	s42.settings();
	s42.onoff();
	s42.chanup();
	std::cout << "\nAdjusted settings for 42\" Tv:\n";
	s42.settings();

	Remote grey;

	grey.set_chan(s42, 10);
	grey.volup(s42);
	grey.volup(s42);
	std::cout << "\n42\" settings after using remote:\n";
	s42.settings();

	grey.ShowAMode();
	s42.ChangeAMode(grey);
	grey.ShowAMode();

	return 0;
}

//TV.h
#ifndef TV_H_
#define TV_H_

#include 

class Tv
{
private:
	int state;
	int volume;
	int maxchannel;
	int channel;
	int mode;
	int input;
public:
	friend class Remote;
	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 ^= 1; }
	bool ison() const { return state == ON; }
	bool volup();
	bool voldown();
	void chanup();
	void chandown();
	void set_mode() { mode ^= 1; }
	void set_input() { input ^= 1; }
	void settings() const;
	void ChangeAMode(Remote &r);
};

class Remote
{
private:
	int mode;
	int AMode;
public:
	friend class Tv;
	Remote(int m = Tv::TV, int n = NORMAL) : mode(m), AMode(n) {}
	enum { NORMAL, INTERACTIVE };
	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 ShowAMode() const { std::cout << "现在是: " << (AMode == NORMAL ? "常规模式" : "交互模式") << std::endl; }
};

#endif

//TV.cpp
#include "TV.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
{
	std::cout << "TV is " << (state == OFF ? "OFF" : "ON") << std::endl;
	if (state == ON)
	{
		std::cout << "Volume setting = " << volume << std::endl;
		std::cout << "Channel setting = " << channel << std::endl;
		std::cout << "Mode = " << (mode == ANTENNA ?  "antenna" : "cable") << std::endl;
		std::cout << "Input = " << (input == TV ? "TV" : "DVD") << std::endl;
	}
}

void Tv::ChangeAMode(Remote & r)
{
	if (state == ON)
		r.AMode ^= 1;
}




//第二题
//main.cpp
#include "exc_mean.h"

double hmean(double a, double b);
double gmean(double a, double b);

int main()
{
	double x, y, z;

	std::cout << "Enter two numbers: ";
	while (std::cin >> x >> y)
	{
		try
		{
			z = hmean(x, y);
			std::cout << "Harmonic mean of " << x << " and " << y << " is " << z << std::endl;
			std::cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << std::endl;
			std::cout << "Enter next set of numbers : ";
		}
		catch (bad_hmean &bg)
		{
			std::cout << bg.what() << std::endl;
			std::cout << "Try again." << std::endl;
			continue;
		}
		catch (bad_gmean &hg)
		{
			std::cout << hg.what() << std::endl;
			std::cout << "Sorry, you don't get to play any more." << std::endl;
			break;
		}
	}

	std::cout << "Bye!" << std::endl;

	return 0;
}

double hmean(double a, double b)
{
	if (a == -b)
		throw bad_hmean();
	return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
	if (a < 0 || b < 0)
		throw bad_gmean();
	return std::sqrt(a * b);
}

//exc_mean.h
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_

#include 
#include 
#include 

class bad_hmean : public std::logic_error
{
public:
	bad_hmean(const std::string &s = "hmean(), invalid argument") : logic_error(s) {}
};

class bad_gmean : public std::logic_error
{
public:
	bad_gmean(const std::string &s = "gmean(), auguments shouble be >= 0") : logic_error(s) {}
};

#endif



//第三题
//main.cpp
#include "exc_mean.h"

double hmean(double a, double b);
double gmean(double a, double b);

int main()
{
	double x, y, z;

	std::cout << "Enter two numbers: ";
	while (std::cin >> x >> y)
	{
		try
		{
			z = hmean(x, y);
			std::cout << "Harmonic mean of " << x << " and " << y << " is " << z << std::endl;
			std::cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << std::endl;
			std::cout << "Enter next set of numbers : ";
		}
		catch (bad_hmean &bg)
		{
			bg.what();
			std::cout << "Sorry, you don't get to play any more." << std::endl;
			break;
		}
		catch (bad_gmean &hg)
		{
			hg.what();
			std::cout << "Sorry, you don't get to play any more." << std::endl;
			break;
		}
	}

	std::cout << "Bye!" << std::endl;

	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);
}

//exc_mean.h
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_

#include 
#include 
#include 

class bad_hmean : public std::logic_error
{
private:
	double a, b;
public:
	bad_hmean(double x = 0, double y =  0) : std::logic_error("hmean") { a = x; b = y; }
	virtual void what() { std::cout << "hmean(), invalid argument. a = " << a << ", b = " << b << std::endl; }
};

class bad_gmean : public std::logic_error
{
private:
	double a, b;
public:
	bad_gmean(double x = 0, double y = 0) : logic_error("gmean") { a = x; b = y; }
	virtual void what() { std::cout << "gmean(), auguments shouble be >= 0. a = " << a << ", b = " << b << std::endl; }
};

#endif



//第四题
//use_sales.cpp
#include "sales.h"
#include 

int main()
{
	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);

	std::cout << "First try block:\n";
	try
	{
		int i;
		std::cout << "Year = " << sales1.Year() << std::endl;
		for (i = 0; i < 12; ++i)
		{
			std::cout << sales1[i] << ' ';
			if (i % 6 == 5)
				std::cout << std::endl;
		}
		std::cout << "Year = " << sales2.Year() << std::endl;
		std::cout << "Label = " << sales2.Label() << std::endl;
		for (i = 0; i <= 12; ++i)
		{
			std::cout << sales2[i] << ' ';
			if (i % 6 == 5)
				std::cout << std::endl;
		}
		std::cout << "End of try block 1.\n";
	}
	catch (std::logic_error &le) 
	{
		std::cout << le.what();
		if (LabeledSales::nbad_index *ni = dynamic_cast(&le))
		{
			std::cout << "Company: " << ni->label_val() << std::endl;
			std::cout << "bad index: " << ni->bi_val() << std::endl;
		}
		else if (Sales::bad_index *bi = dynamic_cast(&le))
			std::cout << "bad index: " << bi->bi_val() << std::endl;
	}

	std::cout << "\nNext try block:\n";
	try
	{
		sales2[2] = 37.5;
		sales1[20] = 23345;
		std::cout << "End of try block 2.\n";
	}
	catch (std::logic_error &le)
	{
		std::cout << le.what();
		if (LabeledSales::nbad_index *ni = dynamic_cast(&le))
		{
			std::cout << "Company: " << ni->label_val() << std::endl;
			std::cout << "bad index: " << ni->bi_val() << std::endl;
		}
		else if (Sales::bad_index *bi = dynamic_cast(&le))
			std::cout << "bad index: " << bi->bi_val() << std::endl;
	}

	return 0;
}

//sales.h
#ifndef SALES_H_
#define SALES_H_

#include 
#include 

class Sales
{
public:
	enum { MONTHS = 12 };
	class bad_index : public std::logic_error
	{
	private:
		int bi;
	public:
		explicit bad_index(int ix, const std::string &s = "Index error in Sales objext\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
{
private:
	std::string label;
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 objext\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);
};

#endif

//sales.cpp
#include "sales.h"

Sales::bad_index::bad_index(int ix, const std::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 < 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 std::string &lb, int ix, const std::string &s) : Sales::bad_index(ix, s)
{
	lbl = lb;
}

LabeledSales::LabeledSales(const std::string &lb, int yy) : Sales(yy)
{
	label = lb;
}

LabeledSales::LabeledSales(const std::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);
}




你可能感兴趣的:(C++,Primer,Plus学习笔记)