Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you help me Write a program, which will act as a simple four-function calcul

ID: 3788115 • Letter: C

Question

Can you help me Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator.

Explanation / Answer

main.cpp


#include <iostream>
#include "numberOnlyInput.h"
#include "mathFunctions.h"

using namespace std;

bool operatorValid(char [], char &);
double numberEntry();

int main ()
   {
   double firstNumber;
   double secondNumber;
   char firstOperator = 0;
   char validOperators [] = "+-*/XxCc"; /*{'+', '-', '*', '/', 'X','x', 'C', 'c'};*/
   double result;

   while (true)
       {
       firstNumber = numberEntry();
       while (true)
           {
           cout << "Press C to clear and start new, X to turn off calculator: " << endl;
           cout << "Enter either +, -, *, or / to continue." << endl;
           cin >> firstOperator;
           while (!operatorValid(validOperators , firstOperator))
               {
               cout << "The operator entered is not a valid operator." << endl;
               cout << "Press C to clear and start new, X to turn off calculator: " << endl;
               cout << "Enter either +, -, *, or / to continue." << endl;
               cin >> firstOperator;
               }
           if (firstOperator == 'X' || firstOperator == 'x')
               {
               return (1);
               }
           if (firstOperator == 'C' || firstOperator == 'c')
               {
               cout << endl;
               break;
               }
           secondNumber = numberEntry();

           switch (firstOperator)
               {
               case '+':
                   //result = firstNumber + secondNumber;                   
                   result = addition (firstNumber , secondNumber) ;
                   cout << "The result is: " << result << endl;
                   firstNumber = result;
                   break;
               case '-':
                   //result = firstNumber - secondNumber;
                   result = subtraction (firstNumber, secondNumber);
                   cout << "The result is: " << result << endl;
                   firstNumber = result;
                   break;
               case '*':
                   //result = firstNumber * secondNumber;
                   result = multiplication (firstNumber, secondNumber);
                   cout << "The result is: " << result << endl;
                   firstNumber = result;
                   break;
               case '/':
                   if (secondNumber == 0)
                       {
                       cout << "Cannot divide by zero." << endl;
                       break;
                       }
                  else
                       {
                       //result = firstNumber / secondNumber;
                       result = division (firstNumber, secondNumber);
                       cout << "The result is: " << result << endl;
                       firstNumber = result;
                       break;
                       }
                   /*
               default:
                   cout << "The operator entered is not a valid operator." << endl;
                   break;
                   */
               }
           }
       }
   return 0;
   }

bool operatorValid(char _validOperators [], char &referenceOperator )
   {
   for (int i = 0; _validOperators[i] != '' ; i++)
       {
       if (referenceOperator == _validOperators[i])
           {
           return(true);
           }
       }
   return (false);
   }

double numberEntry()
   {
   double number;
   cout << "Enter number:" << endl;
   //cin >> number;
   number = readNumber();
   return (number);
   }


numberOnlyInput.h


#ifndef NUMBERONLYINPUT_H
#define NUMBERONLYINPUT_H

double readNumber();

#endif

numberOnlyInput.cpp

//#include <conio.h>//reliance on this makes it a windows only function
#include <string.h>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>

#include "numberOnlyInput.h"

using namespace std;

double readNumber ()
   {
   char   c;
   bool   isNegative = false;
   long   wholePart = 0;
   long   numCharsEntered   = 0;

   //bool   pastDecimal = false;
   long   numPastDecimal = 0;
   long   fractionalPart = 0;
   long   numLeadingZeros = 0;
   double   completeNumber = 0;
   char   lastCharEntered;

   while ((c = cin.get ()) != ' ')
       {
       switch (c)
           {
           case '0'://if it is zero right after the decimal, the second number will be wrong
               if (numPastDecimal == 0)
                   {
                   wholePart = (wholePart * 10) + (c - '0');
                   numCharsEntered++;
                   lastCharEntered = c;
                   break;
                   }
               else
                   {
                   //will only occur if this is beyond decimal
                   if (numPastDecimal == 1 )
                       {
                       numLeadingZeros++;
                       }
                   else if (lastCharEntered == '0' && numLeadingZeros == (numPastDecimal - 1) )
                       {
                       numLeadingZeros++;
                       }
                   else
                       {
                       fractionalPart = (fractionalPart * 10) + (c - '0');
                       }
                   numPastDecimal ++;
                   numCharsEntered ++;
                   lastCharEntered = c;
                   break;
                   }
           case '1':
           case '2':
           case '3':
           case '4':
           case '5':
           case '6':
           case '7':
           case '8':
           case '9':
               if (numPastDecimal < 1)
                   {
                   wholePart = (wholePart * 10) + (c - '0');
                   numCharsEntered++;
                   lastCharEntered = c;
                   break;
                   }
               else
                   {
                   fractionalPart = (fractionalPart * 10) + (c - '0');
                   numPastDecimal ++;
                   numCharsEntered ++;
                   lastCharEntered = c;
                   break;
                   }
           case '.':
               if (numPastDecimal == 0 )
                   {
                   numPastDecimal++;
                   numCharsEntered++;
                   break;
                   }
               else
                   {
                   c = '';
                   break;
                   }
           case '':
               if (numCharsEntered > 0)
                       {
                       cout.put ('');
                       cout.put (' ');
                       if (numPastDecimal == 0)
                           {
                           wholePart = wholePart / 10;
                           //long, so last number is truncated
                           }
                       else
                           {
                           numPastDecimal --;
                           if (numPastDecimal == numLeadingZeros)
                               {
                               numLeadingZeros --;
                               }
                           fractionalPart = fractionalPart / 10;
                           }
                       numCharsEntered--;
                       if (numCharsEntered == 0)
                           {
                           isNegative = false;
                           }
                       }
               else
                   {  
                   c = '';
                   }
               break;
           case '-':
               if (numCharsEntered == 0)
                   {
                   isNegative = true;
                   numCharsEntered++;
                   }
               else
                   {
                   c = '';
                   }
               break;
           case '+':
               if (numCharsEntered == 0)
                   {
                   numCharsEntered++;
                   }
               else
                   {
                   c = '';
                   }
               break;
           default:
               c = '';
           }
       cout.put (c);
       }
   cout.put (' ');
   if (isNegative)
           wholePart = -wholePart;
   if (numPastDecimal == 0 )
       {
       return wholePart;
       }
   else //put them together
       {
       string completeNumberString;
       stringstream ss1;
       ss1 << wholePart << '.' ;
       if (numLeadingZeros > 0)
           {
           for (int i = 0; i < numLeadingZeros; i++)
               {
               ss1 << '0';
               }
           }
       ss1 << fractionalPart ;
       completeNumberString = ss1.str();
       //cout << completeNumberString << endl;
       char * pEnd; //needed for strtod
       completeNumber = strtod (completeNumberString.c_str(), &pEnd);
       return completeNumber;
       }
   }

   mathFunctions.cpp
  
   double addition(double a , double b)
   {
   double answer;
   answer = a + b;
   return (answer);
   }

double subtraction(double a , double b)
   {
   double answer = a - b;
   return (answer);
   }

double multiplication(double a , double b)
   {
   double answer = a * b;
   return (answer);
   }

double division(double a , double b)
   {
   double answer = a / b;
   return (answer);
   }
  
  
  
mathFunctions.h

#ifndef MATHFUNCTIONS_H
#define MATHFUNCTIONS_H

double addition( double , double);
double subtraction( double, double);
double multiplication(double , double);
double division(double , double);

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote