Answer to Question #272497 in C++ for Pica

Question #272497

Create a class named Complex which contains:

  • data field named real of type double
  • data field named imag of type double
  • no-arg constructor that initializes real & imag to 0
  • parametrized constructor with specified values for real & imag
  • overloaded stream extraction operator << that will display a complex number in the form a + bi
  • overloaded stream insertion operator >> that will ask the user for real and imag part of the complex number
  • overloaded binary + operator and a binary – operator
  • overloaded binary * operator and / operator
  • function mag() that will return the magnitude of the complex number.
  • function conjuage() that returns the conjugate of a complex number

Sample:

Enter the first complex number 

Enter real part: 3

Enter imag part: 4

Enter the second complex number

Enter real part: 4

Enter imag part: -6

You entered the numbers

C1 = 3 + 4i

C2 = 4 – 6i

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Magnitude

6: conjugate

9: Quit

Enter your selection: 1

C1 + C2 = 7 - 2i


1
Expert's answer
2021-11-28T05:25:53-0500
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
class Complex{
    public:
    double real, img;


    Complex(){
        this->real = 0;
        this->img = 0;
    }
    Complex(int real, int img){
        this->real = real;
        this->img = img;
    }
    friend ostream& operator<<(ostream&, const Complex);
    friend istream& operator>>(istream&, Complex&);
    friend Complex operator+(const Complex&, const Complex&);
    friend Complex operator-(const Complex&, const Complex&);
    friend Complex operator*(const Complex&, const Complex&);
    friend Complex operator/(const Complex&, const Complex&);


    double mag();
    Complex conjugate();


    friend bool operator<(Complex&, Complex&);
    friend bool operator>(Complex&, Complex&);
    friend bool operator<=(Complex&, Complex&);
    friend bool operator>=(Complex&, Complex&);
    friend bool operator==(Complex&, Complex&);
    friend bool operator!=(Complex&, Complex&);
};
ostream& operator<<(ostream &stream, Complex a){
    stream << a.real;
    if(a.img < 0) stream<< " - ";
    else stream<<" + ";
    stream << abs(a.img) <<"i";
    return stream;
}
istream& operator>>(istream &stream, Complex& a){
    cout<<"Enter real part: ";
    stream>>a.real;
    cout<<"Enter imaginary part: ";
    stream>>a.img;


    return stream;
}
Complex operator+(const Complex& a, const Complex& b){
    return Complex(a.real + b.real, a.img + b.img);
}
Complex operator-(const Complex& a, const Complex& b){
    return Complex(a.real - b.real, a.img - b.img);
}
Complex operator*(const Complex& a, const Complex& b){
    return Complex(a.real * b.real + a.img * b.img, a.real * b.img + a.img * b.real);
}
Complex operator/(const Complex& a, const Complex& b){
    return Complex((a.real * b.real + a.img * b.img)/(b.real * b.real + b.img * b.img), (a.img * b.img - a.real * b.img)/(b.real * b.real + b.img * b.img));
}


double Complex:: mag(){
    return sqrt(this->real * this->real + this->img * this->img);
}


Complex Complex:: conjugate(){
    return Complex(this->real, -this->img);
}


bool operator<(Complex& a, Complex& b){
    return a.mag() < b.mag();
}
bool operator>(Complex& a, Complex& b){
    return a.mag() > b.mag();
}
bool operator<=(Complex& a, Complex& b){
    return a.mag() <= b.mag();
}
bool operator>=(Complex& a, Complex& b){
    return a.mag() >= b.mag();
}
bool operator==(Complex& a, Complex& b){
    return a.mag() == b.mag();
}
bool operator!=(Complex& a, Complex& b){
    return a.mag() != b.mag();
}


int main(){
    Complex C1, C2;
    cout<<"Enter the first complex number\n";
    cin>>C1;
    cout<<"Enter the second complex number\n";
    cin>>C2;
    cout<<"You entered the numbers\n";
    cout<<"C1 = "<<C1<<endl;
    cout<<"C2 = "<<C2<<endl;


    int choice;
    cout<<"1: Addition\n";
    cout<<"2: Subtraction\n";
    cout<<"3: Multiplication\n";
    cout<<"4: Division\n";
    cout<<"5: Magnitude\n";
    cout<<"6: conjugate\n";
    cout<<"9: Quit\n";


    do{
        cin>>choice;
        switch(choice){
            case 1: cout<<"C1 + C2 = "<<C1 + C2<<endl; break;
            case 2: cout<<"C1 - C2 = "<<C1 - C2<<endl; break;
            case 3: cout<<"C1 * C2 = "<<C1 * C2<<endl; break;
            case 4: cout<<"C1 / C2 = "<<C1 / C2<<endl; break;
            case 5: cout<<"Magnitude of C1 = "<<C1.mag()<<endl;
                    cout<<"Magnitude of C2 = "<<C2.mag()<<endl;
                    break;
            case 6: cout<<"Conjugate of C1 = "<<C1.conjugate()<<endl;
                    cout<<"Conjugate of C2 = "<<C2.conjugate()<<endl;
                    break;
            default: break;
        }
    }while(choice != 9);
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS