C++ programme to illustrate a calculator. Perform calculations on two operands using a class calculator. Calculator should add, subtract, multiply and divide operands



#include<iostream.h>
#include<conio.h>
#include<process.h>

class calculator
{
               float result;
               int o1,o2;
               public:
               void enter();
               void showresult();
               void add();
               void sub();
               void mul();
               void div();
               void clear();
};


void calculator::enter()
{
               cout<<"Enter a operant:";
               cin>>o1;
               cout<<"Enter the other operant:";
               cin>>o2;
}


void calculator::showresult()
{
               cout<<"The result of the operation is:"<<result<<endl;
}


void calculator::add()
{
               result=o2+o1;
}

void calculator::sub()
{
               result=o1-o1;
}


void calculator::mul()
{
               result=o1*o2;
}


void calculator::div()
{
               result=o1/o2;
}


void calculator::clear()
{
               result=0;
}



int main()
{
               char ch='y';
               calculator c1;
               while(ch=='y')
               {
                              c1.enter();
                              cout<<"Which operation do you want to perform:";
                              cin>>ch;
                              switch(ch)
                              {
                                             case '+':c1.add();
                                                            break;
                                             case '-':c1.sub();
                                                            break;
                                             case '*':c1.mul();
                                                            break;
                                             case '/':c1.div();
                                                            break;
                                             default :cout<<"Wrong choice:";
                                                            exit(0);
                              }
                              c1.showresult();
                              c1.clear();
                              cout<<"Do you want to continue:";
                              cin>>ch;
               }
               return 0;
}

OUTPUT:

 Enter a operand:4
Enter the other operand:8
Which operation do you want to perform:*
The result of the operation is:32
Do you want to continue:y
Enter an operand:4
Enter the other operand:8
Which operation do you want to perform:+                                       
The result of the operation is:12                                              
Do you want to continue:n

No comments:

Post a Comment