Sunday, 16 November 2014

Complex Arithmetic

/* Complex Numbers
* **Addition
* **Subtraction
* **Multiplication
* **Division
******* *Turbo C++ **16/11/2014 ****Hemanth
*/
#include<iostream.h>
#include<conio.h>
class Complex
{
             float x,y;
                     void addition(Complex &,Complex &);
                     void subtraction(Complex &,Complex &);
                     void multiplication(Complex &,Complex &);
                     void division(Complex ,Complex);
                     void display(Complex &);
Complex(int a)
{
                      //No Default Values
}
Complex()
{
                 //OverLoading Constructor To Take Values from User
                cout<<"Give REAL part of Complex Number\n"<<endl;
               cin>>x;
               cout<<"Give IMAGINARY part of Complex Number\n"<<endl;
               cin>>y;
}
};
void Complex::addition(Complex &a,Complex &b)
{
                x=(a.x+b.x);
               y=(a.y+b.y);
}
void Complex::subtraction(Complex &a,Complex &b)
{
                 x=(a.x-b.x);
                y=(a.y-b.y);
}
void Complex::multiplication(Complex &a,Complex &b)
{
               x=(a.x*b.x)-(a.y*b.y);
              y=(a.x*b.y)+(a.y*b.y);
}
void Complex::division(Complex a,Complex b)
{
                 b.y=(-1*b.y)                     //Taking Conjugate of num2 and performing multiplication
                Complex numerator(2);
                numerator.multiplication(a,b);
               x=numerator.x/((b.x*b.x)+(b.y*b.y));
               y=numerator.y/((b.x*b.x)+(b.y*b.y));
}
void Complex::display(Complex &result)
{
             cout<<result.x;
            if(result.y<0)
                    cout<<"-i"<<-1*result.y;
           else
                   cout<<"+i"<<result.y;
}
int main()
{
        clrscr();
        int option;
        Complex result(1);
        cout<<"\t\tArithmetic Operations On Complex Numbers\n\n";
        cout<<"Enter 1st Complex Number\n"<<endl;
       Complex num1;
       cout<<"Enter 2nd Complex Number\n"<<endl;
       Complex num2;
       do{
         clrscr();
        cout<<"\t\tNUM1: ";
        num1.display(num1);
        cout<<"\t\tNUM2: ";
        num2.display(num2);
        cout<<"\n\n\t1.Addition\n\n\t2.Subtraction\n\n\t3.Multiplication\n\n";
         cout<<"\t4.Division\n\n\t5.Exit\n\nEnter Your Choice:";
        cin>>option;
        switch(option)
        {
        case 1:
              result.addition(num1,num2);
              cout<<"\nAddition is ";
              result.display(result);
             break;
       case 2:
            result.subtraction(num1,num2);
            cout<<"\nSubtraction is ";
            result.display(result);
            break;
       case 3:
           result.multiplication(num1,num2);
          cout<<"\nMultiplication is ";
          result.display(result);
         break;
       case 4:
         result.division(num1,num2);
         cout<<"\nDivision is ";
         result.display(result);
        break;
    }
    getch();
  } while(option!=5);
return 0;
}
            

No comments:

Post a Comment