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

11-1
应该是修改list11.15,当当官方店买的,难道是盗版书吗。。。
打开file之后,操作跟cout类似

vect.h
#ifndef VECT_h_
#define VECT_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

vect.cpp

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

using namespace std;

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;
			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 <fstream>
#include "vect.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 file;
	file.open("C://test.txt");
	if (!file.is_open()) {
		cout << "Couldn't open the file!";
		exit(EXIT_FAILURE);
	}
	cout << "Enter target distance (q to quit):";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		file << "Target Distance: " << target << ", " << "Step Size: " << dstep << endl;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
			file << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")" << endl;
		}
		file << "After " << steps << " steps, the subject "
			"has the following location:\n";
		file << result << endl;
		result.polar_mode();
		file << " or\n" << result << endl;
		result.polar_mode();
		file << "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;
	return 0;
}

11-2
不储存长度角度的话,去掉mag,ang,setmag(),setang(),其他函数也需要修改,main.cpp不变
vect.h

#ifndef VECT_h_
#define VECT_h_
#include <iostream>
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode { RECT, POL };
	private:
		double x;
		double y;
		Mode mode;
	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;
		friend Vector operator*(double n, const Vector& a);
		friend std::ostream& operator << (std::ostream& os, const Vector& v);
	};
}

#endif

vect.cpp

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

using namespace std;

namespace VECTOR
{
	const double Rad_to_deg = 45.0 / atan(1.0);
	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, ang;
			mag = n1;
			ang = n2 / Rad_to_deg;
			x = mag * cos(ang);
			y = mag * sin(ang);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y;
			mode = RECT;
		}
	}
	void Vector::reset(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			x = n1* cos(n2);
			y = n1 * sin(n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n"; 
			x = y = 0.0;
			mode = RECT;
		}
	}

	Vector::~Vector()
	{
	}

	double Vector::magval() const
	{
		return sqrt(x * x + y * y);
	}
	double Vector::angval() const
	{ 
		return (x == 0.0 && y == 0.0)?0.0:atan2(y, x);
	}
	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;
	}
}

11-3
前两个没变

main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vect.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 file;
	int N,totalsteps = 0, maxsteps = 0, minsteps = 0;
	double averageSteps;

	file.open("C://test.txt");
	if (!file.is_open()) {
		cout << "Couldn't open the file!";
		exit(EXIT_FAILURE);
	}
	cout << "Enter times to try: ";
	cin >> N;
	cout << "Enter target distance (q to quit):";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		for (int i = 0; i < N; i++)
		{
			file << "#" << i << " Target Distance: " << target << ", " << "Step Size: " << dstep << endl;
			while (result.magval() < target)
			{
				direction = rand() % 360;
				step.reset(dstep, direction, Vector::POL);
				result = result + step;
				steps++;
				file << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")" << endl;
			}
			file << "After " << steps << " steps, the subject "
				"has the following location:\n";
			file << result << endl;
			result.polar_mode();
			file << " or\n" << result << endl;
			result.polar_mode();
			file << "Average outward distance per step = "
				<< result.magval() / steps << endl;
			if (maxsteps == 0 || minsteps == 0)
				maxsteps = minsteps = steps;
			if (maxsteps < steps)
				maxsteps = steps;
			else if (minsteps > steps)
				minsteps = steps;
			totalsteps += steps;
			steps = 0;
			result.reset(0.0, 0.0);
		}
		cout << "Average steps: " << totalsteps / N << endl;
		cout << "Max steps: " << maxsteps << endl;
		cout << "Min steps: " << minsteps << endl;
		cout << "Enter target distance (q to quit): ";
	}
	cout << "Bye!\n";
	cin.clear();
	while (cin.get() != '\n')
		continue;
	return 0;
}

11-4
主函数不变
mytime3.h

#ifndef MYTIME3_H_
#define MYTIME3_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);
	friend Time operator+(const Time& t1, const Time& t2);
	friend Time operator-(const Time& t1, const Time& t2);
	friend Time operator*(const Time& t, double m);
	friend Time operator*(double m, const Time& t);
	friend std::ostream& operator<<(std::ostream& os, const Time& t);
};
#endif // !MYTIME3_H_

mytime3.cpp

#include"mytime3.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 %= 60;
	return sum;
}

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

Time operator*(double mult, const Time& t)
{
	Time result;
	long totalminutes = t.hours * mult * 60 + t.minutes * mult;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}
Time operator*(const Time& t, double mult)
{ 
	Time result;
	long totalminutes = t.hours * mult * 60 + t.minutes * mult;
	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;
}

main.cpp

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

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: " << 10.0 * tosca << endl;
}

11-5
结合前面的例子,重载运算符
stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
	enum { Lbs_per_stn = 14 };
	enum Mode { STONE, FPO };
	int state;
	int	stone;
	double pds_left;
	double pounds;
	Mode mode;
public:
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();
	void set_mode();
	Stonewt operator+(const Stonewt& b) const;
	Stonewt operator-(const Stonewt& b) const;
	Stonewt operator*(double n) const;
	friend Stonewt operator*(double n, const Stonewt& a);
	friend std::ostream& operator<<(std::ostream& os, const Stonewt& v);
};

#endif // !STONEWT

stonewt.cpp

#include <iostream>
using std::cout;
#include "stonewt.h"

Stonewt::Stonewt()
{
    stone = pounds = pds_left = 0;
    mode = STONE;
}
Stonewt::Stonewt(double lbs)
{
    stone = int(lbs) / Lbs_per_stn;
    pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
    pounds = lbs;
    mode = FPO;
}
Stonewt::Stonewt(int stn, double lbs)
{
    stone = stn;
    pds_left = lbs;
    pounds = stn * Lbs_per_stn + lbs;
    mode = STONE;
}
Stonewt::~Stonewt() {}

void Stonewt::set_mode()
{
    mode = STONE;
}
Stonewt Stonewt::operator+(const Stonewt& b) const
{
    Stonewt temp;
    temp.pounds = pounds + b.pounds;
    temp.stone = int(temp.pounds) / 14;
    temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);
    return temp;
}
Stonewt Stonewt::operator-(const Stonewt& b) const
{
    Stonewt temp;
    temp.pounds = pounds - b.pounds;
    temp.stone = int(temp.pounds) / 14;
    temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);
    return temp;
}
Stonewt Stonewt::operator*(double n) const
{
    Stonewt temp;
    temp.pounds = pounds * n;
    temp.stone = int(temp.pounds) / 14;
    temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);
    return temp;
}
Stonewt operator*(double n, const Stonewt& a)
{
    Stonewt temp;
    temp.pounds = a.pounds * n;
    temp.stone = int(temp.pounds) / 14;
    temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);
    return temp;
}
std::ostream& operator<<(std::ostream& os, const Stonewt& s)
{
    if (s.mode == Stonewt::STONE)
        os << "weighed " << s.stone << " stone, " << s.pds_left << " pounds\n";
    else if (s.mode == Stonewt::FPO)
        os << "weighed " << s.pounds << " pounds\n";
    else
        os << "Stone object mode is invalid";
    return os;
}

main.cpp

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

11-6
stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
	enum { Lbs_per_stn = 14 };
	int state;
	int	stone;
	double pds_left;
	double pounds;
public:
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();
	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;
};

#endif // !STONEWT

stone.cpp

#include <iostream>
using std::cout;
#include "stonewt.h"

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

main.cpp

#include <iostream>
#include "Stonewt.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
	Stonewt arr[6] = {
		Stonewt(11.1,0),
		Stonewt(10.1,1),
		Stonewt(12.1,2)
	};
	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 << "Max element: " << Max << endl;
	cout << "Min element : " << Min << endl;
	cout << "Number of elements above 11: " << num << endl;
	return 0;
}

11-7
complex0.h

#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include <iostream>
class complex
{
private:
	double re;
	double im;
public:
	complex();
	complex(double x, double y);
	~complex();
	complex operator+(complex& a);
	complex operator-(complex& a);
	complex operator*(complex& a);
	friend complex operator*(double n, complex& a);
	friend complex operator*(complex& a, double n);
	complex operator~();
	friend std::ostream& operator<<(std::ostream& os, const complex& c);
	friend std::istream& operator>>(std::istream& is, complex& c);
};

#endif // !COMPLEX0_H_

complex.cpp

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

complex::complex()
{
	re = 0;
	im = 0;
}
complex::complex(double x, double y)
{
	re = x;
	im = y;
}

complex::~complex()
{}

complex complex::operator+(complex& a)
{
	return complex(re + a.re, im + a.im);
}
complex complex::operator-(complex& a)
{
	return complex(re - a.re, im - a.im);
}
complex complex::operator*(complex& a)
{
	return complex(re * a.re - im * a.im, re * a.im + im * a.re);
}
complex operator*(double n, complex& a)
{
	return complex(n * a.re, n * a.im);
}
complex operator*(complex& a, double n)
{
	return complex(n * a.re, n * a.im);
}
complex complex::operator~()
{
	return complex(re, -im);
}
std::ostream& operator<<(std::ostream& os, const complex& a)
{
	os << "(" << a.re << "," << a.im << "i)";
	return os;
}
std::istream& operator>>(std::istream& is, complex& c)
{
	cout << "real: ";
	is >> c.re;
	if (!is)
		return is;
	cout << "imaginary: ";
	is >> c.im;
	return is;
}

main.cpp

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

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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值