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

Create a class named COMPLEX to represent a complex number and allow the arithme

ID: 3556533 • Letter: C

Question

 Create a class named COMPLEX to represent a complex           number and allow the arithmetic operators to function          properly.           Complete the class named COMPLEX so that arithmetic can be performed           between a pair of COMPLEX objects, a COMPLEX and a           double, and a double and a COMPLEX using the           arithmetic operators.                    Write a main( ) that reads in five doubles. The first          two will be the real and imaginary parts of a first           COMPLEX, the second two will be the real and           imaginary parts of a second COMPLEX, and the fifth           value will be a solo double value. Use these values           to build two COMPLEX objects and exercise the           arithmetic operators for the class COMPLEX.  Notes: -----   - Double check your math.   - The "prescribed" inputs are defined in advance for testing.      By knowing the order and number of inputs your program      expects, various tests can be made by supplying different      inputs for different runs of the program.        - Arithmetic formulas for a COMPLEX:     a+bi + c+di = (a+c) + (b+d)i     a+bi - c+di = (a-c) + (b-d)i     a+bi * c+di = (ac-bd) + (bc+ad)i     a+bi / c+di = (a+bi / c+di) * (c-di / c-di)                 = (ac+bd)/(cc+dd) + (bc-ad)/(cc+dd)i     +(a+bi)     = a+bi     -(a+bi)     = -a-bi   - Try to eliminate redundant code.      - Just read the five doubles, nothing else. 

Explanation / Answer

//-----------------------------------
// Complex Number
//-----------------------------------
#include<iostream>
#include<math.h>

using namespace std;

//---------------------------
// class complex
//---------------------------
class complex{
private:
float real;
float img;
public:
complex(float x , float y) : real(x) ,img(y){ }
complex operator+(complex c2);
complex operator-(complex c1);
complex operator-();
complex operator*(complex c1);
void showData();
};

//----------------------------------------------
// overloadin + operator for complex class
//-----------------------------------------------
complex complex::operator+(complex c2)
{
float real2=0,img2=0;
real2 = real + c2.real;
img2 = img + c2.img;
complex c3(real2,img2);
return c3;
}

//--------------------------------------------------------
// overloading -(binary) operator for complex class
//--------------------------------------------------------
complex complex::operator-(complex c1)
{
float real2=0,img2=0;
real2 = real - c1.real;
img2 = img - c1.img;
complex c3(real2,img2);
return c3;
}

//---------------------------------------------------------
// overloading -(unary) operator for complex class
//---------------------------------------------------------
complex complex::operator-()
{
float real2=0,img2=0;
real2 = -real;
img2 = -img;
complex c3(real2,img2);
return c3;
}


//-----------------------------------------------------------
// overlaoding * operator for complex class
//-----------------------------------------------------------
complex complex::operator*(complex c1)
{
float real2=0, img2=0;
real2 = (real*(c1.real)) - (img*(c1.img));
img2 = ((real*(c1.img)) + (img*(c1.real)));
complex c3(real2,img2);
return c3;
}

//-----------------------------------------------------------
// function to show dat of complex class
//-----------------------------------------------------------
void complex::showData()
{
if(img > 0)
{
cout << real << " + " << img << "i"<< endl;
}
else
{
cout << real << " - " << -img << "i" << endl;
}
}

//------------------------------------------
// MAIN FUNCTION
//------------------------------------------
int main()
{
float real,img;
int decision =0;
cout << "Enter the real part of the complex number : ";
cin >> real;
cout << "Enter the imaginary part of the complex number : ";
cin >> img;
complex c1(real,img);
do
{
cout << " MENU " << endl;
cout << "ENTER YOUR CHOICE " << endl;
cout << "1.ADDITION " << endl;
cout << "2.SUBTRACTION " << endl;
cout << "3.NEGATION " << endl;
cout << "4.MULTIPLICATION " << endl;
cout << "5.EXIT " <<endl;
cin >> decision;
complex c3(0,0),c5(0,0),c6(0,0),c7(0,0);
c7 = c5 + c7 + c6;
switch (decision)
{
case 1:
{
cout << "Enter the real part of the complex number : ";
cin >> real;
cout << "Enter the imaginary part of the complex number : ";
cin >> img;
complex c2(real,img);
c3 = c1 + c2;
c3.showData();
break;
}
case 2:
       {
cout << "Enter the real part of the complex number : ";
cin >> real;
cout << "Enter the imaginary part of the complex number : ";
cin >> img;
complex c2(real,img);
c3 = c1 - c2;
c3.showData();
break;
       }
case 3:
{
cout << "Enter the real part of the complex number : ";
cin >> real;
cout << "Enter the imaginary part of the complex number : ";
cin >> img;
complex c2(real,img);
c3 = -c2;
c3.showData();
break;
}
case 4:
{
cout << "Enter the real part of the complex number : ";
cin >> real;
cout << "Enter the imaginary part of the complex number : ";
cin >> img;
complex c2(real,img);
c3 = c1 * c2;
c3.showData();
break;
}
case 5:
cout << "PROGRAMS TERMINATES " << endl;
break;
default:
cout << "ENTER LEGAL VALUES " << endl;
}
}while(decision != 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