C++PrimerPlus学习——第十五章编程练习

15-1
tv.h

#ifndef TV_H_
#define TV_H_
#include <iostream>>

class Remote;

class Tv
{
public:
	friend class Remote;
	enum { Off, On };
	enum { Minval, Maxval };
	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;
	void set_remote_mode(Remote& r);
private:
	int state;
	int volume;
	int maxchannel;
	int channel;
	int mode;
	int input;
};

class Remote
{
private:
	int mode;
public:
	friend class Tv;
	enum { Normal, Interact };
	Remote(int m = Normal) :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 showmode() const;
};
#endif // !TV_H_

tv.cpp

#include <iostream>
#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
{
	using std::cout;
	using std::endl;
	cout << "TV is " << (state == Off ? "Off" : "On") << endl;
	if (state == On)
	{
		cout << "Voluume setting = " << volume << endl;
		cout << "Channel setting = " << channel << endl;
		cout << "Mode = " << (mode == Antenna ? "Antenna" : "Cable") << endl;
		cout << "Input = " << (input == TV ? "TV" : "DVD") << endl;
	}
}

void Tv::set_remote_mode(Remote& r)
{
	if (state == On)
		r.mode = (r.mode == Remote::Normal) ? Remote::Interact : Remote::Normal;
}

void Remote::showmode() const
{
	using std::cout;
	using std::endl;
	cout << "Remote mode is: " << (mode == Normal ? "Normal" : "Interact") << endl;
}

15-2
这个抄的网上的,感觉很简洁
exc_mean.h

#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
class bad_hmean : public logic_error
{
public:
    explicit bad_hmean(const string& s) :logic_error(s) {}
};
class bad_gmean : public logic_error
{
public:
    explicit bad_gmean(const string& s) :logic_error(s) {}
};

#endif // !EXC_MEAN_H_

main.cpp

#include <iostream>
#include <cmath>
#include "exc_meanc.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 {
			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>: ";
		}
		catch (bad_hmean& bg)
		{
			bg.what();
			cout << "Try again.\n";
			continue;
		}
		catch (bad_gmean& hg)
		{
			cout << hg.what();
			cout << "Sorry, you don't get to play any more.\n";
			break;
		}
	}
	cout << "Bye!\n";
	return 0;
}

double hmean(double a, double b)
{
	if (a == -b)
		throw bad_hmean("hmean() error: invalid arguments: a = -b\n");
	return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
	if (a < 0 || b < 0)
		throw bad_gmean("gmean() arguments should be >= 0\n");
	return std::sqrt(a * b);
}

15-3
exc_meanc.h

#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include <iostream>
#include <stdexcept>
#include <string>

class bad_hmean :public std::logic_error
{
private:
	std::string name;
public:
	double v1;
	double v2;
	explicit bad_hmean(double a = 0, double b = 0, const std::string& s = "Error in hmean()\n");
	void mesg();
	virtual ~bad_hmean() throw() {}
};

inline void bad_hmean::mesg()
{
	std::cout << name << "(" << v1 << ", " << v2 << ") arguments a=-b should be div a+b=0!\n";
}
class bad_gmean :public std::logic_error
{
private:
	std::string name;
public:
	double v1;
	double v2;
	explicit bad_gmean(double a = 0, double b = 0, const std::string& s = "Error in gmean()\n");
	void mesg();
	virtual ~bad_gmean() throw() {}
};

inline void bad_gmean::mesg()
{
	std::cout << name << "(" << v1 << ", " << v2 << ") arguments a * b should above 0!\n";
}
#endif // !EXC_MEAN_H_

exc_mean.cpp

#include "exc_meanc.h"
#include <string>
bad_hmean::bad_hmean(double a, double b, const std::string& s):v1(a), v2(b), logic_error(s)
{
	name = "hmean";
}

bad_gmean::bad_gmean(double a, double b, const std::string& s) : v1(a), v2(b), logic_error(s)
{
	name = "gmean";
}

main.cpp

#include <iostream>
#include <cmath>
#include "exc_meanc.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 {
			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>: ";
		}
		catch (bad_hmean& bg)
		{
			cout << bg.what();
			cout << "Error:";
			bg.mesg();
			cout << "\nTry again.\n";
			continue;
		}
		catch (bad_gmean& hg)
		{
			cout << hg.what();
			cout << "Error:";
			hg.mesg();
			cout << "\nSorry, you don't get to play any more.\n";
			break;
		}
	}
	cout << "Bye!\n";
	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);
}

15-4
头文件和源文件和书上一样,不过这书翻译太渣了,题意经常模糊不清,难受,就不能优化一下语言么
main.cpp

#include "sales.h"
#include <iostream>

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);
	Sales::bad_index* s;
	LabeledSales::nbad_index* l;

	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 (std::logic_error& bad)
	{
		cout << bad.what();
		if (l = dynamic_cast<LabeledSales::nbad_index*>(&bad))
		{
			cout << "Company: " <<l->label_val() << endl;
			cout << "bad index: " << l->bi_val() << endl;
		}
		else if (s = dynamic_cast<Sales::bad_index*>(&bad))
			cout << "bad index: " << s->bi_val() << endl;
	}
	catch (Sales::bad_index& bad)
	{
		cout << bad.what();
		cout << "bad index: " << bad.bi_val() << endl;
	}
	cout << "\nNext try block 2.\n";
	try
	{
		sales2[2] = 37.5;
		sales1[20] = 23345;
		cout << "End of try blcok 2.\n";
	}
	catch (std::logic_error& bad)
	{
		cout << bad.what();
		if (l = dynamic_cast<LabeledSales::nbad_index*>(&bad))
		{
			cout << "Company: " << l->label_val() << endl;
			cout << "bad index: " << l->bi_val() << endl;
		}
		else if (s = dynamic_cast<Sales::bad_index*>(&bad))
			cout << "bad index: " << s->bi_val() << endl;
	}
	cout << "done\n";
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值