Monday, 17 October 2011

COMPLEX NUMBER MANIPULATION


PROGRAM:
//COMPLEX NUMBER MANIPULATION
#include<iostream.h>     
#include<math.h>                              //including header files
class complex                                                   //defining class
{
        public:
                int real,img;                                   //declaring data members
        public:
                void getdata()
                {
                        cout<<"\nEnter real part\t:";                //getting the real part
                        cin>>real;
                        cout<<"\nEnter imaginary part\t:";      //getting the imaginary part
                        cin>>img;
                }
                complex operator +(complex t1)                  //receiving object & returning object
                {
                        complex t3;                         //definition for add function
                        t3.real=t1.real+real;                 //storing the resulting real part in third object
                        t3.img=t1.img+img;                //storing the resulting imaginary part in object 3
                        return t3;                           
                }
                        complex operator -(complex t1)
                        {
                                    complex t3;
                                    t3.real=t1.real-real;                  //storing the resulting real part in third object
                                                                //storing the resulting imaginary part in object 3
                                    t3.img=t1.img-img;
                                    return t3;
                        }
                        complex operator *(complex t1)
                        {
                                    complex t3;
                                    t3.real=(t1.real)*(real);           //storing the resulting real part in third object
                        t3.img=t1.img+img;                  //storing the resulting imaginary part in object 3
                                    t3.img=(t1.img)*(img);
                                    t3.real=t3.real-t3.img;
                                    t3.img=((t1.img)*(real))+((t1.real)*(img));
                                    return t3;                                    //returning object
                        }
                        complex operator /(complex t1)
                        {
                                    int x,y,z;
                                    complex t3;                                 //creating a new object
                                    t1.img=0-t1.img;
                                    x=(t1.real)*(t1.real);                //calculating the denominator of the complex number
                                    y=(t1.img)*(t1.img);
                                    z=sqrt(x+y);
                                    t3.real=(t1.real)*(real);
                                    t3.img=(t1.img)*(img);
                                    t3.real=t3.real-t3.img;
                                    t3.img=((t1.img)*(real))+((t1.real)*(img));   //calculating the numerator of the complex number
                                    t3.real=t3.real/z;
                                    t3.img=t3.img/z;
                                    return t3;                              //returning object
                        }
                void display()
                {
                                    if(img>0)
                                    {
                        cout<<real<<" "<<"+"<<" "<<img<<"i";  
                                    }                                                //displaying the complex number
                                    else
                                    {
                                    cout<<real<<" "<<img<<"i"; 
                                    }
                }
};
int main()                                               //definition for main function
{
        complex c1,c2,c3;                                //creating three objects
        short int ch;
            system("clear");
        cout<<"<<<<< COMPLEX  NUMBER MANIPULATION >>>>>\n";
        cout<<"\nEnter 1st complex number\n";
        c1.getdata();                                   //calling getdata function for object1
        cout<<"\nEnter 2nd complex number";
        c2.getdata();                                   //calling getdata function for object2
        cout<<"\nThe two complex numbers are\n";
        c1.display();                                   //calling display function for object1
        cout<<"\n";
        c2.display();                                  //calling display function for object2
            do
            {
            cout<<"\n1.complex addition\n2.complex subtraction\n3.complex multiplication\n4.complex division\n5.exit";
            cin>>ch;
            switch (ch)
            {
            case 1:
            {
            c3=c1+c2;                                       //invoking operator + function
            cout<<"\nThe resulting complex number is \n";
            c3.display();
                        break;
            }
            case 2:
            {
                        c3=c1-c2;                                                        //invoking operator - function
                        cout<<"\nThe resulting complex number is \n";
                        c3.display();
                        break;
            }
            case 3:
            {
                        c3=c1*c2;                                                        //invoking operator * function
                        cout<<"\nThe resulting complex number is \n";
                        c3.display();
                        break;
            }
            case 4:
            {
                        c3=c1/c2;                                                         //invoking operator / function
                        cout<<"\nThe resulting complex number is \n";
                        c3.display();
                        break;
            }
            }
            }while(ch<5);
        return 0;
}

























OUTPUT:
<<<<< COMPLEX  NUMBER MANIPULATION >>>>>


Enter 1st complex number


Enter real part :2


Enter imaginary part    :5


Enter 2nd complex number
Enter real part :7


Enter imaginary part    :8


The two complex numbers are
2 + 5i
7 + 8i
1.complex addition
2.complex subtraction
3.complex multiplication
4.complex division
5.exit1


The resulting complex number is
9 + 13i
1.complex addition
2.complex subtraction
3.complex multiplication
4.complex division
5.exit2


The resulting complex number is
5 + 3i
1.complex addition
2.complex subtraction
3.complex multiplication
4.complex division
5.exit3


The resulting complex number is
-26 + 51i
1.complex addition
2.complex subtraction
3.complex multiplication
4.complex division
5.exit4


The resulting complex number is
5 + 1i
1.complex addition
2.complex subtraction
3.complex multiplication
4.complex division
5.exit

No comments:

Post a Comment