C++ Primer Plus第六版第十一章使用类编程练习答案

1.

main.cpp

#include "Vector.h"
#include
#include
#include
int main()
{
	using namespace std;
	ofstream outFile;
	outFile.open("hello.txt");
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	cout << "Enter target distance (q to quit): ";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		outFile << "Target Distance: " << target
			<< ", Step Size: " << dstep << endl;
		outFile << "0: (x,y) = (0, 0)" << endl;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			outFile << steps + 1 << ": " << result << endl;
			steps++;
		}
		outFile << "After " << steps << " steps, the subject "
			"has the following location:\n";
		outFile << result << endl;
		result.polar_mode();
		outFile << " or\n" << result << endl;
		outFile << "Average outward distance per step = "
			<< result.magval() / steps << endl;
		steps = 0;
		result.reset(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
	}
	cout << "Bye!\n";
	outFile.close();
	cin.clear();
	while (cin.get() != '\n')
		continue;
	return 0;
}

vector.h
为了方便,类的声明和定义都在头文件里面了

#ifndef VECTOR_H_
#define VECTOR_H_
#include 
using std::cout;
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode { RECT, POL };
	private:
		double x;
		double y;
		double mag;
		double ang;
		Mode mode;
		void set_mag();
		void set_ang();
		void set_x();
		void set_y();
	public:
		Vector();
		Vector(double n1, double n2, Mode form = RECT);
		void reset(double n1, double n2, Mode form = RECT);
		~Vector();
		double xval() const { return x; }
		double yval() const { return y; }
		double magval() const { return mag; }
		double angval() const { return ang; }
		void polar_mode();
		void rect_mode();
		Vector operator+(const Vector &b) const;
		Vector operator-(const Vector &b) const;
		Vector operator-() const;
		Vector operator*(double n) const;
		friend Vector operator*(double n, const Vector & a);
		friend std::ostream &
			operator<< (std::ostream & os, const Vector & v);
	};
	const double Rad_to_deg = 45.0 / atan(1.0);
	void Vector::set_mag()
	{
		mag = sqrt(x * x + y * y);
	}
	void Vector::set_ang()
	{
		if (x == 0.0 && y == 0.0)
			ang = 0.0;
		else
			ang = atan2(y, x);
	}
	void Vector::set_x()
	{
		x = mag * cos(ang);
	}
	void Vector::set_y()
	{
		y = mag * sin(ang);
	}
	Vector::Vector()
	{
		x = y = mag = ang = 0.0;
		mode = RECT;
	}
	Vector::Vector(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		}
		else if (form == POL)
		{
			mag = n1;
			ang = n2 / Rad_to_deg;
			set_x();
			set_y();
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = mag = ang = 0.0;
			mode = RECT;
		}
	}
	void Vector::reset(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		}
		else if (form == POL)
		{
			mag = n1;
			ang = n2 / Rad_to_deg;
			set_x();
			set_y();
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = mag = ang = 0.0;
			mode = RECT;
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	Vector Vector::operator+(const Vector &b) const
	{
		return Vector(x + b.x, y + b.y);
	}
	Vector Vector::operator-(const Vector &b) const
	{
		return Vector(x - b.x, y - b.y);
	}
	Vector Vector::operator-() const
	{
		return Vector(-x, -y);
	}
	Vector Vector::operator*(double n) const
	{
		return Vector(n * x, n * y);
	}
	Vector operator*(double n, const Vector & a)
	{
		return a * n;
	}
	std::ostream & operator<<(std::ostream & os, const Vector & v)
	{
		if (v.mode == Vector::RECT)
			os << "(x,y) = (" << v.x << ", " << v.y << ")";
		else if (v.mode == Vector::POL)
		{
			os << "(m,a) = (" << v.mag << ", "
				<< v.ang * Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
}
#endif // VECTOR_H_H

2.

测试代码和上面的一样.

#ifndef VECTOR_H_
#define VECTOR_H_
#include 
#include
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode { RECT, POL };
	private:
		double x;
		double y;
		Mode mode;
		void set_x(double n1, double n2);
		void set_y(double n1, double n2);
	public:
		Vector();
		Vector(double n1, double n2, Mode form = RECT);
		void reset(double n1, double n2, Mode form = RECT);
		~Vector();
		double xval() const { return x; }
		double yval() const { return y; }
		double magval() const { return sqrt(x * x + y * y); }
		double angval() const;
		void polar_mode();
		void rect_mode();
		Vector operator+(const Vector &b) const;
		Vector operator-(const Vector &b) const;
		Vector operator-() const;
		Vector operator*(double n) const;
		friend Vector operator*(double n, const Vector & a);
		friend std::ostream &
			operator<< (std::ostream & os, const Vector & v);
	};

	const double Rad_to_deg = 45.0 / atan(1.0);
	double Vector::angval() const
	{
		if (x == 0.0 && y == 0.0)
			return 0.0;
		else
			return atan2(y, x);
	}
	void Vector::set_x(double n1, double n2)
	{
		x = n1 * cos(n2);
	}
	void Vector::set_y(double n1, double n2)
	{
		y = n1 * sin(n2);
	}
	Vector::Vector()
	{
		x = y = 0.0;
		mode = RECT;
	}
	Vector::Vector(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			n2 = n2 / Rad_to_deg;
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = RECT;
		}
	}
	void Vector::reset(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			n2 = n2 / Rad_to_deg;
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = RECT;
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	Vector Vector::operator+(const Vector &b) const
	{
		return Vector(x + b.x, y + b.y);
	}
	Vector Vector::operator-(const Vector &b) const
	{
		return Vector(x - b.x, y - b.y);
	}
	Vector Vector::operator-() const
	{
		return Vector(-x, -y);
	}
	Vector Vector::operator*(double n) const
	{
		return Vector(n * x, n * y);
	}
	Vector operator*(double n, const Vector & a)
	{
		return a * n;
	}
	std::ostream & operator<<(std::ostream & os, const Vector & v)
	{
		if (v.mode == Vector::RECT)
			os << "(x,y) = (" << v.x << ", " << v.y << ")";
		else if (v.mode == Vector::POL)
		{
			os << "(m,a) = (" << v.magval() << ", "
				<< v.angval() * Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
}
#endif // VECTOR_H_H

3.

#include "vector.h"
#include
#include
#include
int main()
{
	int i = 0, Max = 0, Min = 10000, sum = 0;
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	cout << "Enter target distance (q to quit): ";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
		}
		++i;
		sum += steps;
		if (steps > Max)
			Max = steps;
		if (Min > steps)
			Min = steps;
		steps = 0;
		result.reset(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
	}
	cout << i << "次测试中最高的步数是: " << Max << endl;
	cout << "最低的步数是: " << Min << endl;
	cout << "平均步数是: " << sum / i << endl;
	cout << "Bye!\n";
	cin.clear();
	while (cin.get() != '\n')
		continue;
	return 0;
}

4.

Time.h

#ifndef MYTIME3_H_
#define MYTIME3_H_
#include 
class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h, int m = 0);
	void AddMin(int m);
	void AddHr(int h);
	void Reset(int h = 0, int m = 0);
	friend Time operator+(const Time & t, const Time & t1);
	friend Time operator-(const Time & t, const Time & t1);
	friend Time operator*(const Time & t, double n);
	friend std::ostream & operator<<(std::ostream & os, const Time & t);

};
Time::Time()
{
	hours = minutes = 0;
}
Time::Time(int h, int m)
{
	hours = h;
	minutes = m;
}
void Time::AddMin(int m)
{
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}
void Time::AddHr(int h)
{
	hours += h;
}
void Time::Reset(int h, int m)
{
	hours = h;
	minutes = m;
}
Time operator+(const Time & t, const Time & t1)
{
	Time sum;
	sum.minutes = t.minutes + t1.minutes;
	sum.hours = t.hours + t1.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}
Time operator-(const Time & t, const Time & t1)
{
	Time diff;
	int tot1, tot2;
	tot1 = t1.minutes + 60 * t1.hours;
	tot2 = t.minutes + 60 * t.hours;
	diff.minutes = (tot2 - tot1) % 60;
	diff.hours = (tot2 - tot1) / 60;
	return diff;
}
Time operator*(const Time & t, double n)
{
	Time result;
	long totalminutes = t.hours * n * 60 + t.minutes * n;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
	os << t.hours << " hours, " << t.minutes << " minutes";
	return os;
}
#endif

main.cpp

#include "Time.h"
#include
int main()
{
	using std::cout;
	using std::endl;
	Time aida(3, 35);
	Time tosca(2, 48);
	Time temp;

	cout << "Aida and Tosca:\n";
	cout << aida << "; " << tosca << endl;
	temp = aida + tosca;
	cout << "Aida + tosca: " << temp << endl;
	temp = aida * 1.17;
	cout << "Aida * 1.17: " << temp << endl;
	cout << "10.0 * Tosca: " << tosca * 10.0 << endl;
	return 0;
}

5.

Stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_
#include 
using std::cout;
class Stonewt
{
private:
	enum Mode { STONE, FPO };
	int stone;
	double pds_left;
	double pounds;
	Mode mode;
public:
	Stonewt();
	~Stonewt() = default;
	Stonewt(int stn, double p);
	Stonewt(double p);
	Stonewt operator+(const Stonewt & b) const;
	Stonewt operator-(const Stonewt & b) const;
	Stonewt operator*(double n) const;
	void set_stone(int n, double f = 0);
	void set_pounds(double f);
	void st_mode();
	void fp_mode();
	friend std::ostream & operator << (std::ostream & os, const Stonewt & v);
};
Stonewt::Stonewt()
{
	mode = STONE;
	stone = 0;
	pounds = 0.0;
	pds_left = 0.0;
}
Stonewt::Stonewt(int stn, double p)
{
	stone = stn + (int(p) % 14);
	pds_left = int(p) % 14 + p - int(p);
	pounds = stone * 14 + pds_left;
	mode = STONE;
}
Stonewt::Stonewt(double p)
{
	pounds = p;
	stone = int(p) / 14;
	pds_left = p - stone * 14;
	mode = FPO;
}
Stonewt Stonewt::operator+(const Stonewt & b) const
{
	Stonewt temp;
	temp.stone = stone + b.stone;
	temp.pounds = pounds + b.pounds;
	temp.pds_left = pds_left + b.pds_left;
	if (temp.pds_left > 14)
	{
		int tem = temp.stone;
		temp.stone = temp.stone + (int(temp.pds_left) / 14);
		temp.pds_left -= (temp.stone - tem) * 14;
	}
	return temp;
}
Stonewt Stonewt::operator-(const Stonewt & b) const
{
	Stonewt temp;
	temp.stone = stone - b.stone;
	if (temp.stone > 0)
	{
		temp.pounds = pounds - b.pounds;
		temp.pds_left = pds_left - b.pds_left;
		if (temp.pds_left < 0)
		{
			temp.pds_left += 14;
			--temp.stone;
		}
	}
	else
	{
		cout << "不能用一个较小的数去减一个较大的数!";
		return *this;
	}
	return temp;
}
Stonewt Stonewt::operator*(double n) const
{
	Stonewt temp;
	temp.stone = n * stone;
	temp.pounds = n * pounds;
	temp.pds_left = n * pds_left;
	if (temp.pds_left > 14)
	{
		int tem = temp.stone;
		temp.stone = temp.stone + (int(temp.pds_left) / 14);
		temp.pds_left -= (temp.stone - tem) * 14;
	}
	return temp;
}

void Stonewt::st_mode()
{
	mode = STONE;
}
void Stonewt::fp_mode()
{
	mode = FPO;
}
void Stonewt::set_stone(int n, double f)
{
	stone = n + (int(f) % 14);
	pds_left = int(f) % 14 + f - int(f);
	pounds = stone * 14 + pds_left;
	mode = STONE;
}
void Stonewt::set_pounds(double f)
{
	stone = 0;
	pounds = f;
	pds_left = 0;
	mode = FPO;
}
std::ostream & operator << (std::ostream & os, const Stonewt & v)
{
	if (v.mode == Stonewt::STONE)
		os << "weighed " << v.stone << " stone, " << v.pds_left << " pounds\n";
	else if (v.mode == Stonewt::FPO)
		os << "weighed " << v.pounds << " pounds\n";
	else
		os << "Stone object mode is invalid";
	return os;
}
#endif

main.cpp

#include "Stonewt.h"
using std::cout;
int main()
{
	Stonewt wolfe(285.7);
	Stonewt hew(10, 15);
	cout << wolfe;
	cout << hew;
	wolfe.st_mode();
	cout << wolfe;
	wolfe = wolfe - hew;
	cout << wolfe;
	wolfe = wolfe + hew;
	cout << wolfe;
	wolfe = wolfe * 10;
	cout << wolfe;
	return 0;
}

6.

main.cpp

#include "Stonewt.h"
using std::cout;
using std::cin;
int main()
{
	Stonewt arr[6] = {
		Stonewt(11,0),
		Stonewt(10,1),
		Stonewt(12,10)
	};
	Stonewt temp(11, 0);
	for (int i = 3; i < 6; ++i)
	{
		double p;
		cout << "Enter pounds: ";
		cin >> p;
		arr[i] = Stonewt(p);
	}
	int Max = 0;
	int Min = 0;
	int num = 0;
	for (int j = 0; j < 6; ++j)
	{
		if (arr[Max] < arr[j])
			Max = j;
		if (arr[Min] > arr[j])
			Min = j;
		if (arr[j] >= temp)
			++num;
	}
	cout << "最大的元素是arr[" << Max << "] 最小的元素是 arr[" << Min
		<< "] 大于或等于11英石的数量是: " << num;
	return 0;
}

Stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_
#include 
using std::cout;
class Stonewt
{
private:
	enum { Lbs_per_stn = 14 };
	int stone;
	double pds_left;
	double pounds;
public:
	Stonewt();
	~Stonewt();
	Stonewt(int stn, double lbs);
	Stonewt(double lbs);
	bool operator<(const Stonewt & t) const;
	bool operator<=(const Stonewt & t) const;
	bool operator>(const Stonewt & t) const;
	bool operator>=(const Stonewt & t) const;
	bool operator==(const Stonewt & t) const;
	bool operator!=(const Stonewt & t) const;
};
Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / Lbs_per_stn;
	pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
	pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn * Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
bool Stonewt::operator<(const Stonewt & t) const
{
	return pounds < t.pounds;
}
bool Stonewt::operator<=(const Stonewt & t) const
{
	return pounds <= t.pounds;
}
bool Stonewt::operator>(const Stonewt & t) const
{
	return pounds > t.pounds;
}
bool Stonewt::operator>=(const Stonewt & t) const
{
	return pounds >= t.pounds;
}
bool Stonewt::operator==(const Stonewt & t) const
{
	return pounds == t.pounds;
}
bool Stonewt::operator!=(const Stonewt & t) const
{
	return pounds != t.pounds;
}
#endif

7.
main.cpp

#include "complex0.h"
using std::cout;
using std::cin;
int main()
{
	Complex a(3.0, 4.0);
	Complex c;
	cout << "Enter a complex number(q to quit):\n";
	while (cin >> c)
	{
		cout << "c is" << c << '\n';
		cout << "complex conjugate is " << ~c << '\n';
		cout << "a is " << a << '\n';
		cout << "a + c is " << a + c << '\n';
		cout << "a - c is " << a - c << '\n';
		cout << "a * c is " << a * c << '\n';
		cout << "2 * c is " << 2 * c << '\n';
		cout << "Enter a complex number (q to quit):\n";
	}
	cout << "Done!\n";
	return 0;
}

complex0.h

#ifndef complex_H_
#define complex_H_
#include 
using std::cout;
class Complex
{
private:
	double real;
	double imaginary;
public:
	Complex();
	~Complex() = default;
	Complex(double, double);
	Complex operator+(const Complex & c) const;
	Complex operator-(const Complex & c) const;
	Complex operator*(const Complex & c) const;
	friend Complex operator*(double n, const Complex & c);
	friend Complex operator~(const Complex & c);
	friend std::ostream & operator<<(std::ostream & os, const Complex & c);
	friend std::istream & operator>>(std::istream & is, Complex & c);
};

Complex::Complex()
{
	real = 0.0;
	imaginary = 0.0;
}
Complex::Complex(double r, double i)
{
	real = r;
	imaginary = i;
}
Complex Complex::operator+(const Complex & c) const
{
	Complex temp;
	temp.real = real + c.real;
	temp.imaginary = imaginary + c.imaginary;
	return temp;
}
Complex Complex::operator-(const Complex & c) const
{
	Complex temp;
	temp.real = real - c.real;
	temp.imaginary = imaginary - c.imaginary;
	return temp;
}
Complex Complex::operator*(const Complex & c) const
{
	Complex temp;
	temp.real = real * c.real - imaginary * c.imaginary;
	temp.imaginary = real * c.imaginary + imaginary * c.real;
	return temp;
}
Complex operator*(double n, const Complex & c) 
{
	Complex temp;
	temp.real = n * c.real;
	temp.imaginary = n * c.imaginary;
	return temp;
}
Complex operator~(const Complex &c)
{
	Complex temp;
	temp.real = c.real;
	temp.imaginary = -c.imaginary;
	return temp;
}
std::ostream & operator<<(std::ostream & os, const Complex & c)
{
	os << " (" << c.real << "," << c.imaginary << "i)";
	return os;
}
std::istream & operator>>(std::istream & is, Complex & c)
{
	cout << "real: ";
	if (is >> c.real)
	{
		cout << "imaginary: ";
		is >> c.imaginary;
	}
	return is;
}
#endif

你可能感兴趣的:(c++,primer,plus,C++,Primer,Plus,第六版,第十一章,编程练习答案)