C++ primer plus 练习11

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(); rect_mode(); vector operator+(const &b)const; operator-(const operator-()const; operator*(double n)const; friend n, const & a); std::ostream &operator << (std::ostream &os, 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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值