Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is in c++ 14.7 (Math: The complex class) A complex number has the f

ID: 3841969 • Letter: T

Question

This program is in c++

14.7 (Math: The complex class) A complex number has the form a bi, where a and b are real numbers and i is 1. The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction. multiplication, and division for complex numbers using the following formulas: a bi c di (a c) (b d)i a bi (c t d) (a c) (b d)i bi) (c di You can also obtain the absolute value for a complex number using the following formula: CA complex number can be interpreted as a point on a plane by identifying the a, b) values as the coordinates of the point. The absolute value of the com- plex number corresponds to the distance of the point to the origin, as shown in Figure 14.2)

Explanation / Answer

Below is the code and sample run of the program. Please don't forget to rate the answer if it helped. Thank you very much.

Complex.h

#include <cmath>
#include <iostream>
#include <sstream>
#include <iomanip>
class Complex
{
private:
double a, b;
public:
Complex():a(0),b(0)
{}
Complex(double a1):a(a1),b(0)
{}
Complex(double a1, double b1):a(a1),b(b1)
{}

double getRealPart()
{
return a;
}
double getImaginaryPart()
{
return b;
}
Complex add(const Complex &other) const
{
double a1 = a + other.a;
double b1 = b + other.b;
return Complex(a1, b1);
}

Complex subtract(const Complex &other) const
{
double a1 = a - other.a;
double b1 = b - other.b;
return Complex(a1, b1);
}

Complex multiply(const Complex &other) const
{
double a1 = (a * other.a) - (b * other.b);
double b1 = (b * other.a) + (a * other.b);
return Complex(a1, b1);
}

Complex divide(const Complex &other) const
{
double denom = other.a * other.a + other.b * other.b;
double a1 = ((a * other.a) + (b * other.b) ) / denom;
double b1 = ((b * other.a) - (a * other.b) ) / denom;
return Complex(a1, b1);
}

double abs()
{
return sqrt(a * a + b * b);
}

std::string toString() const
{
std::stringstream ss;
ss << std::setprecision(4) << a;
;
if(b != 0)
{
if( b > 0)
ss << " + " << std::setprecision(4) << b << "i";
else
ss << " - " << std::setprecision(4) << -b << "i";


}
return ss.str();
}

Complex & operator += ( const Complex &c2)
{
Complex ans = this->add(c2);
this->a = ans.a;
this->b = ans.b;
return *this;
}

Complex & operator -= ( const Complex &c2)
{
Complex ans = this->subtract(c2);
this->a = ans.a;
this->b = ans.b;
return *this;
}

Complex & operator *= ( const Complex &c2)
{
Complex ans = this->multiply(c2);
this->a = ans.a;
this->b = ans.b;
return *this;
}

Complex & operator /= ( const Complex &c2)
{
Complex ans = this->divide(c2);
this->a = ans.a;
this->b = ans.b;
return *this;
}

double operator [](int i)
{
if(i == 0)
return a;
else if(i == 1)
return b;
else
return 0;
}

Complex operator +()
{
return *this;
}

Complex operator -()
{
return Complex(-a, -b);
}
//pre increment
Complex operator ++()
{
a += 1;
b += 1;
return *this;
}
//pre decrement
Complex operator --()
{
a -= 1;
b -= 1;
return *this;
}

//post increment
Complex operator ++(int )
{
Complex before(a,b);
a += 1;
b += 1;
return before;
}
//post decrement
Complex operator --(int )
{
Complex before(a, b);
a -= 1;
b -= 1;
return before;
}


friend Complex operator + (const Complex &c1, const Complex &c2);
friend Complex operator - (const Complex &c1, const Complex &c2);
friend Complex operator * (const Complex &c1, const Complex &c2);
friend Complex operator / (const Complex &c1, const Complex &c2);
friend std::ostream & operator << (std::ostream &out, const Complex &c);
friend std::istream & operator >> (std::istream &in, Complex &c);

};

Complex operator + (const Complex &c1, const Complex &c2)
{
return c1.add(c2);
}
Complex operator - (const Complex &c1, const Complex &c2)
{
return c1.subtract(c2);
}

Complex operator * (const Complex &c1, const Complex &c2)
{
return c1.multiply(c2);
}

Complex operator / (const Complex &c1, const Complex &c2)
{
return c1.divide(c2);
}

std::ostream & operator << (std::ostream &out, const Complex &c)
{
out<< "(" << c.toString() << ")";
return out;
}

std::istream & operator >> (std::istream &input, Complex &c)
{
input >> c.a >> c.b;
return input;
}

main.cpp

#include "Complex.h"
#include <iostream>
using namespace std;
int main()
{
Complex c1, c2, c3, c4, c5, c6;
cout << "Enter the first complex number: ";
cin >> c1;
cout << "Enter the second complex number: ";
cin >> c2;
c3 = c1 + c2;
c4 = c1- c2;
c5 = c1 * c2;
c6 = c1 / c2;
cout<< c1 << " + " << c2 << "= " << c3.toString()<<endl;
cout<< c1 << " - " << c2 << "= " << c4.toString()<<endl;
cout<< c1 << " * " << c2 << "= " << c5.toString()<<endl;
cout<< c1 << " / " << c2 << "= " << c6.toString()<<endl;
cout<< "|" << c1.toString() << "| = " << c1.abs() <<endl;
cout<< c1 << "[0] = " << c1[0] <<endl;
cout<< c1 << "[1] = " << c1[1] <<endl;
cout<< "-" << c1 << " = " << -c1 <<endl;
cout<< "++ " << c1 << " = " << ++c1 <<endl;
cout<< c2 << " -- = " << c2-- <<endl;
cout<< "after post decrement "<<c2 <<endl;

}

output

Enter the first complex number: 3.5 5.5
Enter the second complex number: -3.5 1
(3.5 + 5.5i) + (-3.5 + 1i)= 0 + 6.5i
(3.5 + 5.5i) - (-3.5 + 1i)= 7 + 4.5i
(3.5 + 5.5i) * (-3.5 + 1i)= -17.75 - 15.75i
(3.5 + 5.5i) / (-3.5 + 1i)= -0.5094 - 1.717i
|3.5 + 5.5i| = 6.5192
(3.5 + 5.5i)[0] = 3.5
(3.5 + 5.5i)[1] = 5.5
-(3.5 + 5.5i) = (-3.5 - 5.5i)
++ (3.5 + 5.5i) = (4.5 + 6.5i)
(-3.5 + 1i) -- = (-3.5 + 1i)
after post decrement (-4.5)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote