# G 改错题—复数运算

G 改错题—复数运算

#include <stdio.h>
#include <iostream.h>

//对begin到end之间部分的代码进行修改，只提交begin到end部分的代码
//******************** begin ********************

class Complex
{
public:

Complex(double r,double i); //构造函数
void display(); //复数的输出
Complex operator +(Complex c1,Complex c2);
Complex operator +(double i);

private:
double real;
double imag;
};

Complex operator +(Complex c1,Complex c2)
{
Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}

Complex::Complex(double r,double i)
{
real=r;
imag=i;
}

void Complex::display()
{
cout << "(" << real << "," << imag << "i)" <<endl;

Complex   Complex::operator +(double i)
{
Complex c;
c.real = real + i;
c.imag = imag ;
return c;

}

//********************* end ********************

int main()
{
double r1,  i1,  r2,  i2 ;
cin>>r1>>i1;
cin>>r2>>i2;

Complex c1(r1,i1);
Complex c2(r2,i2);

Complex c3;
c3=c1+c2;
c3.display();

c3= c1 + 3.3;
c3.display();

c3=3.3+c1;
c3.display();

return 0;
}

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

``````(8,-6i)
(6.3,4i)
(6.3,4i)``````

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

class Complex
{
public:

Complex(double r=0,double i=0); //构造函数
void display(); //复数的输出
friend Complex operator +(Complex c1,Complex c2);
Complex operator +(double i);

private:
double real;
double imag;
};

Complex operator +(Complex c1,Complex c2)
{
Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}

Complex::Complex(double r,double i)
{
real=r;
imag=i;
}

void Complex::display()
{
cout << "(" << real << "," << imag << "i)" <<endl;
}

Complex   Complex::operator +(double i)
{
Complex c;
c.real = real + i;
c.imag = imag ;
return c;

}
int main()
{
double r1,  i1,  r2,  i2 ;
cin>>r1>>i1;
cin>>r2>>i2;

Complex c1(r1,i1);
Complex c2(r2,i2);

Complex c3;
c3=c1+c2;
c3.display();

c3= c1 + 3.3;
c3.display();

c3=3.3+c1;
c3.display();

return 0;
}

``````

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

class Complex
{
public:

Complex(double r=0,double i=0); //构造函数
void display(); //复数的输出
friend Complex operator +(Complex c1,Complex c2);
Complex operator +(double i);

private:
double real;
double imag;
};

Complex operator +(Complex c1,Complex c2)
{
Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}

Complex::Complex(double r,double i)
{
real=r;
imag=i;
}

void Complex::display()
{
cout << "(" << real << "," << imag << "i)" <<endl;
}

Complex   Complex::operator +(double i)
{
Complex c;
c.real = real + i;
c.imag = imag ;
return c;

}
int main()
{
double r1,  i1,  r2,  i2 ;
cin>>r1>>i1;
cin>>r2>>i2;

Complex c1(r1,i1);
Complex c2(r2,i2);

Complex c3;
c3=c1+c2;
c3.display();

c3= c1 + 3.3;
c3.display();

c3=3.3+c1;
c3.display();

return 0;
}

``````