#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
friend ostream & operator<< (ostream &,Complex &);
friend istream & operator>> (istream &,Complex &);
private:
double real;
double imag;
};
//下面定义成员函数
int main()
{
Complex c1,c2,c3;
cout<<"请输入c1的值"<<endl;
cin>>c1;
cout<<"请输入c2的值"<<endl;
cin>>c2;
cout<<"c1=";
cout<<c1;
cout<<"c2=";
cout<<c2;
c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;
c3=c1-c2;
cout<<"c1-c2=";
cout<<c3;
c3=c1*c2;
cout<<"c1*c2=";
cout<<c3;
c3=c1/c2;
cout<<"c1/c2=";
cout<<c3;
system("pause");
return 0;
}
ostream & operator<< (ostream & output,Complex &t)
{
output << t.real ;
if(t.imag>=0)
{
output<<"+";
}
output<<t.imag<<"i";
cout<<endl;
return output;
}
istream & operator>> (istream &input,Complex &t)
{
input >> t.real >> t.imag;
return input;
}
Complex Complex::operator+(Complex &c2)
{
return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator-(Complex &c2)
{
return Complex(real-c2.real,imag-c2.imag);
}
Complex Complex::operator*(Complex &c2)
{
return Complex(real*c2.real-imag*c2.imag,real*c2.imag+imag*c2.real);
}
Complex Complex::operator/(Complex &c2)
{
double a=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
double b=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return Complex(a,b);
}