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

Create the following functions a. A default Constructor that sets the complex nu

ID: 3677627 • Letter: C

Question

Create the following functions
a. A default Constructor that sets the complex number to 0
b. Constructor that takes a single double for the real part of complex number and sets the Imaginary=0
c. Constructor that takes two doubles (real and imaginary parts of a complex number).
d. getReal() - returns real part of complex number as a double.
e. getImaginary() - returns imaginary part of number as a double.
f. getMagnitude() - returns the magnitude of the polar form of the number
g. getAngle() - returns the angle of the polar form of the number
h. setReal() - sets the real part of the complex number
i. setImaginary() - sets the imaginary part of the complex number
j. setComplex() - sets both real and imaginary parts of a complex number
k. Create individual multiplication, division, addition, and subtraction functions. They all take a myComplex object and return a myComplex object.
l. Operators: +, -, *, /
m. conjugate() function which returns the conjugate of the current object.
n. Equality operators: == and !=. Two complex numbers are equivalent if their real/imaginary parts differ by at most 0.001.
o. Overloaded << operator so Complex objects print like:
(5.3 + 2.1i)
p. Overloaded >> operator so you can enter a complex number from the keyboard.

Explanation / Answer

complex.cpp

#include "complex.h"

//implement constructors
Complex::Complex() { //this constructor sets all data members to zero or false
    real = 0;
    imaginary = 0;
    magnitude = 0;
    angle = 0;
    NaN = 0;
}

Complex::Complex(double r, double i) { //this constructor takes in a real and imaginary component
    real = r;
    imaginary = i;
    polar(); //generates corresponding polar representation
    NaN = 0;
}

Complex::Complex(double r) { //this constructor takes in a real component
    real = r;
    imaginary = 0; //assumed to be zero
    polar(); //generates corresponding polar representation
    NaN = 0;
}

//implement getters
double Complex::getReal() {
    return real;
}
double Complex::getImaginary() {
    return imaginary;
}
double Complex::getMagnitude() {
    return magnitude;
}
double Complex::getAngle() {
    return angle;
}
bool Complex::getNaN() {
    return NaN;
}

//implement setters
void Complex::setReal(double r) {
    real = r;
}

void Complex::setImaginary(double i) {
    imaginary = i;
}
void Complex::setMagnitude(double m) {
    magnitude = m;
}
void Complex::setAngle(double a) {
    angle = a;
}
void Complex::setNaN(bool n) {
    NaN = n;
}

//implement other methods
void Complex::setComplex(double a, double b) { //this is like we discussed in class
    setReal(a);
    setImaginary(b);
    polar();
}

void Complex::displayRect() { //prints number in rectangular format– see output sample for format
    std::cout << std::setprecision(2) << std::fixed;
  
    std::cout.width(15);
    if (getNaN()) {
        std::cout << "NaN ";
    } else if(getImaginary()==0) {
        std::cout << getReal() << endl;
    } else {
        std::cout << getReal() << " + " << getImaginary() << "j" << endl;
    }
}

void Complex::displayPolar() { //prints number in polar format– see output sample for format
    std::cout << std::setprecision(2) << std::fixed;
  
    std::cout.width(15);
    if (getNaN()) {
        std::cout << "NaN ";
    } else {
        std::cout << getMagnitude() << " < " << getAngle() << endl;
    }
}

void Complex::polar() { //this fills in the mag and angle data members based on rectangular values
    //Set magnitude as following: mag = sqrt(real^2 + imag^2)
    double realTemp = getReal();
    double imagTemp = getImaginary();
    double temp = pow(realTemp, 2) + pow(imagTemp, 2);
    setMagnitude(sqrt(temp));
  
    if (realTemp > 0 && imagTemp > 0) { //normal calculation here
        setAngle(atan(imagTemp/realTemp));
    } else if (realTemp < 0 && imagTemp > 0) { //need to add pi to answer
        setAngle(atan(imagTemp/realTemp)+M_PI);
    } else if (realTemp < 0 && imagTemp < 0) { //need to subtract pi from answer
        setAngle(atan(imagTemp/realTemp)-M_PI);
    } else if (realTemp > 0 && imagTemp < 0) { //normal calculation here
        setAngle(atan(imagTemp/realTemp));
    } else { //realTemp == 0, cannot divide by 0
        setNaN(1);
    }
}

void Complex::rect() { //this fills in the real and imag data members based on polar values
    setReal(getMagnitude() * cos(getAngle())); //real = mag * cos(ang)
    setImaginary(getMagnitude() * sin(getAngle())); //imag = mag * sin(ang)
}

void Complex::conj(){ //takes the conjugate of the number
    double temp = getImaginary();
    setImaginary(-1*temp);
    polar();
}

//specify overriding functions
Complex Complex::operator+(Complex RHS) {
    Complex sum;
    sum.setReal(getReal() + RHS.getReal()); //add
    sum.setImaginary(getImaginary() + RHS.getImaginary()); //add
  
    sum.polar(); //make sure the result is in polar format too
  
    return(sum);
}

Complex Complex::operator-(Complex RHS) {
    Complex subtract;
    subtract.setReal(getReal() - RHS.getReal()); //subtract
    subtract.setImaginary(getImaginary() - RHS.getImaginary()); //subtract
  
    subtract.polar(); //make sure the result is in polar format too
  
    return(subtract);
}

Complex Complex::operator*(Complex RHS) {
    Complex multiply;
    double r1 = getReal();
    double r2 = RHS.getReal();
    double i1 = getImaginary();
    double i2 = RHS.getImaginary();
  
  
    multiply.setReal(r1*r2 - i1*i2);
    multiply.setImaginary(r1*i2 + r2*i1);
  
    multiply.polar(); //make sure the result is in polar format too
  
    return(multiply);
}

Complex Complex::operator/(Complex RHS) {
    Complex divide;
    double r1 = getReal();
    double r2 = RHS.getReal();
    double i1 = getImaginary();
    double i2 = RHS.getImaginary();
    double tempNum;
    double tempDenom;
  
    if (RHS.getReal() > 0 || RHS.getImaginary() > 0) { //check for division by 0, if any is above 0, fine
        tempNum = (r1*r2 + i1*i2); //numerator used for real
        tempDenom = (r2*r2 + i2*i2); //denominator used for both real and imag
      
        divide.setReal(tempNum / tempDenom); //divide
      
        tempNum = (r2*i1 - r1*i2); //recalculate numerator used for imag
        divide.setImaginary(tempNum / tempDenom); //divide
  
        divide.polar(); //make sure the result is in polar format too
    } else {
        divide.setNaN(1); //set NaN to true if division by 0
    }
  
    return(divide);
}

complex.h

#ifndef __ECE2036__complex__
#define __ECE2036__complex__

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

class Complex {
private:
    //define instance variables
    double real;
    double imaginary;
    double magnitude;
    double angle;
    bool NaN;
  
public:
    //define constructors
    Complex();
    Complex(double, double);
    Complex(double);
  
    //define getters
    double getReal();
    double getImaginary();
    double getMagnitude();
    double getAngle();
    bool getNaN();
  
    //define and implement setters
    void setReal(double);
    void setImaginary(double);
    void setMagnitude(double);
    void setAngle(double);
    void setNaN(bool);
  
    //define other methods
    void setComplex(double, double); //this is like we discussed in class
    void displayRect(); //prints number in rectangular format– see output sample for format
    void displayPolar(); //prints number in polar format– see output sample for format
    void polar(); //this fills in the mag and angle data members based on rectangular values
    void rect(); //this fills in the real and imag data members based on polar values
    void conj(); //takes the conjugate of the number
  
    //specify overriding functions
    Complex operator+(Complex);
    Complex operator-(Complex);
    Complex operator*(Complex);
    Complex operator/(Complex);
  
};
#endif /* defined(__ECE2036__complex__) */


main.cpp


#include <stdio.h>

#include <iostream>
#include <math.h>
#include "complex.h"

using namespace std;

int main()
{
  
    //first let's declare an array of complex numbers
    Complex a[5];
    Complex result[5];
    Complex test1(4.5,6.5);
    Complex test2(8.0);
    int i;
  
    cout << "012345678901234567890123456789012345678901234567890" <<endl;
    cout << "Testing test1 : " ;
    test1.displayPolar();
    cout << endl;
  
    cout << "Testing test2 : " ;
    test2.displayRect();
    cout << endl;
  
    //I will fill these with data
    a[0].setComplex(3,4);
    a[1].setComplex(-1,3);
    a[2].setComplex(-1.23,-9.83);
    a[3].setComplex(3.14,-98.3);
    a[4].setComplex(2.71,1.61);
  
    //Now print the data in rectangular format
    cout << " Print Array of Complex Numbers in Rectangular Format" << endl;
    for (i = 0; i < 5; i++)
    {
        a[i].displayRect();
    }
  
    //Now print the data in polar format
    cout << " Print Array of Complex Numbers in Polar Format" << endl;
    for (i = 0; i < 5; i++)
    {
        a[i].displayPolar();
    }
  
  
    //Now test the add function
    cout << " Testing add operator a[0] + a[1]" << endl;
    result[0] = a[0] + a[1];
    result[0].displayRect();
  
    //Now test the sub function
    cout << " Testing subtract operator a[1] - a[2]" << endl;
    result[1] = a[1] - a[2];
    result[1].displayRect();
  
  
    //Now test the multiply function
    cout << " Testing multiply operator a[2] * a[3]" << endl;
    result[2] = a[2] * a[3];
    result[2].displayRect();
  
    //Now test the divide function
    cout << " Testing divide operator a[3] / a[4]" << endl;
    result[3] = a[3] / a[4];
    result[3].displayRect();
  
    //Now test the divide by zero
    cout << " Testing divide by zero a[4] / (0)" << endl;
    result[4] = a[4] / Complex(0,0);
    result[4].displayRect();
  
    //Now take the conjugate of the result array
    cout << " Now display the conjugate of the results array" << endl;
    for (i = 0; i < 5 ; i++)
    {
        result[i].conj();
        result[i].displayRect();
    }
  
    //Now display the results array in polar format
    cout << " Now display the results array in polar format" << endl;
    for (i = 0; i < 5 ; i++)
    {
        result[i].displayPolar();
    }
  
}//end main

sample output

012345678901234567890123456789012345678901234567890                                                                                                         
Testing test1 :            7.91 < 0.97                                                                                                                      
                                                                                                                                                            
Testing test2 :            8.00                                                                                                                             
                                                                                                                                                            
                                                                                                                                                            
Print Array of Complex Numbers in Rectangular Format                                                                                                       
           3.00 + 4.00j                                                                                                                                     
          -1.00 + 3.00j                                                                                                                                     
          -1.23 + -9.83j                                                                                                                                    
           3.14 + -98.30j                                                                                                                                   
           2.71 + 1.61j                                                                                                                                     
Testing test1 :            7.91 < 0.97                                                                                                                      
                                                                                                                                                            
Testing test2 :            8.00                                                                                                                             
                                                                                                                                                            
                                                                                                                                                            
Print Array of Complex Numbers in Rectangular Format                                                                                                       
           3.00 + 4.00j                                                                                                                                     
          -1.00 + 3.00j                                                                                                                                     
          -1.23 + -9.83j                                                                                                                                    
           3.14 + -98.30j                                                                                                                                   
           2.71 + 1.61j                                                                                                                                     
                                                                                                                                                            
Print Array of Complex Numbers in Polar Format                                                                                                             
           5.00 < 0.93                                                                                                                                      
           3.16 < 1.89                                                                                                                                      
           9.91 < -1.70                                                                                                                                     
          98.35 < -1.54                                                                                                                                     
           3.15 < 0.54                                                                                                                                      
                                                                                                                                                            
Testing add operator a[0] + a[1]                                                                                                                           
           2.00 + 7.00j                                                                                                                                     
                                                                                                                                                            
Testing subtract operator a[1] - a[2]                                                                                                                      
           0.23 + 12.83j                                                                                                                                    
                                                                                                                                                            
Testing multiply operator a[2] * a[3]                                                                                                                      
        -970.15 + 90.04j                                                                                                                                    
                                                                                                                                                            

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