编辑代码

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex(double real = 0, double imag = 0) :_real{ real }, _imag{ imag } {}
	double real()const { return _real; }
	void real(double real) { _real = real; }
	double imag()const { return _imag; }
	void imag(double imag) { _imag = imag; }

	//声明复数的输出
	friend ostream& operator<<(ostream& out, const Complex& c);

	//声明复数的共轭
	friend Complex operator ~(const Complex&);
	
	//声明复数的 加、减、乘、除
	friend Complex operator +(const Complex&,const Complex&);	// a+b
	friend Complex operator -(const Complex&,const Complex&);	// a-b
	friend Complex operator *(const Complex&,const Complex&);	// a*b
	friend Complex operator /(const Complex&,const Complex&);	// a/b

	//声明复数的 加、减、乘、除
	Complex& operator +=(const Complex&);	//a+=b
	Complex& operator -=(const Complex&);	//a-=b
	Complex& operator *=(const Complex&);	//a*=b
	Complex& operator /=(const Complex&);	//a/=b


private:
	double _real, _imag;
};
ostream& operator<<(ostream& o, const Complex& c)
{
	char tempC{ c._imag >= 0 ? '+' : '-' };
	o << c._real << " " << tempC << " " << (c._imag >= 0 ? c._imag : -c._imag) << "i";
	return o;
}

Complex operator~(const Complex &c)
{
	return Complex(c._real, -c._imag);
}

Complex operator+(const Complex &c1,const Complex &c2)	//c1+c2
{
	Complex temp;
	temp.real(c1._real + c2._real);
	temp.imag(c1._imag + c2._imag);
	return temp;
}
Complex operator-(const Complex &c1,const Complex &c2)	//c1-c2
{
	Complex temp;
	temp.real(c1._real - c2._real);
	temp.imag(c1._imag - c2._imag);
	return temp;
}

Complex operator*(const Complex &c1,const Complex &c2)	//c1*c2
{
	Complex temp;
	temp.real(c1._real*c2._real - c1._imag*c2._imag);
	temp.imag(c1._real*c2._imag + c1._imag*c2._real);
	return temp;
}

Complex operator/(const Complex &c1,const Complex &c2)	//c1/c2
{
	double temp{ c2._real*c2._real + c2._imag*c2._imag };
	Complex tempC=c1*(~c2);
	tempC._real /= temp;
	tempC._imag /= temp;
	return tempC;
}

Complex& Complex::operator+=(const Complex& c)
{
	_real += c._real;
	_imag += c._imag;
	return *this;
}

Complex& Complex::operator-=(const Complex& c)
{
	_real -= c._real;
	_imag -= c._imag;
	return *this;
}
Complex& Complex::operator*=(const Complex& c)
{
	_real = _real*c._real - _imag*c._imag;
	_imag = _imag*c._real + _real*c._imag;
	return *this;
}
Complex& Complex::operator/=(const Complex& c)
{
	double temp{ c._real*c._real + c._imag*c._imag };
	//Complex tempC(c._real, -c._imag);
	//*this = ((*this)*tempC);
	*this = (*this)*(~c);
	this->_real /= temp;
	this->_imag /= temp;
	return *this;
}

int main()
{
	Complex a(3, 4), b(-2, -3);
	cout << "复数 a = " << a << endl;
	cout << "复数 b = " << b << endl;
	cout << "a + b = " << a + b << endl<<endl;
	cout << "复数 a = " << a << endl;
	cout << "复数 b = " << b << endl;
	cout << "a - b = " << a - b << endl<<endl;
	cout << "复数 a = " << a << endl;
	cout << "复数 b = " << b << endl;
	cout << "a * b = " << a * b << endl<<endl;
	cout << "复数 a = " << a << endl;
	cout << "复数 b = " << b << endl;
	cout << "a / b = " << a / b << endl<<endl;
    //临时对象
    cout<<"创建一个临时对象:"<<Complex(3,6)<<endl;
    cout<<"创建一个默认构造临时对象:"<<Complex()<<endl;

    //实数与复数的 加、减、乘、除
    cout<<"9 + 复数 a ("<<a<<") = "<< 9+a <<endl;
    cout<<"9 - 复数 a ("<<a<<") = "<< 9-a <<endl;
    cout<<"9 * 复数 a ("<<a<<") = "<< 9*a <<endl;
    cout<<"9 / 复数 a ("<<a<<") = "<< 9/a <<endl<<endl;
    //复数与实数的 加、减、乘、除
    cout<<"复数 a ("<<a<<")"<<" + 9  = "<< a+9 <<endl;
    cout<<"复数 a ("<<a<<")"<<" - 9  = "<< a-9 <<endl;
    cout<<"复数 a ("<<a<<")"<<" * 9  = "<< a*9 <<endl;
    cout<<"复数 a ("<<a<<")"<<" / 9  = "<< a/9 <<endl;
    //虚数与复数的 加、减、乘、除
    //cout<<9i+a<<endl;
	return 0;
}