Create a calculator for complex number by creating a class of complex number with overloading all operators in it.(Operators: ++,--,+,-,/,*,>>,<<)
class Complex
{
double real;
double image;
public:
void setReal(double re)
{
real = re;
}
void setImage(double im)
{
image = im;
}
double getReal() const
{
return real;
}
double getImage() const
{
return image;
}
Complex(double re, double im): real(re), image(im){}
Complex operator+(Complex &complexNum)
{
complexNum.real = real + complexNum.real;
complexNum.image = image + complexNum.image;
return complexNum;
}
Complex operator-(Complex &complexNum)
{
complexNum.real = real - complexNum.real;
complexNum.image = image - complexNum.image;
return complexNum;
}
Complex operator*(Complex &complexNum)
{
double i, j;
i = real * complexNum.real - image * complexNum.image;
j = real * complexNum.image + complexNum.real * image;
complexNum.real = i;
complexNum.image = j;
return complexNum;
}
Complex operator / (Complex& complexNum)
{
double i, j, k;
k = real * real + complexNum.image * complexNum.image;
i = (real * complexNum.real + image * complexNum.image) / k;
j = (complexNum.real * image - real * complexNum.image) / k;
real = i;
image = j;
return *this;
}
Complex operator ++()
{
real++;
return *this;
}
Complex operator --()
{
real--;
return *this;
}
friend ostream &operator<< (ostream &out, const Complex &complexNum)
{
if (complexNum.image < 0)
out << complexNum.real << "+i(" << complexNum.image << ")\n";
else
out << complexNum.real << "+i" << complexNum.image << "\n";
return out;
}
friend istream &operator>>(istream &in, Complex &complexNum)
{
cout << "Введите реальную часть: ";
in >> complexNum.real;
cout << "Введите мнимую часть: ";
in >> complexNum.image;
return in;
}
};
Comments
Leave a comment