Friday, 7 December 2012

Complex number addition cpp program


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class complex
{ private:
          double real,image;
  public:
          complex();
          complex(double,double);
          void getcomplex();
          void dispcomplex();
          friend complex addcomplex(complex,complex);
};
complex::complex()
{        real=0.0;
          image=0.0;
}

complex::complex(double x,double y)
{        real=x;
          image=y;

}
void complex::getcomplex()
{        cout<<" Real Part : ";
          cin>>real;
          cout<<" Imaginary Part : ";
          cin>>image;
}
void complex::dispcomplex()
{        cout<<" ("<<real<<" + "<<image<<" i)";
}

complex addcomplex(complex x,complex y)
{        complex temp;
           temp.real=x.real+y.real;
          temp.image=x.image+y.image;
          return temp;
}
void main()
{        int c;
          complex c1,c2,c3;
          while(1)
          {        clrscr();
cout<<" Perform the following operations on Complex Numbers .\n ";
                   cout<<" -----------------------------------------------------\n ";
                   cout<<" 1.Enter two complex numbers.\n ";
                   cout<<" 2.Display the complex numbers.\n ";
                   cout<<" 3.Add complex numbers.\n ";
                   cout<<" 4.Exit from program.\n ";
                   do
                   {        cout<<" Select your choice(1:4) : \n";
                             cin>>c;
                   }
                   while((c<0)||(c>4));
          clrscr();
          switch(c)
          { case 1 :
cout<<" Enter the first complex number\n";
                    c1.getcomplex();
                    cout<<" Enter the second complex number\n";
                    c2.getcomplex();
                 break;
      case 2 :
cout<<" Complex numbers you entered are \n";
                    c1.dispcomplex();
                    cout<<" and ";
                    c2.dispcomplex();
                    break;
      case 3 :
cout<<" Sum of ";
                    c1.dispcomplex();
                    cout<<" and ";
                    c2.dispcomplex();
                    cout<<" is ";
                    c3=addcomplex(c1,c2);
                    c3.dispcomplex();
                    break;
          case 4 :
exit(1);break;
          }
          cout<<endl<<" Press any key to continue....";
          getch();
     }
}

No comments:

Post a Comment