Wednesday, 8 January 2014

Program of Complex number using class

/* Program of Complex number using class */
#include<iostream>
using namespace std;

class complex
{
    int real,imaginary;
public:
    complex()
    {
        real=0;
        imaginary=0;
    }
    void read();
    void show();
    complex add(complex,complex);
    complex mul(complex,complex);
};

void complex::read()
{
    cout<<"\nReal part=";
    cin>>real;
    cout<<"\nImaginary part=";
    cin>>imaginary;
}

void complex::show()
{
    if(real==0)
    {
        if(imaginary==0)
            cout<<0;
        else
            cout<<imaginary<<"i";
    }
    else
    {
        cout<<real;
        if(imaginary>0)
            cout<<"+"<<imaginary<<"i";
        else if(imaginary<0)
            cout<<imaginary<<"i";
    }
    cout<<"\n";
}

complex complex::add(complex a,complex b)
{   
    real=a.real+b.real;
    imaginary=a.imaginary+b.imaginary;   
}

complex complex::mul(complex a,complex b)
{   
    real=a.real*b.real-a.imaginary*b.imaginary;
    imaginary=a.real*b.imaginary+b.real*a.imaginary;   
}
main()
{
    complex c1,c2,c3,c4,c5,c6;   
    c1.read();   
    c2.read();
    c3.add(c1,c2);
    c3.show();
    c4.mul(c1,c2);
    c4.show();
    return 0;
}

No comments:

Post a Comment