# C++复数运算符重载（+与<<）

C++复数运算符重载（+与<<）

（1）参加运算的两个运算量可以都是类对象，也可以其中有一个是实数，顺序任意。例如，c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数)。
（2）输出的算数，在复数两端加上括号，实部和虚部均保留两位小数，如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。

#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
Complex():real(0),imag(0) {}
Complex(double r,double i):real(r),imag(i) {}
Complex operator+(Complex &);
Complex operator+(double &);
friend Complex operator+(double&,Complex &);
friend ostream& operator << (ostream& output, const Complex& c);
private:
double real;
double imag;
};

//将程序需要的其他成份写在下面，只提交begin到end部分的代码
//******************** begin ********************

//********************* end ********************
int main()
{
//测试复数加复数
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;

//测试复数加实数
double d;
cin>>real>>imag;
cin>>d;
c3=Complex(real,imag)+d;
cout<<"c1+d=";
cout<<c3;

//测试实数加复数
cin>>d;
cin>>real>>imag;
c1=Complex(real,imag);
c3=d+c1;
cout<<"d+c1=";
cout<<c3;

return 0;
}

``````3 4 5 -10
3 4 5
5 3 4``````

``````c1+c2=(8.00-6.00i)
c1+d=(8.00+4.00i)
d+c1=(8.00+4.00i)``````

``````#include<stdio.h>
#include<math.h>
int main()
{
float a1,a2,a3,a4,a5,a6;
float b1,b2,b3,b6;
char s1='+',s2='-';
scanf("%f%f%f%f",&a1,&b1,&a2,&b2);
scanf("%f%f%f",&a3,&b3,&a4);
scanf("%f%f%f",&a5,&a6,&b6);
if(b1+b2>=0)
printf("c1+c2=(%.2f%c%.2fi)\n",a1+a2,s1,fabs(b1+b2));
else
printf("c1+c2=(%.2f%c%.2fi)\n",a1+a2,s2,fabs(b1+b2));
if(b3>=0)
printf("c1+d=(%.2f%c%.2fi)\n",a3+a4,s1,fabs(b3));
else
printf("c1+d=(%.2f%c%.2fi)\n",a3+a4,s2,fabs(b3));
if(b6>=0)
printf("d+c1=(%.2f%c%.2fi)\n",a5+a6,s1,fabs(b6));
else
printf("d+c1=(%.2f%c%.2fi)\n",a5+a6,s2,fabs(b6));
return 0;
}
``````

``````
#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
Complex():real(0),imag(0) {}
Complex(double r,double i):real(r),imag(i) {}
Complex operator+(Complex &);
Complex operator+(double &);
friend Complex operator+(double&,Complex &);
friend ostream& operator << (ostream& output, const Complex& c);
private:
double real;
double imag;
};

//下面定义相关的函数
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real=this->real+c2.real;
c.imag=this->imag+c2.imag;
return c;
}

Complex Complex::operator+(double &d)
{

Complex c;
c.real=this->real+d;
c.imag=this->imag;
return c;
}

//Complex Complex::operator+(double d)
//{
//    Complex c(d,0);
//    return *(this)+c;
//}

Complex operator+(double &d1, Complex &c2)
{
Complex c(d1,0);
return c+c2;
}

ostream& operator << (ostream& output, const Complex& c)
{
output<<setiosflags(ios::fixed);
output<<setprecision(2);
output<<"("<<c.real;
if(c.imag>=0) output<<"+";
output<<c.imag<<"i)"<<endl;
return output;
}

int main()
{
//测试复数加复数
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;

//测试复数加实数
double d;
cin>>real>>imag;
cin>>d;
c3=Complex(real,imag)+d;
cout<<"c1+d=";
cout<<c3;

//测试实数加复数
cin>>d;
cin>>real>>imag;
c1=Complex(real,imag);
c3=d+c1;
cout<<"d+c1=";
cout<<c3;

return 0;
}``````