#ifndef Complex_Number_H
#define Complex_Number_H
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class ComplexNumber
{
private:
double real,imag;
public:
ComplexNumber();
ComplexNumber(double x,double y);
void display(string name);
void display();
void sets(double x,double y);
void gets(double& x,double& y);
void operator=(ComplexNumber other);
void operator=(string str);
ComplexNumber operator+(ComplexNumber& other);
ComplexNumber operator-(ComplexNumber& other);
ComplexNumber operator*(ComplexNumber& other);
bool operator==(ComplexNumber& c1);
friend ostream &operator<<(ostream& output,const ComplexNumber& C)
{
if(C.real!=0&&C.imag>0)
{
output << C.real << " + ";
if(C.imag!=1)output << C.imag;
output << "i" << endl;
}
else if(C.real==0&&C.imag!=0)
{
if(C.imag!=1&&C.imag!=-1)output << C.imag;
else if(C.imag==-1)output<<"-";
output << "i" << endl;
}
else if(C.real==0&&C.imag==0)
{
output << "0" << endl;
}
else if(C.real!=0&&C.imag==0)
{
output << C.real << endl;
}
else
{
output << C.real << " - ";
if(C.imag!=-1)output << -1*C.imag;
output << "i" << endl;
}
}
};
ComplexNumber::ComplexNumber()
{
real = 0;
imag = 0;
}
ComplexNumber::ComplexNumber(double x,double y)
{
real = x;
imag = y;
}
void ComplexNumber::gets(double& x,double& y)
{
x = real;
y = imag;
}
void ComplexNumber::sets(double x,double y)
{
real = x;
imag = y;
}
ComplexNumber ComplexNumber::operator+(ComplexNumber& other)
{
double otherreal,otherimag;
other.gets(otherreal,otherimag);
ComplexNumber ret(real + otherreal,imag + otherimag);
return ret;
}
ComplexNumber ComplexNumber::operator-(ComplexNumber& other)
{
double otherreal,otherimag;
other.gets(otherreal,otherimag);
ComplexNumber ret(real - otherreal,imag - otherimag);
return ret;
}
ComplexNumber ComplexNumber::operator*(ComplexNumber& other)
{
double otherreal,otherimag;
other.gets(otherreal,otherimag);
ComplexNumber ret(real * otherreal - imag * otherimag,real * otherimag + imag * otherreal);
return ret;
}
void ComplexNumber::operator=(ComplexNumber other)
{
double otherreal,otherimag;
other.gets(otherreal,otherimag);
real = otherreal;
imag = otherimag;
}
void ComplexNumber::operator=(string str)
{
string refined;
for(int i = 0;i < str.size();i++)
{
if(str[i] != ' ')refined+=str[i];
}
stringstream ss(refined);
if(refined.find("i") == -1)
{
ss >> real;
imag = 0;
}
else if(refined.find("+") == -1 && refined.find("-") == -1)
{
if(!(ss >> imag))imag = 1;
real = 0;
}
else if(refined.find("-") == -1)
{
char buf;
ss >> real >> buf;
if(!(ss >> imag))imag = 1;
}
else if(refined.find("+") == -1)
{
if(refined.find("-")==0)
{
if(!(ss >> imag))imag = -1;
real = 0;
}
else
{
char buf;
double temp;
ss >> real >> buf;
if(!(ss >> temp))imag = -1;
else imag = -1 * temp;
}
}
}
bool ComplexNumber::operator==(ComplexNumber& c1)
{
double x1,y1;
c1.gets(x1,y1);
if(x1 == real && y1 == imag)return true;
return false;
}
void ComplexNumber::display()
{
if(real!=0&&imag>0)
{
cout << real << " + ";
if(imag!=1)cout << imag;
cout << "i" << endl;
}
else if(real==0&&imag!=0)
{
if(imag!=1&&imag!=-1)cout << imag;
else if(imag==-1)cout<<"-";
cout << "i" << endl;
}
else if(real==0&&imag==0)
{
cout << "0" << endl;
}
else if(real!=0&&imag==0)
{
cout << real << endl;
}
else
{
cout << real << " - ";
if(imag!=-1)cout << -1*imag;
cout << "i" << endl;
}
}
void ComplexNumber::display(string name)
{
if(real!=0&&imag>0)
{
cout << name << " is " << real << " + ";
if(imag!=1)cout << imag;
cout << "i" << endl;
}
else if(real==0&&imag!=0)
{
cout << name << " is ";
if(imag!=1&&imag!=-1)cout << imag;
else if(imag==-1)cout<<"-";
cout << "i" << endl;
}
else if(real==0&&imag==0)
{
cout << name << " is " << "0" << endl;
}
else if(real!=0&&imag==0)
{
cout << name << " is " << real << endl;
}
else
{
cout << name << " is " << real << " - ";
if(imag!=-1)cout << -1*imag;
cout << "i" << endl;
}
}
#endif