class complex numbers operations HW-

#include <iostream>

using namespace std;
class complex
{
    int real, imaginary;

public:
    void input();
    void display();
    complex add(complex);
    complex sub(complex);
    complex mult(complex);
    complex div(complex);
};
void complex::input()
{
    cout << "Enter real part: ";
    cin >> real;
    cout << "Enter the imaginary part: ";
    cin >> imaginary;
}
void complex::display()
{
    cout << "Complex number is: " << real << "+" << imaginary << "i" << endl;
}
complex complex::add(complex num)
{
    complex c;
    c.real = real + num.real;
    c.imaginary = imaginary + num.imaginary;
    return c;
}
complex complex::sub(complex num)
{
    complex c;
    c.real = real - num.real;
    c.imaginary = imaginary - num.imaginary;
    return c;
}
complex complex::mult(complex num)
{
    complex c;
    c.real = (real * num.real - imaginary * num.imaginary);
    c.imaginary = (real * num.imaginary + imaginary * num.real);
    return c;
}
int main()
{
    complex c1, c2, c3;
    // cout << "Enter the first complex number: ";
    // c1.input();
    // c1.display();
    // cout << "Enter the second complex number: ";
    // c2.input();
    // c2.display();
    // cout << "After addition: ";
    // c3 = c1.add(c2);
    // c3.display();
    // cout << "Enter the first complex number: ";
    // c1.input();
    // c1.display();
    // cout << "Enter the second complex number: ";
    // c2.input();
    // c2.display();
    // cout << "After subtraction: ";
    // c3 = c1.sub(c2);
    // c3.display();
    // cout << "Enter the first complex number: ";
    // c1.input();
    // c1.display();
    // cout << "Enter the second complex number: ";
    // c2.input();
    // c2.display();
    // cout << "After multiplication: ";
    // c3 = c1.mult(c2);
    // c3.display();
    char ch;
    while (1)
    {
        cout << "1.add(+) 2.sub(-) 3.mult(*) 4.div(/) 5.exit(x)\n";
        cout << "Enter the operation you want to perform:(+,-,*,/)\n";
        cin >> ch;
        switch (ch)
        {
        case '+':
            cout << "Enter the first input: ";
            c1.input();
            c1.display();
            cout << "Enter the second input: ";
            c2.input();
            c2.display();
            c3 = c1.add(c2);
            c3.display();
            break;
        case '-':
            cout << "Enter the first input: ";
            c1.input();
            c1.display();
            cout << "Enter the second input: ";
            c2.input();
            c2.display();
            c3 = c1.sub(c2);
            c3.display();
            break;
        case '*':
            cout << "Enter the first input: ";
            c1.input();
            c1.display();
            cout << "Enter the second input: ";
            c2.input();
            c2.display();
            c3 = c1.mult(c2);
            c3.display();
            break;
        // case '/':
        //     c1.input();
        //     c2.input();
        //     c3 = c1.div(c2);
        //     c3.display();
        //     break;
        case 'x':
            cout << "You have exited...";
            return 0;
        default:
            cout << "Wrong Input!!\nplease enter one of the given choices:\n";
            break;
        }
    }
    return 0;
}

Comments

Popular Posts