c++ Primer Plus 第十一章 答案

第1题
头文件和函数定义和原文相同
vector.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>

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;

		//friends
		friend Vector operator*(double n, const Vector& a);
		friend std::ostream& operator<<(std::ostream& os, const Vector& v);
	};
}
#endif // !VECTOR_H_

vector.cpp

#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
	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(), 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(), 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;
	}
}

usevector.cpp

#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vector.h"

int main()
{
	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;
	ofstream fout;
	fout.open("thewalk.txt");

	cout << "Enter target distance (q to quit): ";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
		{
			break;
		}

		fout << "Target Distance: " << target << ", Step Size: " << dstep << endl;

		while (result.magval() < target)
		{
			fout << steps << ": (x,y) = (" << result.xval() << ", " << result.yval() << ")\n";
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
		}
		cout << "After " << steps << " steps, the subject has the following location:\n";
		fout << "After " << steps << " steps, the subject has the following location:\n";
		cout << result << endl;
		fout << result << endl;
		result.polar_mode();
		cout << " or\n" << result << endl;
		fout << " or\n" << result << endl;
		cout << "Average outward distance per step = " << result.magval() / steps << endl;
		fout << "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";
	cin.clear();
	while (cin.get() != '\n')
	{
		continue;
	}
}

第2题
vector.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>

namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode { RECT, POL };

	private:
		double x;
		double y;
		Mode mode;

		void set_x(double mag, double ang);
		void set_y(double mag, double ang);

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

		//friends
		friend Vector operator*(double n, const Vector& a);
		friend std::ostream& operator<<(std::ostream& os, const Vector& v);
	};
}

#endif // !VECTOR_H_

vector.cpp

#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
	const double Rad_to_deg = 45.0 / atan(1.0);

	void Vector::set_x(double mag, double ang)
	{
		x = mag * cos(ang);
	}

	void Vector::set_y(double mag, double ang)
	{
		y = mag * sin(ang);
	}

	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)
		{
			double mag = n1;
			double ang = n2 / Rad_to_deg;
			set_x(mag, ang);
			set_y(mag, ang);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector(), 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)
		{
			double mag = n1;
			double ang = n2 / Rad_to_deg;
			set_x(mag,ang);
			set_y(mag,ang);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector(), vector set to 0\n";
			x = y = 0.0;
			mode = RECT;
		}
	}

	Vector::~Vector()
	{
	}

	double Vector::magval() const
	{
		double mag;
		mag = sqrt(x * x + y * y);
		return mag;
	}

	double Vector::angval() const
	{
		double ang;
		if (x == 0.0 && y == 0.0)
		{
			ang = 0.0;
		}
		else
		{
			ang = atan2(y, x);
		}
		return ang;
	}

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

main.cpp

#include <cstdlib>
#include <ctime>
#include "vector.h"

int main()
{
	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++;
		}
		cout << "After " << steps << " steps, the subject has the following location:\n";
		cout << result << endl;
		result.polar_mode();
		cout << " or\n" << result << endl;
		cout << "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";
	cin.clear();
	while (cin.get() != '\n')
	{
		continue;
	}
}

第3题
vector.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>

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

vector.cpp

#include <cmath>
#include "vector.h"

using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
	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;
	}
}

main.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"

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

	cout << "How many times do you want: ";
	cin >> times;
	int* steps_n = new int[times];
	for (int i = 0; i < times; i++)
	{
		cout << "Enter #" << i + 1 << " target distance: ";
		while (!(cin >> target))
		{
			cout << "Enter #" << i + 1 << " target distance: ";
			cin.clear();
			while (cin.get() != '\n')
			{
				continue;
			}
		}
		cout << "Enter #" << i + 1 << " step length: ";
		while (!(cin >> dstep))
		{
			cout << "Enter #" << i + 1 << " step length: ";
			cin.clear();
			while (cin.get() != '\n')
			{
				continue;
			}
		}

		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
		}
		steps_n[i] = steps;

		steps = 0;
		result.reset(0.0, 0.0);
	}

	int maxsteps = steps_n[0];
	int minsteps = steps_n[0];
	int sumsteps = 0, avesteps;

	for (int i = 0; i < times; i++)
	{
		maxsteps = maxsteps > steps_n[i] ? maxsteps : steps_n[i];
		minsteps = minsteps < steps_n[i] ? minsteps : steps_n[i];
		sumsteps += steps_n[i];
		cout << "#" << i + 1 << " steps = " << steps_n[i] << endl;
	}
	avesteps = sumsteps / times;

	delete[] steps_n;
	
	cout << "Max steps = " << maxsteps << endl;
	cout << "Min steps = " << minsteps << endl;
	cout << "Average steps = " << avesteps << endl;
}

第4题
mytime.h

#ifndef MYTIME_H_
#define MYTIME_H_
#include <iostream>

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);
	Time operator*(double n) const;

	friend Time operator+(const Time& t1, const Time& t2);
	friend Time operator-(const Time& t1, const Time& t2);
	friend Time operator*(double n, const Time& t);
	friend std::ostream& operator<<(std::ostream& os, const Time& t);
};
#endif // !MYTIME_H_

mytime.cpp

#include "mytime.h"

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& t1, const Time& t2)
{
	Time sum;
	sum.minutes = t1.minutes + t2.minutes;
	sum.hours = t1.hours + t2.hours + sum.minutes / 60;
	sum.minutes = sum.minutes % 60;
	return sum;
}

Time operator-(const Time& t1, const Time& t2)
{
	Time diff;
	int t1m, t2m;
	t1m = t1.hours * 60 + t1.minutes;
	t2m = t2.hours * 60 + t2.minutes;
	diff.minutes = (t1m - t2m) % 60;
	diff.hours = (t1m - t2m) / 60;
	return diff;
}

Time Time::operator*(double n) const
{
	Time result;
	result.minutes = hours * n * 60 + minutes * n;
	result.hours = result.minutes / 60;
	result.minutes = result.minutes % 60;
	return result;
}

Time operator*(double n, const Time& t)
{
	Time result;
	result.minutes = t.hours * n * 60 + t.minutes * n;
	result.hours = result.minutes / 60;
	result.minutes = result.minutes % 60;
	return result;
}

std::ostream& operator<<(std::ostream& os, const Time& t)
{
	os << t.hours << " hours, " << t.minutes << " minutes.";
	return os;
}

main.cpp

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

int main()
{
	using std::cout;
	using std::endl;
	Time a(3, 35);
	Time b(2, 31);

	cout << "a : " << a << endl;
	cout << "b : " << b << endl;
	cout << "a + b : " << a + b << endl;
	cout << "a - b : " << a - b << endl;
	cout << "a * 2 : " << a * 2 << endl;
	cout << "2 * a: " << 2 * a << endl;
}

第5题
stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>

class Stonewt
{
public:
	enum Mode { STONE, PDS_INT, PDS_DOUBLE };

private:
	enum { Lbs_per_stn = 14 };
	int stone;
	double pds_left;
	double pounds;
	Mode mode;

public:
	Stonewt();
	Stonewt(double lbs, Mode m = STONE);
	Stonewt(int stn, double lbs, Mode m = STONE);
	~Stonewt();
	void stone_mode();
	void pds_int_mode();
	void pds_double_mode();
	Stonewt operator*(double n) const;  //st*double

	friend Stonewt operator+(const Stonewt& st1, const Stonewt& st2);  //st1+st2
	friend Stonewt operator-(const Stonewt& st1, const Stonewt& st2);  //st1-st2
	friend Stonewt operator*(double n, const Stonewt& st);  //double*st
	friend std::ostream& operator<<(std::ostream& os, const Stonewt& st);
};
#endif // !STONEWT_H_

stonewt.cpp

#include "stonewt.h"

Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
	mode = STONE;
}

Stonewt::Stonewt(double lbs, Mode m)
{
	stone = (int)lbs / Lbs_per_stn;
	pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs;
	pounds = lbs;
	mode = m;
}

Stonewt::Stonewt(int stn, double lbs, Mode m)
{
	stone = stn;
	pds_left = lbs;
	pounds = (double)stn * Lbs_per_stn + lbs;
	mode = m;
}

Stonewt::~Stonewt() {};

void Stonewt::stone_mode()
{
	mode = STONE;
}

void Stonewt::pds_int_mode()
{
	mode = PDS_INT;
}

void Stonewt::pds_double_mode()
{
	mode = PDS_DOUBLE;
}

Stonewt Stonewt::operator*(double n) const  //st*double
{
	return Stonewt(pounds * n);
}

Stonewt operator+(const Stonewt& st1, const Stonewt& st2)  //st1+st2
{
	return Stonewt(st1.pounds + st2.pounds);
}

Stonewt operator-(const Stonewt& st1, const Stonewt& st2)  //st1-st2
{
	return Stonewt(st1.pounds - st2.pounds);
}

Stonewt operator*(double n, const Stonewt& st)  //double*st
{
	//return Stonewt(st.pounds * n);
	return st * n;
}

std::ostream& operator<<(std::ostream& os, const Stonewt& st)
{
	if (st.mode == Stonewt::Mode::STONE)
	{
		os << st.stone << " stone, " << st.pds_left << " pounds\n";
	}
	else if (st.mode == Stonewt::Mode::PDS_DOUBLE)
	{
		os << st.pounds << " pounds\n";
	}
	else if (st.mode == Stonewt::Mode::PDS_INT)
	{
		os << (int)st.pounds << " pounds\n";
	}
	else
	{
		std::cout << "Unknown Mode.\n";
	}
	return os;
}

main.cpp

#include "stonewt.h"
using std::cout;

int main()
{
	Stonewt s1(275);
	Stonewt s2(285.7);
	Stonewt s3(21, 8);
	Stonewt sa = s1 + s2;
	Stonewt sb = s1 + s3;
	Stonewt sc = 2 * s1;
	Stonewt sd = s1 * 2;
	Stonewt se = s2 - s1;

	cout << "Stone Mode:\n";
	cout << "S1 stone: " << s1;
	cout << "S2 stone: " << s2;
	cout << "S3 stone: " << s3;
	cout << "S1 + S2 stone: " << sa;
	cout << "S1 + S3 stone: " << sb;
	cout << "2 * S1 stone: " << sc;
	cout << "S1 *2 stone: " << sd;
	cout << "S2 - S1 stone: " << se;
	s1.pds_double_mode();
	s2.pds_double_mode();
	s3.pds_double_mode();
	sa.pds_double_mode();
	sb.pds_double_mode();
	sc.pds_double_mode();
	sd.pds_double_mode();
	se.pds_double_mode();
	cout << "Pounds-double Mode:\n";
	cout << "S1 pounds(double): " << s1;
	cout << "S2 pounds(double): " << s2;
	cout << "S3 pounds(double): " << s3;
	cout << "S1 + S2 pounds(double): " << sa;
	cout << "S1 + S3 pounds(double): " << sb;
	cout << "2 * S1 pounds(double): " << sc;
	cout << "S1 * 2 pounds(double): " << sd;
	cout << "S2 - S1 pounds(double): " << se;
	s1.pds_int_mode();
	s2.pds_int_mode();
	s3.pds_int_mode();
	sa.pds_int_mode();
	sb.pds_int_mode();
	sc.pds_int_mode();
	sd.pds_int_mode();
	se.pds_int_mode();
	cout << "Pounds-int Mode:\n";
	cout << "S1 pounds(int): " << s1;
	cout << "S2 pounds(int): " << s2;
	cout << "S3 pounds(int): " << s3;
	cout << "S1 + S2 pounds(int): " << sa;
	cout << "S1 + S3 pounds(int): " << sb;
	cout << "2 * S1 pounds(int): " << sc;
	cout << "S1 * 2 pounds(int): " << sd;
	cout << "S2 - S1 pounds(int): " << se;
}

第6题
stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_

class Stonewt
{
private:
	enum { Lbs_per_stn = 14 };
	int stone;
	double pds_left;
	double pounds;

public:
	Stonewt();
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	~Stonewt();
	void show_lbs() const;
	void show_stn() const;

	bool operator<(const Stonewt& st) const;
	bool operator<=(const Stonewt& st) const;
	bool operator>(const Stonewt& st) const;
	bool operator>=(const Stonewt& st) const;
	bool operator==(const Stonewt& st) const;
	bool operator!=(const Stonewt& st) const;
};
#endif // !STONEWT_H_

stonewt.cpp

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

Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
}

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 = (double)stn * Lbs_per_stn + lbs;
}

Stonewt::~Stonewt() {};

void Stonewt::show_stn() const
{
	std::cout << stone << " stone, " << pds_left << " pounds\n";
}

void Stonewt::show_lbs() const
{
	std::cout << pounds << " pounds\n";
}

bool Stonewt::operator<(const Stonewt& st) const
{
	return pounds < st.pounds;
}

bool Stonewt::operator<=(const Stonewt& st) const
{
	return pounds <= st.pounds;
}

bool Stonewt::operator>(const Stonewt& st) const
{
	return pounds > st.pounds;
}

bool Stonewt::operator>=(const Stonewt& st) const
{
	return pounds >= st.pounds;
}

bool Stonewt::operator==(const Stonewt& st) const
{
	return pounds == st.pounds;
}

bool Stonewt::operator!=(const Stonewt& st) const
{
	return pounds != st.pounds;
}

main.cpp

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

int main()
{
	using std::cout;
	using std::endl;

	Stonewt st[6] = { 175,28.5,{26,7} };
	for (int i = 3; i < 6; i++)
	{
		double pounds;
		cout << "Enter #" << i + 1 << " value(pounds): ";
		std::cin >> pounds;
		st[i] = pounds;
	}

	Stonewt maxst = st[0], minst = st[0];
	Stonewt sttemp(11, 0);
	int n = 0;
	for (int i = 0; i < 6; i++)
	{
		maxst = maxst > st[i] ? maxst : st[i];
		minst = minst < st[i] ? minst : st[i];
		st[i].show_stn();
		if (st[i]>=sttemp)
		{
			n++;
		}
	}
	cout << "Min = ";
	minst.show_stn();
	cout << "Max = ";
	maxst.show_stn();
	cout << "There are " << n << " bigger than 11 stone.\n";
}

第7题
complex0.h

#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include <iostream>

class Complex0
{
private:
	double real_num;
	double imaginary_num;

public:
	Complex0();
	Complex0(double real, double imaginary);
	~Complex0();
	Complex0 operator+(const Complex0& c) const;
	Complex0 operator-(const Complex0& c) const;
	Complex0 operator*(const Complex0& c) const;
	Complex0 operator*(double n) const;
	Complex0 operator~() const;

	friend Complex0 operator* (double n, const Complex0& c);
	friend std::istream& operator>>(std::istream& is, Complex0& c);
	friend std::ostream& operator<<(std::ostream& os, const Complex0& c);
};

#endif // !COMPLEX0_H_=

complex0.cpp

#include "complex0.h"

Complex0::Complex0()
{
	real_num = imaginary_num = 0.0;
}

Complex0::Complex0(double real, double imaginary)
{
	real_num = real;
	imaginary_num = imaginary;
}

Complex0::~Complex0() {}

Complex0 Complex0::operator+(const Complex0& c) const
{
	return Complex0(real_num + c.real_num, imaginary_num + c.imaginary_num);
}

Complex0 Complex0::operator-(const Complex0& c) const
{
	return Complex0(real_num - c.real_num, imaginary_num - c.imaginary_num);
}

Complex0 Complex0::operator*(const Complex0& c) const
{
	return Complex0(real_num * c.real_num - imaginary_num * c.imaginary_num, real_num * c.imaginary_num + imaginary_num * c.real_num);
}

Complex0 Complex0::operator*(double n) const
{
	return Complex0(n * real_num, n * imaginary_num);
}

Complex0 Complex0::operator~() const
{
	return Complex0(real_num, -imaginary_num);
}

Complex0 operator*(double n, const Complex0& c)
{
	return c * n;
}

std::istream& operator>>(std::istream& is, Complex0& c)
{
	std::cout << "real: ";
	is >> c.real_num;
	std::cout << "imaginary: ";
	is >> c.imaginary_num;
	return is;
}

std::ostream& operator<<(std::ostream& os, const Complex0& c)
{
	os << "(" << c.real_num << ", " << c.imaginary_num << "i)";
	return os;
}

main.cpp

#include <iostream>
using namespace std;
#include "complex0.h"

int main()
{
	Complex0 a(3.0, 4.0);
	Complex0 c;
	cout << "Enter a complex number (q to quit):\n";
	while (cin >> c)
	{
		cout << "c is " << c << endl;
		cout << "complex conjugate is " << ~c << endl;
		cout << "a is " << a << endl;
		cout << "a + c is " << a + c << endl;
		cout << "a - c is " << a - c << endl;
		cout << "a * c is " << a * c << endl;
		cout << "2 * c is " << 2 * c << endl;
		cout << "Enter a complex number (q to quit):\n";
	}
	cout << "Done!\n";
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值