Saturday, 11 January 2014

A C++ program to design a calculator using class.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

class calc
{
            double newEntry;
            double displayedVal;
public:
double doDivideZero(double &)
{
            newEntry =0;
            displayedVal = 0.0;
           


            if (newEntry !=0)
            {
                        displayedVal = displayedVal / newEntry;
            } else cout << "Wrong Operation, Cannot Divide by Zero" << endl;
           
                       
                       
           
            return 0;          
}

};



Void  main()
{
           
           
            double displayedVal;
            double newEntry;
            char command_character ;

           
            displayedVal = 0.0;

            calc c;
            cout << "  Enter accepted Operator:" ;
            cin >> command_character;
            while (command_character != 'Q' || command_character != 'q')
            {
                        switch(command_character)
                        {
                        case 'c':
                        case 'C': displayedVal = 0.0;
                                                  break;
                        case '+': cout << "  Enter Number:";
                                                  cin >> newEntry;
                                                  displayedVal = displayedVal + newEntry;
                                                  break;
                        case '-': cout << "  Enter Number:";
                                                  cin >> newEntry;
                                                  displayedVal = displayedVal - newEntry;
                                                  break;
                        case '*': cout << "  Enter Number:";
                                                  cin >> newEntry;
                                                  displayedVal = displayedVal * newEntry;
                                                  break;
                        case '/': cout << "  Enter Number:";
                                          cin >> newEntry;
                                                  displayedVal = displayedVal / newEntry;
                                                  if (newEntry == 0)
                                                  {
                                                              c.doDivideZero(double &);
                                                  }
                                                 
                                                  break;
                        case '^': cout << "  Enter Number:";
                                                  cin >> newEntry;
                                                  displayedVal = pow (displayedVal,newEntry);
                                                  break;
                                                  default : cout << "  Unacceptable Operator(" << command_character << ")" << endl;
                        }
                        cout << "  The result so far is: " <<displayedVal<< endl;
                        cout << "  Enter Operator:";
                        cin >> command_character;
                       
                       
            }



Getch();

}

No comments:

Post a Comment