Answer to Question #252398 in C++ for khangphanana

Question #252398

Construct a Complex structure to represent a complex number with both real and imaginary components being real numbers. Request:


Write functions that calculate the sum, difference, product, and quotient of two complex numbers

Write a function that calculates the sum and difference of two complex numbers simultaneously.

Write a function that calculates the modulus of a complex number

Write a function that displays a complex number in the correct format (a+bj where a is the real component, b is the imaginary component)


1
Expert's answer
2021-10-17T01:45:19-0400
i)
// Complex numbers are entered by the user
//Write a function that displays a complex number in the correct format (a+bj where a is the real component, b is the imaginary component)

#include <iostream>
using namespace std;


typedef struct complex {
    float real;
    float imag;
} complexNumber;


complexNumber addComplexNumbers(complex, complex);


int main() {
    complexNumber num1, num2, complexSum;
    char signOfImag;


    cout << "For 1st complex number," << endl;
    cout << "Enter real and imaginary parts respectively:" << endl;
    cin >> num1.real >> num1.imag;


    cout << endl
         << "For 2nd complex number," << endl;
    cout << "Enter real and imaginary parts respectively:" << endl;
    cin >> num2.real >> num2.imag;


    // Call add function and store result in complexSum
    complexSum = addComplexNumbers(num1, num2);


    // Use Ternary Operator to check the sign of the imaginary number
    signOfImag = (complexSum.imag > 0) ? '+' : '-';


    // Use Ternary Operator to adjust the sign of the imaginary number
    complexSum.imag = (complexSum.imag > 0) ? complexSum.imag : -complexSum.imag;


    cout << "Sum = " << complexSum.real << signOfImag << complexSum.imag << "i";


    return 0;
}


complexNumber addComplexNumbers(complex num1, complex num2) {
    complex temp;
    temp.real = num1.real + num2.real;
    temp.imag = num1.imag + num2.imag;
    return (temp);
}

ii)
//Write functions that calculate the sum, difference, product, and quotient of two complex numbers
#include <iostream>
using namespace std;


class ComplexNumber {


    public:
        double real;
        double imaginary;


        void add(ComplexNumber a, ComplexNumber b) {


            //Just add real- and imaginary-parts
            double real = a.real + b.real;
            double imaginary = a.imaginary + b.imaginary;
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout << "a + b = " << c.real << " + (" << c.imaginary << ") * i" << endl;
        }


        void sub(ComplexNumber a, ComplexNumber b) {


            //Just subtract real- and imaginary-parts
            double real = a.real - b.real;
            double imaginary = a.imaginary - b.imaginary;
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout << "a - b = " << c.real << " + (" << c.imaginary << ") * i" << endl;
        }


        void multiply(ComplexNumber a, ComplexNumber b) {


            //Use binomial theorem to find formula to multiply complex numbers
            double real = a.real * b.real - a.imaginary * b.imaginary;
            double imaginary = a.imaginary * b.real + a.real * b.imaginary;
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout << "a * b = " << c.real << " + (" << c.imaginary << ") * i" << endl;
        }




        void divide(ComplexNumber a, ComplexNumber b) {


            //Again binomial theorem
            double real = (a.real * b.real + a.imaginary * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);
            double imaginary = (a.imaginary * b.real - a.real * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout << "a : b = " << c.real << " + (" << c.imaginary << ") * i" << endl;
        }


    /*
     * Constructor to create complex numbers
     */
    ComplexNumber(double real, double imaginary) {
        this->real = real;
        this->imaginary = imaginary;
    }
};


int main() {


    /*
     * Variables for the real- and imaginary-parts of
     * two complex numbers
     */
    double realA;
    double imaginaryA;
    double realB;
    double imaginaryB;


    /*
     * User input
     */
    cout << "enter real(A), imag(A), real(B) and imag(B) >> ";
    cin >> realA >> imaginaryA >> realB >> imaginaryB;
    cout << endl;


    /*
     * Creation of two objects of the type "ComplexNumber"
     */
    ComplexNumber a = ComplexNumber(realA, imaginaryA);
    ComplexNumber b = ComplexNumber(realB, imaginaryB);


    /*
     * Calling the functions to add, subtract, multiply and
     * divide the two complex numbers.
     */
    a.add(a, b);
    a.sub(a, b);
    a.multiply(a, b);
    a.divide(a, b);
    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
APPROVED BY CLIENTS