September 4, 2012 / IST / Language.

 

/*Q2-> WAP in C++ to process Complex Numbers.It Has To perform Addition,
 Subtraction and Multiplication of Complex Numbers.Print the Result in X+iY
 Form*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
class complex
{
  private:                             //private members
	  int rlprt1,rlprt2,rrlprt;
	  int imprt1,imprt2,rimprt;
  public:
	complex()                    //constructor
	{rlprt1=rlprt2=rrlprt=0;
	 imprt1=imprt2=rimprt=0;
	}
	void getcmplx();
	void addcmplx();
	void subcmplx();             //function declarations
	void procmplx();
	void display();
	~complex(){}                //destructor
};

void complex::getcmplx()             //fn to get complex no.
{
  cout<<"\n\nFIRST COMPLEX NUMBER:";
  cout<<"\n---------------------";
  cout<<"\n\nEnter the Real Part:";
  cin>>rlprt1;
  cout<<"\nEnter the Imaginary Part:";
  cin>>imprt1;

  cout<<"\n\nSECOND COMPLEX NUMBER:";
  cout<<"\n---------------------";
  cout<<"\n\nEnter the Real Part:";
  cin>>rlprt2;
  cout<<"\nEnter the Imaginary Part:";
  cin>>imprt2;
}
void complex::addcmplx()             //fn to add complex no.
{
  rrlprt=rlprt1+rlprt2;
  rimprt=imprt1+imprt2;
}
void complex::subcmplx()            //fn to subtract complex no.
{
  rrlprt=rlprt1-rlprt2;
  rimprt=imprt1-imprt2;
}
void complex::procmplx()          //fn to multiply complex no.
{
  rrlprt=(rlprt1*rlprt2)+(-(imprt1*imprt2));
  rimprt=(rlprt1*imprt2)+(imprt1*rlprt2);
}
void complex::display()          //fn to display complex no.
{  if(rimprt<0)
      cout<<"\nResult of the Operation is "<<rrlprt<<"-i"<<abs(rimprt);
   else
      cout<<"\nResult of the Operation is "<<rrlprt<<"+i"<<rimprt;
}

void main()
{
  complex ob;                    //object declaration
  int ch;
  char choice;
  do
  { clrscr();
    cout<<"\n\n\t\tC O M P L E X  N U M B E R  O P E R A T I O N S";
    cout<<"\n\t\t-----------------------------------------------";
    cout<<"\n\n1.ADDITION";
    cout<<"\n2.SUBTRACTION";
    cout<<"\n3.MULTIPLICATION";              //menu
    cout<<"\n4.EXIT";
     cout<<"\nEnter Your Choice:";
     cin>>ch;
       switch(ch)
	  {
	     case 1: ob.getcmplx();
		     ob.addcmplx();
		     ob.display();
		     break;
					     //calling of the functions
	     case 2: ob.getcmplx();
		     ob.subcmplx();
		     ob.display();
		     break;

	     case 3: ob.getcmplx();
		     ob.procmplx();
		     ob.display();
		     break;

	     case 4: exit(0);
	     default:cout<<"\nPlease enter a Valid Choice";
	  }

    cout<<"\nDo You Want To Continue(Y/N):";
    cin>>choice;
  }while(choice=='Y' || choice=='y');
}
	       //end of program