C++ Primer Plus第十一章编程练习

1、Vector类、随机漫步

//.h
#ifndef VEC_H_
#define VEC_H_
#include <iostream>
namespace VEC
{
	class Vector
	{	
	public:
		enum Mode { RECT, POL };
	private:
		Mode mode;
		double x;
		double y;
		double ang;
		double mag;		
		void set_x();
		void set_y();
		void set_ang();
		void set_mag();
	public:
		Vector();
		Vector(double n, double m, Mode mode = RECT);
		~Vector();
		void reset(double n, double m, Mode mode = RECT);
		double xval()const { return x; }
		double yval()const { return y; }
		double angval()const { return ang; }
		double magval()const { return mag; }
		void polar_mode();
		void rect_mode();
		//------------------------------
		friend std::ostream & operator<<(std::ostream & os, const Vector & v);
		friend Vector operator*(const double n, const Vector & v);
		//------------------------------
		Vector operator+(const Vector & v)const;
		Vector operator-(const Vector & v)const;
		Vector operator*(const double n)const;
		Vector operator-();
		operator double();
	};
}
#endif
//.cpp
#include "vec.h"
#include <iostream>
#include <cmath>

namespace VEC
{
	const double ANG_TO_DEG = 57.295779;
	void Vector::set_x()
	{
		x = mag * cos(ang);
	}
	void Vector::set_y()
	{
		y = mag * sin(ang);
	}
	void Vector::set_ang()
	{
		if (x == 0 && y == 0)
		{
			ang = 0;
		}
		else
		{
			ang = std::atan2(y, x);
		}
	}
	void Vector::set_mag()
	{
		mag = std::sqrt(x * x + y * y);
	}
	Vector::Vector()
	{
		mode = RECT;
		x = 0.0;
		y = 0.0;
		ang = 0.0;
		mag = 0.0;
	}
	Vector::Vector(double n, double m, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n;
			y = m;
			set_ang();
			set_mag();
		}
		else if (form == POL)
		{
			mag = n;
			ang = m / ANG_TO_DEG;
			set_x();
			set_y();
		}
		else
		{
			std::cout << "illegal mode\n";
			mode = RECT;
			x = 0.0;
			y = 0.0;
			ang = 0.0;
			mag = 0.0;
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::reset(double n, double m, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n;
			y = m;
			set_ang();
			set_mag();
		}
		else if (form == POL)
		{
			mag = n;
			ang = m / ANG_TO_DEG;
			set_x();
			set_y();
		}
		else
		{
			std::cout << "illegal mode, vector set to 0\n";
			mode = RECT;
			x = 0.0;
			y = 0.0;
			ang = 0.0;
			mag = 0.0;
		}
	}
	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	//-------------------------------
	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 << "(magnitude, angle) = (" << v.mag << ", " << (v.ang * ANG_TO_DEG) << ")";
		}
		else
		{
			os << "illegal mode";
		}
		return os;
	}
	Vector operator*(const double n, const Vector & v)
	{
		Vector amount;
		amount = v * n;
		return amount;
	}
	Vector Vector::operator+(const Vector & v)const
	{
                                         
		return Vector(x + v.x, y + v.y);
	}
	Vector Vector::operator-(const Vector & v)const
	{
		Vector sum;
		double x = 0; 
		x = this->x - v.x;
		double y = 0; 
		y = this->y - v.y;
		sum = Vector(x, y);  //方法需要一个新对象,推荐通过构造函数创建新对象,确保一致性。
		return sum;
	}

	Vector Vector::operator*(const double n)const
	{
		Vector amount;
		double x = 0; 
		double y = 0; 
		x = this->x * n;
		y = this->y * n;
		amount = Vector(x, y);
		return amount;
	}

	Vector Vector::operator-()
	{
		return Vector(-x, -y);
	}

	Vector::operator double()
	{
		return mag;
	}

}
//main().cpp
#include "vec.h"
#include <iostream>
#include <ftream>
#include <cstdlib>	//rand()、srand()、exit()
#include <ctime>	//time()

int main()
{
	using VEC::Vector;
	using std::cout;
	using std::cin;
	using std::endl;
	using std::ofstream;
	using std::string;

	ofstream fout;
	string fname = "donation.txt";
	fout.open(fname);
	if (!fout.is_open())
	{
		exit(EXIT_FAILURE);
	}
	else
	{
		cout << "success to " << fname << endl;
	}
	Vector origin(0, 0);
	Vector step;
	unsigned long steps = 0;
	double target;
	double dstep;
	double direction;

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

2、Vector类:通过方法提供另一种表示。

//.h
private:
		Mode mode;
		double x;
		double y;
		/*double ang;
		double mag;*/
		void set_x(double mag, double ang);
		void set_y(double mag, double ang);
		double get_ang()const;
		/*double get_mag();*/
public:
		double angval()const { return get_ang(); }
		double magval()const { return std::sqrt(x * x + y * y); }
//.cpp
	void Vector::set_x(double mag, double ang)
	{
		x = mag * cos(ang / ANG_TO_DEG);
	}
	void Vector::set_y(double mag, double ang)
	{
		y = mag * sin(ang / ANG_TO_DEG);
	}
	double Vector::get_ang()
	{
		if (x == 0 && y == 0)
		{
			/*ang = 0;*/
			return 0;
		}
		else
		{
			/*ang = std::atan2(y, x);*/
			return std::atan2(y, x);
		}
	}
	Vector::Vector()
	{
		mode = RECT;
		x = 0.0;
		y = 0.0;
		/*ang = 0.0;
		mag = 0.0;*/
	}
	Vector::Vector(double n, double m, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n;
			y = m;
			/*set_ang();
			set_mag();*/
		}
		else if (form == POL)
		{
			/*mag = n;
			ang = m / ANG_TO_DEG;*/
			set_x(n, m);
			set_y(n, m);
		}
		else
		{
			std::cout << "illegal mode\n";
			mode = RECT;
			x = 0.0;
			y = 0.0;
			/*ang = 0.0;
			mag = 0.0;*/
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::reset(double n, double m, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n;
			y = m;
			/*set_ang();
			set_mag();*/
		}
		else if (form == POL)
		{
			//mag = n;
			//ang = m / ANG_TO_DEG;
			set_x(n, m);
			set_y(n, m);
		}
		else
		{
			std::cout << "illegal mode, vector set to 0\n";
			mode = RECT;
			x = 0.0;
			y = 0.0;
		/*	ang = 0.0;
			mag = 0.0;*/
		}
	}

3、

	int n;

	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;
		cout << "Enter the loop: ";
		cin >> n;
		std::srand(std::time(0));
		double * p = new double[n];
		for (int i = 0; i < n; i++)
		{
			while (origin.magval() < target)
			{
				fout << steps << ": " << "(x, y) = (" << origin.xval() << ", " << origin.yval() << ")\n";
				direction = rand() % 360;
				step.reset(dstep, direction, VEC::Vector::POL);
				origin = origin + step;
				steps++;
			}
			fout << "After " << steps << " steps, the subject has the following location:\n"
				<< origin << endl;
			origin.polar_mode();
			fout << "or\n"
				<< origin << endl;
			fout << "Average outward distance per step = " << target / steps << endl;
			fout << endl;
			p[i] = target / steps;
			steps = 0;
			origin.reset(0, 0);
		}
		double temp_min = p[0];
		double temp_max = p[0];
		for (int i = 0; i < n; i++)
		{
			temp_min = temp_min < p[i] ? temp_min : p[i];
			temp_max = temp_max < p[i] ? p[i] : temp_max;
		}
		delete[] p;
		fout << "---------------------------\n";
		fout << "min: " << temp_min << endl;
		fout << "max: " << temp_max << endl;
		fout << "---------------------------\n";
		cout << "Enter target distance (q to quit): ";
	}

4、Time类

//.h
#ifndef "TIME_H_"
#define "TIME_H_"
#include <iostream>

namespace mytime
{
	class Time
	{
	private:
		int hours;
		int minutes;
	public:
		Time(int h = 0, int mi = 0);
		void Show()const;
		friend Time operator+(const Time & t1, const Time & t2);
		friend Time operator*(const Time & t, int n);
		friend Time operator*(int n, const Time& t);
		friend std::ostream & operator<<(std::ostream & os, const Time & t);
	};
}
#endif
//.cpp
#include "time.h"

namespace mytime
{
	Time::Time(int h, int mi)
	{
		hours = h;
		minutes = mi;
	}
	void Time::Show() const
	{
		std::cout << hours << " : " << minutes << std::endl;
	}
	std::ostream & operator<<(std::ostream & os, const Time & t)
	{
		os << t.hours << " : " << t.minutes;
		return os;
	}
	Time operator+(const Time & t1, const Time & t2)
	{
		int total_minutes = t1.hours * 60 + t1.minutes + t2.hours * 60 + t2.minutes;
		int h = total_minutes / 60;
		int m = total_minutes % 60;
		return Time(h, m);
	}
	Time operator*(const Time & t, int n)
	{
		int total_minutes = (t.minutes + t.hours * 60) * n;
		int h = total_minutes / 60;
		int m = total_minutes % 60;
		return Time(h, m);
	}
	Time operator*(int n, const Time& t)
	{
		return t * n;
	}
}

5、磅和英镑

#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>

class Stonewt
{
public: 
	enum Mode { LB, ST};
private:
	enum { LBS_PER_STN = 14};
	Mode mode;
	int stone;
	double lbs_left;
	double pounds;
public:
	Stonewt(double lbs, Mode form = LB);
	Stonewt(int stn, double lbs, Mode form = LB);
	Stonewt();
	~Stonewt();
	void set_st();
	void set_lb();
	Stonewt operator*(const double n)const;
	friend Stonewt operator*(const double n, const Stonewt & s);	
	friend Stonewt operator+(const Stonewt & a, const Stonewt & b);
	friend Stonewt operator-(const Stonewt & a, const Stonewt & b);
	friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif
//.cpp
#include "stonewt.h"

Stonewt::Stonewt(double lbs, Mode form)
{
	mode = form;
	if (mode == LB || mode == ST)
	{
		pounds = lbs;
		stone = lbs / LBS_PER_STN;
		lbs_left = pounds - stone * LBS_PER_STN;/*int(lbs) % LBS_PER_STN;*/
	}
	else
	{
		std::cout << "illegal mode\n"
			<< "object is set to 0\n";
		mode = LB;
		stone = 0;
		pounds = 0.0;
		lbs_left = 0.0;
	}
		
}
Stonewt::Stonewt(int stn, double lbs, Mode form)
{
	mode = form;
	if (mode == LB || mode == ST)
	{
		mode = form;
		stone = stn;
		lbs_left = lbs;
		pounds = stone * LBS_PER_STN + lbs_left;
	}
	else
	{
		std::cout << "illegal mode\n"
			<< "object is set to 0\n";
		mode = LB;
		stone = 0;
		pounds = 0.0;
		lbs_left = 0.0;
	}
		
}
Stonewt::Stonewt()
{
	mode = LB;
	stone = 0;
	pounds = 0.0;
	lbs_left = 0.0;
}
Stonewt::~Stonewt()
{		
}
void Stonewt::set_st()
{
	mode = ST;
}
void Stonewt::set_lb()
{
	mode = LB;
}
Stonewt Stonewt::operator*(const double n) const
{
	double total_lbs = pounds * n;
	return Stonewt(total_lbs);
}
Stonewt operator*(const double n, const Stonewt & s)
{
	return s * n;
}
Stonewt operator+(const Stonewt & a, const Stonewt & b)
{
	double sum_lbs = a.pounds + b.pounds;
	return Stonewt(sum_lbs);
}
Stonewt operator-(const Stonewt & a, const Stonewt & b)
{
	return Stonewt(a.pounds - b.pounds);
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
	if (s.mode == Stonewt::LB)
	{
		os << s.pounds << "(lb)";
	}
	else if (s.mode == Stonewt::ST)
	{
		os << s.stone << "(st) " << s.lbs_left << "(lb)";
	}
	else
	{
		os << "illegal mode";
	}
	return os;
}
//main().cpp
#include "stonewt.h"
#include <iostream>

int main()
{
	using WEIGHT::Stonewt;
	Stonewt cat(2, 16);
	Stonewt dog(50);
	/*Stonewt sum = cat + dog;*/
	/*Stonewt sum = dog - cat;*/
	Stonewt sum = dog * 2;
	std::cout << sum << std::endl;
	sum.set_st();
	std::cout << sum << std::endl;
	system("pause");
	return 0;
}

6、关系运算符的重载

//.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
public: 
	enum Mode { LB, ST};
private:
	enum { LBS_PER_STN = 14};
	Mode mode;
	int stone;
	double lbs_left;
	double pounds;
public:
	Stonewt(double lbs, Mode form = LB);
	Stonewt(int stn, double lbs, Mode form = LB);
	Stonewt();
	~Stonewt();
	void set_st();
	void set_lb();
	Stonewt operator*(const double n)const;
	friend Stonewt operator*(const double n, const Stonewt & s);
	friend Stonewt operator+(const Stonewt & a, const Stonewt & b);
	friend Stonewt operator-(const Stonewt & a, const Stonewt & b);
	friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
	friend bool operator>(const Stonewt & a, const Stonewt & b);
	friend bool operator<(const Stonewt & a, const Stonewt & b);
	friend bool operator>=(const Stonewt & a, const Stonewt & b);
	friend bool operator<=(const Stonewt & a, const Stonewt & b);
	friend bool operator==(const Stonewt & a, const Stonewt & b);
	friend bool operator!=(const Stonewt & a, const Stonewt & b);
}
	
#endif
#include "stonewt.h"

Stonewt::Stonewt(double lbs, Mode form)
{
	mode = form;
	if (mode == LB || mode == ST)
	{
		pounds = lbs;
		stone = lbs / LBS_PER_STN;
		lbs_left = pounds - stone * LBS_PER_STN;/*int(lbs) % LBS_PER_STN;*/
	}
	else
	{
		std::cout << "illegal mode\n"
			<< "object is set to 0\n";
		mode = LB;
		stone = 0;
		pounds = 0.0;
		lbs_left = 0.0;
	}

}
Stonewt::Stonewt(int stn, double lbs, Mode form)
{
	mode = form;
	if (mode == LB || mode == ST)
	{
		mode = form;
		stone = stn;
		lbs_left = lbs;
		pounds = stone * LBS_PER_STN + lbs_left;
	}
	else
	{
		std::cout << "illegal mode\n"
			<< "object is set to 0\n";
		mode = LB;
		stone = 0;
		pounds = 0.0;
		lbs_left = 0.0;
	}

}
Stonewt::Stonewt()
{
	mode = LB;
	stone = 0;
	pounds = 0.0;
	lbs_left = 0.0;
}
Stonewt::~Stonewt()
{

}
void Stonewt::set_st()
{
	mode = ST;
}
void Stonewt::set_lb()
{
	mode = LB;
}

Stonewt Stonewt::operator*(const double n) const
{
	double total_lbs = pounds * n;
	return Stonewt(total_lbs);
}
Stonewt operator*(const double n, const Stonewt & s)
{
	return s * n;
}

Stonewt operator+(const Stonewt & a, const Stonewt & b)
{
	double sum_lbs = a.pounds + b.pounds;
	return Stonewt(sum_lbs);
}
Stonewt operator-(const Stonewt & a, const Stonewt & b)
{
	return Stonewt(a.pounds - b.pounds);
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
	if (s.mode == Stonewt::LB)
	{
		os << s.pounds << "(lb)";
	}
	else if (s.mode == Stonewt::ST)
	{
		os << s.stone << "(st) " << s.lbs_left << "(lb)";
	}
	else
	{
		os << "illegal mode";
	}
	return os;
}
bool operator>(const Stonewt & a, const Stonewt & b)
{
	if (a.pounds > b.pounds)
	{
		return true;
	}
	else
	{
		return false;
	}

}
bool operator<(const Stonewt & a, const Stonewt & b)
{
	if (a.pounds < b.pounds)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator>=(const Stonewt & a, const Stonewt & b)
{
	if (a.pounds >= b.pounds)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator<=(const Stonewt & a, const Stonewt & b)
{
	if (a.pounds <= b.pounds)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator==(const Stonewt & a, const Stonewt & b)
{
	if (a.pounds == b.pounds)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator!=(const Stonewt & a, const Stonewt & b)
{
	if (a.pounds != b.pounds)
	{
		return true;
	}
	else
	{
		return false;
	}
}
#include "stonewt.h"
#include <iostream>

int main()
{
	using WEIGHT::Stonewt;
	Stonewt arr[6]
	{
		Stonewt(20),
		Stonewt(1, 5),
		Stonewt(26)
	};
	std::cout << "Enter the lb:\n";
	for (int i = 3; i < 6; i++)
	{
		double temp;
		std::cout << "#" << i << ": ";
		std::cin >> temp;
		arr[i] = temp;
	}
	Stonewt max = arr[0];
	Stonewt min = arr[0];
	for (int i = 0; i < 6; i++)
	{
		max = max < arr[i] ? arr[i] : max;
		min = min < arr[i] ? min : arr[i];
	}
	std::cout << "max: " << max << '\n'
		<< "min: " << min << '\n';
	Stonewt temp(11, 0);
	int count = 0;
	for (int i = 0; i < 6; i++)
	{
		if (arr[i] >= temp)
		{
			count++;
		}
	}
	std::cout << ">=11(st): " << count << '\n';
	system("pause");
	return 0;
}

7、复数

//.h
#ifnde COMPLEX_00_H_
#define COMPLEX_00_H_
#include <iostream>

namespace plurality
{
	class Complex
	{
	private:
		double real;
		double imaginary;
	public:
		Complex(double r, double i);
		Complex();
		~Complex();
		friend Complex operator+(const Complex & a, const Complex & b);
		friend Complex operator-(const Complex & a, const Complex & b);
		Complex operator*(const Complex & c)const;
		friend Complex operator*(double n, const Complex & c);
		Complex operator~()const;

		friend std::ostream & operator<<(std::ostream & os, const Complex & c);
		friend std::istream & operator >> (std::istream & is, Complex & c);
	};
}
#endif
//.cpp
#include "complex00.h"

namespace plurality
{
	Complex::Complex(double r, double i)
	{
		real = r;
		imaginary = i;
	}
	Complex::Complex()
	{
		real = 0.0;
		imaginary = 0.0;
	}
	Complex::~Complex()
	{

	}
	Complex Complex::operator*(const Complex & c) const
	{
		double r = real * c.real - imaginary * c.imaginary;
		double i = real * c.imaginary + imaginary * c.real;
		return Complex(r, i);
	}
	Complex Complex::operator~()const
	{
		return Complex(real, -imaginary);
	}
	Complex operator+(const Complex & a, const Complex & b)
	{
		return Complex(a.real + b.real, a.imaginary + b.imaginary);
	}
	Complex operator-(const Complex & a, const Complex & b)
	{
		return Complex(a.real - b.real, a.imaginary - b.imaginary);
	}
	Complex operator*(double n, const Complex & c)
	{
		return Complex(n* c.real, n * c.imaginary);
	}
	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)
	{
		std::cout << "real: ";
		is >> c.real;
		//if (!is)
		//{
		//	is.clear();
		//	std::cout << "error, real is set to 0";
		//	c.real = 0.0;
		//}
		//while (is.get() != '\n')
		//{
		//	continue;
		//}
		if (is)
		{
			std::cout << "imaginary: ";
			is >> c.imaginary;
		}
		//if (!is)
		//{
		//	std::cout << "error, imaginary is set to 0";
		//	c.imaginary = 0.0;
		//}
		return is;
	}
}
#include "complex00.h"
#include <iosream>

int main()
{
	using plurality::Complex;
	using std::cout;
	using std::cin;
	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 = " << a + c << '\n';
		cout << "a - c = " << a - c << '\n';
		cout << "a * c = " << a * c << '\n';
		cout << "2 * c = " << 2 * c << '\n';
		cout << "Enter a complex number (q to quit):\n";
	}
	cout << "Done!\n";
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值