C++ primer plus 练习11

c++ Primer Plus(第六版)第十一章,编程练习之路 c++ Primer Plus(第六版)第十一章,个人学习记录,成长中 阅读详情
11-1
//Vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include
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
#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;
	}
}

//rankwalk.cpp
#include
using namespace std;
#include
#include
#include"vector.h"
#include

int main()
{
	using VECTOR::Vector;
	srand(time(0));
	ofstream outFile("rankwalk.txt");
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	cout << "Enter tarvet distance (q to quit):";
	while (cin >> target)
	{
		outFile << "Target Distance:  " << target << ",  ";
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		outFile << "Step Size :  " << dstep <

11-2
//Vector_h
#ifndef VECTOR_H_
#define VECTOR_H_
#include
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;//必须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
#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);
	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;
			x = mag * cos(ang);
			y = mag * sin(ang);
		}
		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)
		{
			double mag = n1;
			double 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  = 0.0;
			mode = RECT;
		}
	}
	double Vector::magval()const
	{
		double 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;
	}
	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;
	}
}

//rankwalk.cpp
#include
using namespace std;
#include
#include
#include"vector.h"

int main()
{
	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 tarvet 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;
	return 0;
}

11-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
//vect.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;
 }
}
//rankwalk.cpp
#include<iostream>
using namespace std;
#include<cstdlib>
#include<ctime>
#include"vector.h"
int main()
{
 using VECTOR::Vector;
 srand(time(0));
 int N, i;
 double max = -999;
 double min = +999;
 double total = 0;
 double direction;
 Vector step;
 Vector result(0.0, 0.0);
 unsigned long steps = 0;
 double target;
 double dstep;
 cout << "要测试几次?\n";
 cin >> N;
 for (i = 0; i < N; i++)
 {
  cout << "Enter tarvet distance (q to quit):";
  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++;
  }
  total += steps;
  if (min > steps) min = steps;
  if (max < steps) max = steps;
  steps = 0;
  if (i == N - 1) break;
  result.reset(0.0, 0.0);
 }
 cout << "Average = " << total / N << " steps. " << endl;
 cout << "Max = " << max << " steps. " << endl;
 cout << "Min = " << min << " steps. " << endl;
 cout << "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;
 cout << "Bye!\n";
 cin.clear();
 while (cin.get() != '\n')
  continue;
 return 0;
}


11-4

//mytime.h
#ifndef MYTIME_H_
#define MYTIME_H_
#include<iostream>

class Time
{
	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 &time, const Time & t);
	friend Time operator-(const Time &time, const Time & t);
	friend Time operator*(const Time &time, double n);
	friend Time operator*(double n, const Time &t){ return t * n; }
	friend std::ostream & operator << (std::ostream& os, const Time & t);
};
#endif
//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 &time, const Time & t)
{
	Time sum;
	sum.minutes = time.minutes + t.minutes;
	sum.hours = time.hours + t.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

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

Time operator*(const Time & time, double n)
{
	Time result;
	long totalminutes = time.hours * n * 60 + time.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;
}<pre class="cpp" name="code">//usetime.cpp
#include<iostream>
#include "mytime.h"
using std::cout;
using std::endl;
int main()
{
	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;
	return 0;
}



11-5
//stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include<iostream>
class Stonewt
{
	enum{Lbs_per_stn = 14};
	enum Mode{LBS , STN};
	Mode mode;
	int stone;
	double pds_left;
	double pounds;
	void set_lbs();
	void set_stn();
public:
	Stonewt();
	Stonewt(double lbs, int stn = 0, Mode = LBS);
	~Stonewt(){}
	friend std::ostream&operator<<(std::ostream&os, const Stonewt& st);
	Stonewt operator+(const Stonewt&st)const;
	Stonewt operator-(const Stonewt&st)const;
	Stonewt operator*(double n)const;
	friend Stonewt operator*(double n, const Stonewt & st){
		return st * n;
	}
	void stn_mode();
	void lbs_mode();
};
#endif
<pre class="cpp" name="code">//stonewt.cpp
#include<iostream>
#include"stonewt.h"
using std::cout;
void Stonewt::set_lbs()
{
	pounds = stone * Lbs_per_stn + pds_left;
}
void Stonewt::set_stn()
{
	stone = int(pounds) / Lbs_per_stn;
	pds_left = int(pounds) % Lbs_per_stn + pounds - int(pounds);
}
Stonewt::Stonewt()
{
	stone = 0;
	pounds = pds_left = 0.0;
	mode = LBS;
}
Stonewt::Stonewt(double lbs, int stn, Mode form)
{
	if (stn != 0) form = STN;
	mode = form;
	if (form == STN)
	{
		stone = stn;
		pds_left = lbs;
		set_lbs();
	}
	else if (form == LBS)
	{
		pounds = lbs;
		set_stn();
	}
	else
	{
		cout << "Incorrect 3rd argument to Stonewt() -- ";
		cout << "Stonewt set to 0\n";
		stone = 0;
		pounds = pds_left = 0.0;
		mode = LBS;
	}
}
Stonewt Stonewt:: operator+(const Stonewt&st)const
{
	double sum = st.pounds + pounds;
	return Stonewt(sum);
}
Stonewt Stonewt::operator-(const Stonewt&st)const
{
	double diff = pounds - st.pounds;
	return Stonewt(diff);
}
Stonewt Stonewt::operator*(double n)const
{
	double result = pounds * n;
	return Stonewt(result);
}

void Stonewt::lbs_mode()
{
	mode = LBS;

}

void Stonewt::stn_mode()
{
	mode = STN;
	set_stn();
}

std::ostream& operator << (std::ostream & os, const Stonewt&st)
{
	if (st.mode == Stonewt::LBS)
		os << st.pounds << " pounds\n";
	else if (st.mode == Stonewt::STN)
		os << st.stone << " stone, " << st.pds_left << " pounds\n";
	return os;
}
//stone.cpp
#include<iostream>
#include"stonewt.h"
using namespace std;
int main()
{
	Stonewt incognito = 275;
	Stonewt wolfe(285.7);
	Stonewt taft(8, 21);
	Stonewt temp;
	temp = wolfe + taft;
	cout << incognito << endl;
	cout << wolfe << endl;
	cout << taft << endl;
	cout << "LBS: " << temp << endl;
	temp.stn_mode();
	cout << "STN: " << temp << endl;
	temp = wolfe - incognito;
	cout << temp << endl;
	temp = taft * 3;
	cout << temp << endl;
}



11-6
//stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include<iostream>
class Stonewt
{
	enum{Lbs_per_stn = 14};
	enum Mode{LBS , STN};//两个模式
	Mode mode;
	int stone;
	double pds_left;
	double pounds;
	void set_lbs();
	void set_stn();
public:
	Stonewt();
	Stonewt(double lbs, int stn = 0, Mode = LBS);
	~Stonewt(){}
	friend std::ostream&operator<<(std::ostream&os, const Stonewt& st);
	Stonewt operator+(const Stonewt&st)const;
	Stonewt operator-(const Stonewt&st)const;
	Stonewt operator*(double n)const;
	friend Stonewt operator*(double n, const Stonewt & st){
		return st * n;
	}
	bool operator < (const Stonewt&st)const{ return pounds < st.pounds; }
	bool operator >(const Stonewt&st)const{ return st.pounds < pounds; }
	bool operator == (const Stonewt&st)const { return pounds == st.pounds; }
	bool operator != (const Stonewt&st)const { return !(pounds == st.pounds); }
	bool operator <= (const Stonewt&st)const { return (*this < st || *this == st); }
	bool operator >= (const Stonewt&st)const { return (st <= *this); }
	void stn_mode();
	void lbs_mode();
};
#endif

//stonewt.cpp
#include<iostream>
#include"stonewt.h"
using std::cout;
void Stonewt::set_lbs()
{
	pounds = stone * Lbs_per_stn + pds_left;
}
void Stonewt::set_stn()
{
	stone = int(pounds) / Lbs_per_stn;
	pds_left = int(pounds) % Lbs_per_stn + pounds - int(pounds);
}
Stonewt::Stonewt()
{
	stone = 0;
	pounds = pds_left = 0.0;
	mode = LBS;
}
Stonewt::Stonewt(double lbs, int stn, Mode form)//小心重定义
{
	if (stn != 0) form = STN;//stn不为0是,mode是STN
	mode = form;
	if (form == STN)
	{
		stone = stn;
		pds_left = lbs;
		set_lbs();
	}
	else if (form == LBS)
	{
		pounds = lbs;
		set_stn();
	}
	else
	{
		cout << "Incorrect 3rd argument to Stonewt() -- ";
		cout << "Stonewt set to 0\n";
		stone = 0;
		pounds = pds_left = 0.0;
		mode = LBS;
	}
}
Stonewt Stonewt:: operator+(const Stonewt&st)const
{
	double sum = st.pounds + pounds;
	return Stonewt(sum);
}
Stonewt Stonewt::operator-(const Stonewt&st)const
{
	double diff = pounds - st.pounds;
	return Stonewt(diff);
}
Stonewt Stonewt::operator*(double n)const
{
	double result = pounds * n;
	return Stonewt(result);
}

void Stonewt::lbs_mode()
{
	mode = LBS;
	set_lbs();
}

void Stonewt::stn_mode()
{
	mode = STN;
	set_stn();
}

std::ostream& operator << (std::ostream & os, const Stonewt&st)
{
	if (st.mode == Stonewt::LBS)
		os << st.pounds << " pounds\n";
	else if (st.mode == Stonewt::STN)
		os << st.stone << " stone, " << st.pds_left << " pounds\n";
	return os;
}<pre class="cpp" name="code">//stone.cpp
#include<iostream>
#include"stonewt.h"
using namespace std;
int main()
{
	Stonewt six[6] = { 10.2, 23, 1.1 };
	int i;
	double pounds;
	for (i = 3; i < 6; i++)
	{
		cout << "Enter the pounds:\n";
		cin >> pounds;
		six[i] = pounds;//记得设置值
	}
	Stonewt max = -999;
	Stonewt min = 999;
	Stonewt eleven(11);
	int n = 0;
	for (i = 0; i < 6; i++)
	{
		if (max < six[i]) max = six[i];
		if (min > six[i]) min = six[i];
		if (six[i] >= eleven) n++;
	}
	cout << "The min: " << min << endl;
	cout << "The max: " << max << endl;
	cout << "大于等于11: " << n << "个。 " << endl;
	return 0;
}



11-7
//complex0.h
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include<iostream>
class Complex
{
	double real;
	double imaginary;
public:
	Complex(){
		real = imaginary = 0.0;
	}
	Complex(double x, double y) :real(x), imaginary(y){}
	Complex operator+(const Complex &com)const;
	Complex operator-(const Complex &com)const;
	Complex operator*(const Complex &com)const;
	Complex operator*(const double &n)const;
	Complex operator~()const;
	friend Complex operator*(const double &n, const Complex &com){ return com * n ;}//注意分号
	friend std::ostream& operator<<( std::ostream&os, const Complex &com);//os不用const,记得ostream要&,返回引用
	friend std::istream& operator>>( std::istream&is, Complex &com);//cin的Complex不用const,因为要改值
};
#endif
//complex0.cpp
#include<iostream>
#include"complex0.h"
Complex  Complex:: operator+(const Complex &com)const
{
	return Complex((real + com.real), (imaginary + com.imaginary));
}

Complex Complex:: operator-(const Complex &com) const
{
	return Complex((real - com.real), (imaginary - com.imaginary));
}

Complex Complex::operator*(const double & n) const
{
	return Complex((real * n), (imaginary * n));
}

Complex Complex::operator*(const Complex &com)const
{
	double newreal = real * com.real - imaginary * com.imaginary;
	double newimaginary = real * com.imaginary + imaginary * com.real;
	return Complex(newreal, newimaginary);
}

Complex Complex::operator~()const
{
	return Complex(real, -imaginary);
}
std::ostream& operator<<(std::ostream&os, const Complex & com)
{
	os << "(" << com.real << "," << com.imaginary << "i)" ;
	return os;
}

std::istream &operator>>(std::istream&is, Complex &com)
{
	std::cout << "real:  ";
	is >> com.real;
	std::cout << "imaginary:   ";
	is >> com.imaginary;
	return is;
}
//main.cpp
#include<iostream>
#include"complex0.h"
using namespace std;
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;
}




C++ Primer Plus(第六版)编程练习答案 第11章 使用类 本章所有编程练习的工程源码可在此处下载(点击此链接下载),供大家参考交流!   1. 修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标示。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似: (……代码省略……) 本题要求将随机漫步者的位置写入到文件中,这些内容刚好和程序清单11.13,11.14,11... 阅读详情

相关推荐

C++ Primer Plus 第6版 第11章 编程练习答案

1. 修改程序清单11.15,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标示。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。 // 类描述中包含两种形式,设计这个类时,将使得用户修改了矢量的一种表示后,对象将自动更新另一种表示。 // 使对象拥有这种智能,使C++类的一个优点 // vector.h #ifndef VECTOR_H_ #define VECTOR_H_ #include <iostream> namespace VECTO

Anna_L的博客 515

第七章:函数--C++编程模块(二)

7.2函数参数和按值传递 C++通常按值传递参数,这意味着将数值参数传递给函数,而后者将其赋值给一个新的变量。 例如如下调用: double volume = cube(side) 其中,side是一个变量,在前面的程序运行中,其值为5。cube()的函数头如下: double cube (double x) 被调用时,该函数将创建一个新的名为x的double变量,并将其初始化为5。这样,cube()执行的操作将不会影响main()中的数据,因为cube()使用的是side的副本,而不是原来的数

m0_60961911的博客 174

DLL导出函数名隐藏实战:三种方法保护代码安全

在Windows软件开发中,动态链接库(DLL)的导出函数名是模块间通信的关键标识,也是逆向工程的重要突破口。理解PE文件格式中的导出表结构是掌握DLL工作原理的基础,它记录了函数地址、名称和序号的映射关系。通过隐藏或混淆这些导出名称,可以有效增加代码分析的难度,提升软件的知识产权保护和安全性。这一技术在软件保护、游戏反作弊和安全组件中具有重要价值。本文聚焦DLL导出函数名隐藏,结合Dependencies工具分析,详细解析仅序号导出、导出转发器和运行时动态修改三种实用方案,为开发者提供从原理到实践的完整指

weixin_29315091的博客 312

C++ 异常处理(一)

例: 计算两个数的调和平均数(两字数字的倒数的平均数的倒数) 表达式为:2.0* x * y / (x + y) 这里x = -y是不允许的,因为上述公式将导致被0除。 方法一:调用 abort() 函数 调用 abort() 函数,需头文件:cstdlib (或 stdlib.h) 其典型实现是向标准错误流(即cerr使用的错误流)发送消息 abnormal program term

D.D.U的博客 1509

企业数字化转型的理论指南:构建未来企业的关键策略与实践路径

数字化转型已经成为当今企业发展和市场竞争的核心驱动力,而在这一过程中,企业架构(EA)发挥着至关重要的作用。这本白皮书《世界级企业架构:建立和发展EA能力的领导者方法》提供了深入的理论指导,为企业如何通过企业架构实现有效的数字化转型提供了清晰的路径。本篇文章将从,解读该书的核心内容,揭示企业在数字化转型过程中应该如何通过EA的构建和实施,驱动组织变革与技术创新。

AZone架构院的博客 3176

利用Python脚本在ADS2024 Update2中动态绘制与验证LPDDR5眼图模板

本文详细介绍了如何在ADS2024 Update2中,利用Python脚本动态绘制与验证LPDDR5眼图模板。通过调用DDS Python接口,工程师可在仿真后无需重新运行,即可灵活添加、修改矩形或六边形眼图模板,并实现批量自动化处理,从而极大提升高速信号(如LPDDR5)的调试与验证效率。

raspberrypi5的博客 608

C primer plus 编程练习 13.11

//cpp 13.11-1 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define LEN 41 int main(void) { int ch; FILE *fp; char file[LEN]; int count = 0; puts("请输入您要打开的文件名:"); scanf("%40s",file); if...

weixin_41944412的博客 432

差分放大电路的计算

反馈,对于运算放大电路来说,运放工作在线性区,所以这里一定是负反馈,没有反馈(开环)或者是正反馈,那是比较器电路而不是放大电路,这时候运放工作在饱和区或称为非线性工作区,正因为饱和,输出才是电源电压的幅值。图2是一种带正反馈的运放电路,这里就不能叫运算放大电路了,因为运放的开环放大倍数理想是无限大,当然实际中不可能无限大,所以如下结构是迟滞电压比较器,运放工作在非线性区或饱和区。实际应用电路中,我们为了简化计算,也是用最简方法计算,经常使用的电路也是上述电路,令电阻相等关系,简化计算。

qq_40170041的博客 361

C++ primer plus 第6版第11章编程练习答案(全)

1. //头文件 #ifndef C__11_hpp #define C__11_hpp #include <iostream> namespace VECTOR { class Vector { public: enum Mode{RECT,POL}; private: double x; double y; double mag; double ang; M

gotoC_的博客 626

RDD2022 数据格式转换与清洗

Rdd2022数据集,是关于道路损伤的数据集,与rdd2020相比增加了两万多张图片但是由于格式不能被yolo直接使用且其中有大量的图片没有符合要求的标注,特写此文章记录数据清洗与格式化的过程。

蓝胖子的博客 9986

智能车图像处理4-入门级分析3 ---十字路口补线

PS :入门级文章适合新手,如果已经有基础可以去看进阶篇文章。

taiyuezyh的博客 1万+

C Primer Plus编程练习答案(Chapter11)

C Primer Plus编程练习答案(Chapter11)

XXxx_MR的博客 209

FreeRTOS全攻略:从入门到精通

本文聚焦 FreeRTOS,从基础概念出发,阐述其开源、轻量级等优势。着重介绍 FreeRTOS 入门知识,涵盖内存管理、任务操作、同步互斥机制等核心内容。其中,空闲任务与钩子函数保障系统基础运行与功能扩展;队列、信号量、互斥锁和事件组等机制,分别从数据传递、资源访问控制、优先级问题解决及多任务同步等方面,为开发者提供高效的任务间协作与资源管理方案,助力开发者掌握 FreeRTOS 核心技术,为嵌入式项目开发筑牢基础。

邓邓子的博客 6376

C++ Primer Plus(第6版)6.11编程练习7

【代码】C++ Primer Plus(第6版)6.11编程练习7。

apple_50569014的博客 290

华为OD机考双机位C卷 - 区间交集(Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 区间交集

算法大师! 1141

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

C++ Primer Plus(第6版)6.11编程练习1

【代码】C++ Primer Plus(第6版)6.11编程练习1。

apple_50569014的博客 143

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

C++ Primer Plus(第6版)6.11编程练习4

【代码】C++ Primer Plus(第6版)6.11编程练习4。

apple_50569014的博客 186

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

C++ Primer Plus(第6版)6.11编程练习9

【代码】C++ Primer Plus(第6版)6.11编程练习9。

apple_50569014的博客 176

C++ Primer Plus(第6版)6.11编程练习3

【代码】C++ Primer Plus(第6版)6.11编程练习3。

apple_50569014的博客 178

C++ Primer Plus(第6版)6.11编程练习8

【代码】C++ Primer Plus(第6版)6.11编程练习8。

apple_50569014的博客 155

C++ Primer Plus(第6版)6.11编程练习6

【代码】C++ Primer Plus(第6版)6.11编程练习6。

apple_50569014的博客 169

C++ Primer Plus(第6版)6.11编程练习5

【代码】C++ Primer Plus(第6版)6.11编程练习5。

apple_50569014的博客 145

C++ Primer Plus(第6版)6.11编程练习2

【代码】C++ Primer Plus(第6版)6.11编程练习2。

apple_50569014的博客 157

编程语言【C++ Primer Plus】编程练习答案——第11章 (待更新)

编程语言【C++ Primer Plus】编程练习答案——第11章 (待更新) 最近开学,事情较多,过两天更新。。。最近开学,事情较多,过两天更新。。。 百度网盘搜索 www.bdsoba.com 阿哇教育 www.awaedu.com 作文哥 www.zuowenge.cn 搜码吧 www.somanba.cn ...

149
上一篇: C++ primer plus 练习10
Z__
Z__
博客等级 码龄12年 0粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值