【C++学习】习题记录 第十一章 使用类

第一题:修改程序11.5,使结果按要求格式输入到文件中

//file1 onehead.h
#ifndef ONEHEAD_H_
#define ONEHEAD_H_
#include<iostream>
namespace VECTOR
{
	class Vector
	{
	public:
		Vector();
		Vector(double n1, double n2, char form = 'r');
		void set(double n1, double n2, char form = 'r');
		~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);
	private:
		double x;
		double y;
		double mag;
		double ang;
		char mode;
		void set_mag();
		void set_ang();
		void set_x();
		void set_y();
	};
}
#endif
//file2 onehead.cpp
#include"onehead.h"
#include<cmath>
using std::sin;
using std::cos;
using std::atan2;
using std::cout;

namespace VECTOR
{
	const double Rad_to_deg = 57.2957795130823;
	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 = 'r';
	}
	Vector::Vector(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		}
		else if (form == 'p')
		{
			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 = 'r';
		}
	}
	void Vector::set(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		}
		else if (form == 'p')
		{
			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 = 'r';
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::polar_mode()
	{
		mode = 'p';
	}
	void Vector::rect_mode()
	{
		mode = 'r';
	}
	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 == 'r')
			os << "(x,y)=(" << v.x << "," << v.y << ")";
		else if (v.mode == 'p')
		{
			os << "(m,a)=(" << v.mag << "," << v.ang*Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
}
//file3 one.cpp
include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include"onehead.h"
int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	ofstream outfile1;
	outfile1.open("steorecord.txt");
	unsigned long steps = 0;
	double target;
	double dstep;

	cout << "Enter target distance(q to quit): ";

	while (cin >> target)
	{
		cout << "Enter step length: ";
		cin >> dstep;
		outfile1 << "Target Distrace : " << target;
		outfile1 << ", Step Size: " << dstep << endl;

		if (!(cin))
			break;
		while (result.magval()<target)
		{
			outfile1 << steps << ": " << result << endl;
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			result = result + step;
			steps++;
		}

		outfile1 << steps << ": " << result << endl;
		cout << "After " << steps << " steps,the subject has the following location: \n";
		cout << result << endl;

		outfile1 << "After " << steps << " steps,the subject has the following location: \n";
		outfile1 << result << endl;

		result.polar_mode();
		cout << " or\n" << result << endl;
		cout << "Average outward distance per step ="
			<< result.magval() / steps << endl;

		outfile1 << " or\n" << result << endl;
		outfile1 << "Average outward distance per step ="
			<< result.magval() / steps << endl;

		steps = 0;
		result.set(0.0, 0.0);

		cout << "Enter target distance(q to quit): ";
	}
	outfile1.close();
	cout << "Bye!\n";
	system("pause");
	return 0;
}

结果显示11.1.1

结果显示11.1.2

第二题:修改程序11.13和11.1不在存储角度和长度值,而是在需要时使用函数计算,应保持原类的公有借口不变,使用11.15对程序测试,结果应该与原来相同。

//file1 twohead.h
#ifndef TWOHEAD_H_
#define TWOHEAD_H_
#include<iostream>
namespace VECTOR
{
	class Vector
	{
	public:
		Vector();
		Vector(double n1, double n2, char form = 'r');
		void set(double n1, double n2, char form = 'r');
		~Vector();
		double xval()const{ return x; };
		double yval()const{ return y; };
		double magval()const{ return sqrt(x*x + y*y);; };
		double angval()const
		{
			if (x == 0.0&&y == 0.0)
				return  0.0;
			else
				return  atan2(y, x);
		};
		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);
	private:
		double x;
		double y;
		char mode;
		void set_x(double n1, double n2 );
		void set_y(double n1, double n2 );
	};
}
#endif
//file2 twohead.cpp
#include"twohead.h"
#include<cmath>
using std::sin;
using std::cos;
using std::atan2;
using std::cout;

namespace VECTOR
{
	const double Rad_to_deg = 57.2957795130823;

	void Vector::set_x(double n1, double n2 )
	{
		x = (n1)*cos(n2/Rad_to_deg);
	}
	void Vector::set_y(double n1, double n2 )
	{
		y = (n1)*sin(n2 / Rad_to_deg);
	}
	Vector::Vector()
	{
		x = y  = 0.0;
		mode = 'r';
	}
	Vector::Vector(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
		}
		else if (form == 'p')
		{
			set_x(n1,n2);
			set_y(n1,n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector()--";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = 'r';
		}
	}
	void Vector::set(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
		}
		else if (form == 'p')
		{
			set_x(n1,n2);
			set_y(n1,n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector()--";
			cout << "vector set to 0\n";
			x = y  = 0.0;
			mode = 'r';
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::polar_mode()
	{
		mode = 'p';
	}
	void Vector::rect_mode()
	{
		mode = 'r';
	}
	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 == 'r')
			os << "(x,y)=(" << v.x << "," << v.y << ")";
		else if (v.mode == 'p')
		{
			os << "(m,a)=(" << v.magval() << "," << v.angval() *Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
}
//file3 twomain.cpp
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include"twohead.h"
int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	ofstream outfile1;
	outfile1.open("steorecordnew.txt");
	unsigned long steps = 0;
	double target;
	double dstep;

	cout << "Enter target distance(q to quit): ";

	while (cin >> target)
	{
		cout << "Enter step length: ";
		cin >> dstep;
		outfile1 << "Target Distrace : " << target;
		outfile1 << ", Step Size: " << dstep << endl;
		if (!(cin))
			break;
		while (result.magval()<target)
		{
			outfile1 << steps << ": " << result << endl;
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			result = result + step;
			steps++;
		}

		outfile1 << steps << ": " << result << endl;
		cout << "After " << steps << " steps,the subject has the following location: \n";
		cout << result << endl;

		outfile1 << "After " << steps << " steps,the subject has the following location: \n";
		outfile1 << result << endl;

		result.polar_mode();
		cout << " or\n" << result << endl;
		cout << "Average outward distance per step ="
			<< result.magval() / steps << endl;

		outfile1 << " or\n" << result << endl;
		outfile1 << "Average outward distance per step ="
			<< result.magval() / steps << endl;

		steps = 0;
		result.set(0.0, 0.0);

		cout << "Enter target distance(q to quit): ";
	}
	outfile1.close();
	cout << "Bye!\n";
	system("pause");
	return 0;
}

结果显示11.2.1

结果显示11.2.2

第三题:修改11.15,报告N此测试中最高,最低及平均步数。

//file3 threemain.cpp 头文件及定义与第二题相同
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include"twohead.h"
int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	ofstream outfile1;
	outfile1.open("steorecordsave.txt");
	unsigned long steps = 0;
	double target;
	double dstep;

	unsigned long stepmax = 0;
	unsigned long stepmin = 0;
	unsigned long stepaveg = 0;
	unsigned long stepsum = 0;

	int N = 0;

	cout << "Enter target distance(q to quit): ";

	while (cin >> target)
	{
		cout << "Enter step length: ";
		cin >> dstep;
		outfile1 << "Target Distrace : " << target;
		outfile1 << ", Step Size: " << dstep << endl;
		if (!(cin))
			break;
		while (result.magval()<target)
		{
			outfile1 << steps << ": " << result << endl;
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			result = result + step;
			steps++;
		}

		N ++;
		if (stepmax <= steps)
			stepmax = steps;
		if (N==1)
		 stepmin=steps;
		if (stepmin >= steps)
			stepmin = steps;

		stepsum += steps;

		outfile1 << steps << ": " << result << endl;
		cout << "After " << steps << " steps,the subject has the following location: \n";
		cout << result << endl;

		outfile1 << "After " << steps << " steps,the subject has the following location: \n";
		outfile1 << result << endl;

		result.polar_mode();
		cout << " or\n" << result << endl;
		cout << "Average outward distance per step ="
			<< result.magval() / steps << endl;

		outfile1 << " or\n" << result << endl;
		outfile1 << "Average outward distance per step ="
			<< result.magval() / steps << endl;

		steps = 0;
		result.set(0.0, 0.0);

		cout << "Enter target distance(q to quit): ";
	}
	stepaveg = stepsum / N;
	cout << "The max steps is : " << stepmax << endl;
	cout << "The min steps is : " << stepmin << endl;
	cout << "The average steps is : " << stepaveg << endl;
	outfile1 << "The max steps is : " << stepmax << endl;
	outfile1 << "The min steps is : " << stepmin << endl;
	outfile1 << "The average steps is : " << stepaveg << endl;
	outfile1.close();
	cout << "Bye!\n";
	system("pause");
	return 0;
}

结果显示11.3

第四题:重写范例11.10-12,使用友元函数实现所有操作符重载。

//file4 fourhead.h
#ifndef FOURHEAD_H_
#define FOURHEAD_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)
	{
		Time result;
		long totalminutes = (t1.hours+t2.hours)*60 + t1.minutes+t2.minutes;
		result.hours = totalminutes / 60;
		result.minutes = totalminutes % 60;
		return result;
	}

	friend Time operator-(const Time & t1, const Time & t2)
	{
		Time result;

		long totalminutes = (t1.hours - t1.hours) * 60 + t1.minutes - t2.minutes;
		result.hours = totalminutes / 60;
		result.minutes = totalminutes % 60;
		return result;
	}
	friend Time operator*(const Time &t,double m)
	{
		Time result;
		long totalminutes = t.hours*m * 60 + t.minutes*m;
		result.hours = totalminutes / 60;
		result.minutes = totalminutes %60;
		return result;
	}

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

	friend std::ostream&operator<<(std::ostream & os, const Time & t)
	{
		os << t.hours << " hours, " << t.minutes << " minutes";
		return os;
	}
};
#endif
//file2 fourhead.cpp
#include"fourhead.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;

}
//file3 fourmain.cpp
#include<iostream>
#include"fourhead.h"
int main()
{
	using std::cout;
	using std::endl;
	Time aids(3, 35);
	Time tosca(2, 48);

	cout << "Aida and Tosca:\n";
	cout << aids << "; " << tosca << endl;
	cout << "Aida + Tosca: " << aids + tosca << endl;
	cout << "Aida * 1.17: " << aids *1.17 << endl;
	cout << "10 * Tosca: " << 10 * tosca << endl;

	system("pause");
	return 0;
}

结果显示11.4

第五题:重写Stonewt,使用状态控制显示格式,重载<<操作符,及加减法乘法,并测试。

//file1 fivehead.h
#ifndef FIVEHEAD_H_
#define FIVEHEAD_H_
#include<iostream>
class Stonewt
{
private:
	enum{Lbs_per_stn=14};
	int stone;
	double pds_left;
	double pounds;
	char mode = 'p';
public:
	Stonewt();
	Stonewt (double lbs);
	Stonewt (int stn, double lbs);
	void setmode(char s){mode = s;};
	Stonewt operator*(double m);
	friend std::ostream &operator<<(std::ostream &os, const Stonewt &t);
	friend Stonewt operator+(const Stonewt & s1, const Stonewt & s2);
	friend Stonewt operator-(const Stonewt & s1, const Stonewt & s2);
	friend Stonewt operator*( double n, Stonewt & s1);

};
#endif
//file2 fivehead.cpp
#include"fivehead.h"
#include<iostream>

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

 std::ostream &operator<<(std::ostream &os, const Stonewt &t)
{
	if (t.mode=='i')
	{
		os << t.stone << " stone," << t.pds_left << " pounds\n";
	}
	else if (t.mode == 'p')
	{
		os << t.pounds << " pounds\n";
	}
	return os;
}

 Stonewt operator+(const Stonewt & s1, const Stonewt & s2)
{
	Stonewt temp(0,0);
	const int n = 14;
	temp.pounds = s1.pounds + s2.pounds;
	temp.pds_left = int(temp.pounds) % n + temp.pounds - int(temp.pounds);
	temp.stone = int(temp.pounds) / n;
	return temp;
}
 Stonewt operator-(const Stonewt & s1, const Stonewt & s2)
 {
	 Stonewt temp(0, 0);
	 const int n = 14;
	 temp.pounds = s1.pounds - s2.pounds;
	 temp.pds_left = int(temp.pounds) % n + temp.pounds - int(temp.pounds);
	 temp.stone = int(temp.pounds) / n;
	 return temp;
 }
 Stonewt Stonewt:: operator*(double m)
 {
	 double temp = pounds*m;
	 return  Stonewt(temp);
 }
 Stonewt operator*(double n,Stonewt & s1 )
 {
	 return s1*n;
 }
//file3 fivemain.cpp
#include<iostream>
#include"fivehead.h"
int main()
{
	using std::cout;
	using std::endl;
	Stonewt st1(9, 2.8);
	Stonewt st2(20);
	//popins1.setmode('i');
	//cout << popins2;
	cout << "St1:" << st1;
	st1.setmode('i');

	cout << "Or" << endl<< st1;
	cout << "St2:" << st2;

	st2.setmode('i');

	cout << "Or" << endl << st2;
	cout <<"st1 * 2="<< st1 * 2  ;
	cout << "2 * st1="<<2 * st2 ;
	cout << "st1 - st2= " << st1 - st2;
	cout << "st1 + st2= " << st1 +st2;
	
	system("pause");
	return 0;
}

结果显示11.5

第六题:重写stonewt类,重载关系操作符,并使用数组声明六个对象,在开始时赋值3个,剩下的采用循环让用户输入,最后筛选最大,最小,及所有大于11的数量。

//file1 sixhead.h
#ifndef SIXHEAD_H_
#define SIXHEAD_H_
#include<iostream>
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);
	friend std::ostream &operator<<(std::ostream &os, const Stonewt &t);
	friend bool operator>(const Stonewt & s1, const Stonewt & s2);
	friend bool operator<(const Stonewt & s1, const Stonewt & s2);
	friend bool operator==(const Stonewt & s1, const Stonewt & s2);
	friend bool operator>=(const Stonewt & s1, const Stonewt & s2);
	friend bool operator<=(const Stonewt & s1, const Stonewt & s2);
	friend bool operator!=(const Stonewt & s1, const Stonewt & s2);

};
#endif
//file2 sixhead.cpp
#include"sixhead.h"
#include<iostream>
Stonewt::Stonewt()
{
	stone = 0;
	pds_left = 0;
	pounds = 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 = stn*Lbs_per_stn + lbs;
}

std::ostream &operator<<(std::ostream &os, const Stonewt &t)
{
	os << t.pounds << " pounds\n";
	return os;
}


 bool operator>(const Stonewt & s1, const Stonewt & s2)
{
	if (s1.pounds > s2.pounds)
		return true;
	else
		return false;
}

 bool operator<(const Stonewt & s1, const Stonewt & s2)
 {
	 if (s1.pounds < s2.pounds)
		 return true;
	 else
		 return false;
 }

 bool operator==(const Stonewt & s1, const Stonewt & s2)
 {
	 if (s1.pounds == s2.pounds)
		 return true;
	 else
		 return false;
 }

 bool operator>=(const Stonewt & s1, const Stonewt & s2)
 {
	 if (s1.pounds >= s2.pounds)
		 return true;
	 else
		 return false;
 }

 bool operator<=(const Stonewt & s1, const Stonewt & s2)
 {
	 if (s1.pounds <= s2.pounds)
		 return true;
	 else
		 return false;
 }

 bool operator!=(const Stonewt & s1, const Stonewt & s2)
 {
	 if (s1.pounds != s2.pounds)
		 return true;
	 else
		 return false;
 }
//file3 sixmain.cpp
#include"sixhead.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
	Stonewt stonear[6] = { 50, 20, 160};
	for (int i = 3; i < 6; i++)
	{
		double temp;
		cout << "Please Enter N0." << i+1<< " pounds: ";
		cin >> temp;
		cin.get();
		stonear[i] = temp;
	}
	Stonewt stand(11);
	Stonewt tempmin,tempmax;
	tempmin = tempmax = stonear[0];
	Stonewt *etem = new Stonewt[];
	int count = 0;
	for (int i = 1; i < 6; i++)
	{
		if (tempmin>stonear[i])
			tempmin = stonear[i];
		if (tempmax<stonear[i])
			tempmax = stonear[i];
		if (stonear[i] >= stand)
		{
			*etem = stonear[i];
			etem++;
			count++;
		}	
	}
	cout << "The max stone weight is:" <<tempmax;
	cout << "The min stone weight is:" << tempmin;
	if (count == 0)
	{
		cout << "NO stone more heavy than 11 pounds.";
	}
	else
	{
		cout << "Weight more than 11 pounds are: \n";
		for (int i = 0; i < count; i++)
		{
			etem--;
			cout << *etem;
		}
	}
	
	system("pause");
	return 0;
}

结果显示11.6

第七题:定义一个复数类来进行相应运算

//file1 sevenhead.h
#ifndef SEVENHEAD_H_
#define SEVENHEAD_H_
#include<iostream>
class complex
{
private:
	double r;
	double i;
public:
	complex();
	complex(double a, double b);
	friend std::ostream & operator<<(std::ostream & os, complex &c1);
	friend std::istream & operator>>(std::istream & os, complex &c1);
	friend complex operator+(const complex &c1, const  complex &c2);
	friend complex operator-(const complex &c1, const  complex &c2);
	friend complex operator*(const complex &c1, const  complex &c2);
	friend complex operator*(double n, const  complex &c1);
	friend complex operator~(complex &c1);
};
#endif
//file2 sevenhead.cpp
#include"sevenhead.h"
#include<iostream>
complex operator+(const complex &c1, const  complex &c2)
{
	complex temp;
	temp.r = c1.r + c2.r;
	temp.i = c1.i + c2.i;
	return temp;
}
complex operator-(const complex &c1, const  complex &c2)
{
	complex temp;
	temp.r = c1.r - c2.r;
	temp.i = c1.i - c2.i;
	return temp;
}
complex operator*(const complex &c1, const  complex &c2)
{
	complex temp;
	temp.r = c1.r * c2.r-c1.i*c2.i;
	temp.i = c1.r * c2.i+c1.i*c2.r;
	return temp;
}
complex operator*(double n, const  complex &c1)
{
	complex temp;
	temp.r = n*c1.r ;
	temp.i = n*c1.i;
	return temp;
}
complex operator~(complex &c1)
{
	complex temp;
	temp = c1;
	temp.i = -temp.i ;
	return temp;
}

std::istream & operator>>(std::istream & os, complex &c1)
{
	std::cout << "Enter real: ";
	os>> c1.r;
	if (os)//退出判断语句
	{
		std::cout << "Enter imagnary: ";
		os >> c1.i;
		return os;
	}
	else;
	return os;

}
std::ostream & operator <<(std::ostream & os,complex &c1)
{
	os << "(" << c1.r << ", " << c1.i << "i)" ;
	return os;
}
complex::complex(double a, double b)
{
	r = a;
	i = b;
}

complex::complex()
{
	r = 0;
	i = 0;
}
//file3 sevenmain.cpp
#include"sevenhead.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;
}

结果显示11.7

十一章结束,先跳过十二章,看第十三章吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉木渡香

感谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值