Pages

Write a program to add and subtract complex equations.

Wednesday, 8 May 2013

#include <iostream.h>
class complex //create class of name complex
{
      double real1, imaginary1, real2, imaginary2, sreal, simaginary; //declare attributes
      public:
      complex() //constructor
      {
               real1=0;
               imaginary1=0;
               real2=0;
               imaginary2=0;
               sreal=0;
               simaginary=0;
      }
      //create functions
      void setvalues() //function to set values of equations
      {
           cout<<"Enter real number of first eqaution: ";
           cin>>real1;
           cout<<"Enter imaginary number of first eqaution: ";
           cin>>imaginary1;
           cout<<"Enter real number of 2nd eqaution: ";
           cin>>real2;
           cout<<"Enter imaginary number of 2nd eqaution: ";
           cin>>imaginary2;
      }
      void add()//function to add two complex equations
      {
           sreal=real1+real2;
           simaginary=imaginary1+imaginary2;
      }
      void sub()//function to subtract two complex equations
      {
           sreal=real1-real2;
           simaginary=imaginary1-imaginary2;
      }
      void answer() //dispaly answer on the screen
      {
           cout<<"{("<<sreal<<")"<<"+"<<"("<<simaginary<<")i}"<<endl;
      }
};
main()
{
      again: // lable for calculation again
      complex a; //create objects
      int b;
      char ask;
      a.setvalues(); //call function to set values of funtions.
      cout<<"Press 1 to add\nPress 2 to subtract\n";
      cin>>b;
      //ask from user wheather equations add or subtract.
      if(b==1)
      {
              a.add();
      }
      else if(b==2)
      {
           a.sub();
      }
      else
      {
          cout<<"Enter valid number.";
          goto again;
      }
      a.answer(); //call function to display or subtract
      //ask from user that he want to calculate again?
      cout<<"You want to claculate again? Y/N ";
      cin>>ask;
      if(ask=='Y' || ask=='y')
      {
           goto again;
      }
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *