#include <iostream.h>
class MyComplex{
private:
int real,imag;
public:
MyComplex();
MyComplex(int a);
MyComplex(int a,int b);
MyComplex(const MyComplex &v);
MyComplex &operator=(const MyComplex &rhs);
MyComplex &operator+=(const MyComplex& rhs);
MyComplex &operator-=(const MyComplex& rhs);
MyComplex &operator*=(const MyComplex& rhs);
MyComplex &operator/=(const MyComplex& rhs);
friend MyComplex operator+(MyComplex m,MyComplex n) ;
friend MyComplex operator-(MyComplex m,MyComplex n) ;
friend MyComplex operator*(MyComplex m,MyComplex n) ;
friend MyComplex operator/(MyComplex m,MyComplex n) ;
friend ostream &operator<<(ostream& s,MyComplex t);
friend istream &operator>>(istream& s,MyComplex& t);
};
MyComplex::MyComplex()
{
real=0;
imag=0;
}
MyComplex::MyComplex(int a)
{
real=a;
imag=0;
}
MyComplex::MyComplex(int a,int b)
{
real=a;
imag=b;
}
MyComplex::MyComplex(const MyComplex& v){
real=v.real;
imag=v.imag;
}
MyComplex& MyComplex::operator=(const MyComplex& rhs){
real=rhs.real;
imag=rhs.imag;
return *this;
}
MyComplex& MyComplex::operator+=(const MyComplex& rhs){
real+=rhs.real;
imag+=rhs.imag;
return *this;
}
MyComplex& MyComplex::operator-=(const MyComplex& rhs){
real-=rhs.real;
imag-=rhs.imag;
return *this;
}
MyComplex& MyComplex::operator*=(const MyComplex& rhs){
real*=rhs.real;
imag*=rhs.imag;
return *this;
}
MyComplex& MyComplex::operator/=(const MyComplex& rhs){
/*MyComplex x;
x.real=(real*rhs.real+imag*rhs.imag)/(rhs.real*rhs.real+rhs.imag*rhs.imag);
x.imag=(imag*rhs.real-real*rhs.imag)/(rhs.real*rhs.real+rhs.imag*rhs.imag);
return x;*/
real/=rhs.real;
imag/=rhs.imag;
return *this;
}
MyComplex operator+(MyComplex m,MyComplex n){
return MyComplex(m.real+n.real,m.imag+n.imag);
}
MyComplex operator-(MyComplex m,MyComplex n) {
return MyComplex(m.real-n.real,m.imag-n.imag);
}
MyComplex operator*(MyComplex m,MyComplex n)
{
return MyComplex(m.real*n.real-m.imag*n.imag,m.real*n.imag+m.imag*n.real);
}
MyComplex operator/(MyComplex m,MyComplex n)
{
MyComplex z ;
z.real=(m.real*n.real+m.imag*n.imag)/(n.real*n.real+n.imag*n.imag);
z.imag=(m.imag*n.real-m.real*n.imag)/(n.real*n.real+n.imag*n.imag);
return z;
}
ostream &operator<<(ostream& s,MyComplex t)
{
if(t.imag!=0)
{
s<<t.real;
s<<"+"<<t.imag<<"i"<<endl;
}
else
s<<t.real<<endl;
return s;
}
istream& operator>>(istream& s,MyComplex& t)
{
s>>t.real>>t.imag;
return s;
}
void main()
{
MyComplex a,b;
//int i,j,m,n;
cout<<"please input the first MyComplex a:"<<endl;
cin>>a;
cout<<"please input the second MyComplex b:"<<endl;
cin>>b;
//MyComplex a(m,n),b(i,j),c,d,e;
cout<<"a="<<a;
cout<<"b="<<b;
//c=a+b;
cout<<"a+b="<<a+b;
//d=a-b;
cout<<"a-b="<<a-b;
//e=a*b;
cout<<"a*b="<<a*b;
cout<<"a/b="<<a/b;
}