#include "stdafx.h"
#include "Complex.h"
#include <iostream>
using namespace std;
istream & operator >>(istream &in,Complex &c)
{
in>>c.real>>c.imag;
return in;
}
ostream & operator <<(ostream &out,Complex &c)
{
if(c.imag>0)
out<<c.real<<"+"<<c.imag<<"i"<<endl;
else if(c.real==0)
out<<c.imag<<"i"<<endl;
else if(c.imag==0)
out<<c.real<<endl;
else out<<c.real<<c.imag<<"i"<<endl;
return out;
}
Complex::Complex()
{real=0;imag=0;}
Complex::Complex(double r,double i)
{
real=r;
imag=i;
}
Complex Complex::operator+(Complex &c1)
{
Complex c;
c.real=real+c1.real;
c.imag=imag+c1.imag;
return c;
}
Complex Complex::operator+(double r)
{
Complex c;
c.real=real+r;
c.imag=imag;
return c;
}
Complex operator+(double r,Complex &c1)
{
Complex c;
c.real=r+c1.real;
c.imag=c1.imag;
return c;
}
Complex Complex::operator-(void)
{
Complex c;
c.real=-real;
c.imag=-imag;
return c;
}
Complex Complex::operator-(Complex &c1)
{
Complex c;
c.real=real-c1.real;
c.imag=imag-c1.imag;
return c;
}
Complex Complex::operator-(double r)
{
Complex c;
c.real=real-r;
c.imag=imag;
return c;
}
Complex operator-(double r,Complex &c1)
{
Complex c;
c.real=r-c1.real;
c.imag=-c1.imag;
return c;
}
Complex Complex::operator++()
{
Complex c;
c.real=++real;
c.imag=++imag;
return c;
}
Complex Complex::operator++(int)
{
Complex temp(*this);
real++;
imag++;
return temp;
}
Complex Complex::operator--()
{
Complex c;
c.real=--real;
c.imag=--imag;
return c;
}
Complex Complex::operator--(int)
{
Complex temp(*this);
real--;
imag--;
return temp;
}
Complex Complex::operator*(Complex &c1)
{
Complex c;
c.real=real*c1.real-imag*c1.imag;
c.imag=real*c1.imag+imag*c1.real;
return c;
}
Complex Complex::operator*(double r)
{
Complex c;
c.real=real*r;
c.imag=imag*r;
return c;
}
Complex operator*(double r,Complex &c1)
{
Complex c;
c.real=r*c1.real;
c.imag=r*c1.imag;
return c;
}
Complex Complex::operator/(Complex &c1)
{
Complex c;
c.real=(real*c1.real+imag*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag);
c.imag=(imag*c1.real-real*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag);
return c;
}
Complex Complex::operator/(double r)
{
if(r)
{
Complex c;
c.real=real/r;
c.imag=imag/r;
return c;
}
else cout<<"Arithmetic Exception"<<endl;
}
Complex operator/(double r,Complex &c1)
{
Complex c;
c.real=r*c1.real/(c1.real*c1.real+c1.imag*c1.imag);
c.imag=-r*c1.imag/(c1.real*c1.real+c1.imag*c1.imag);
return c;
}