/* calculator12.cpp implements a simple 8-function calculator.
 *
 * Input:  operand1, theOperator, operand2
 * Output: the result of applying theOperator to operand1 and 
operand2.
 
*********************************************************************/

#include <iostream.h>      	   // cin, cout, <<, >>
#include <string>                // string

double OperationResult(char theOperator, double operand1, double 
operand2);
int Factorial(int n);

int main()
{
   const string MENU = "Enter:\n"
                       "  + for the addition operation\n"
                       "  - for the subtraction operation\n"
                       "  * for the multiplication operation\n"
                       "  / for the division operation\n"
                       "  ^ for the exponentiation operation\n"
                       "  l for the base-10 logarithm operation\n"
                       "  ! for the factorial operation and\n"
                       "  q to quit.\n"
                       "--> ";

   cout << "Welcome to the 8-function calculator!\n\n";

   double operand1,
          operand2,
          result;
   char theOperator;

   for (;;)
   {
      cout << MENU;
      cin >> theOperator;

      if (theOperator == 'q') break;

      cout << "Enter the first operand: ";
      cin >> operand1;

      if (theOperator == '+' || theOperator == '-' ||
          theOperator == '*' || theOperator == '/' ||
          theOperator == '^')
      {
         cout << "Enter the second operand: ";
         cin >> operand2;
      }

      result = OperationResult(theOperator, operand1, operand2);

      cout << "The result is " << result << endl << endl;
   }

   return 0;
}

/*** Insert the #include directives and the definitions of functions
     Factorial() from Figures 3.8 and OperationResult() from
     Figure 3.11 here.  ***/

/*** Insert the #include directive and the definition of
     Factorial() from Figure 3.8 here. ***/

/* Factorial computes the factorial of a nonnegative integer. 
 *     
 * Receive:      n, an integer
 * Precondition: n is nonnegative
 * Return:       n!
 *************************************************************/

#include <cassert>             // assert()

int Factorial(int n)
{
   assert(n >= 0);

   int product = 1;

   for (int count = 2; count <= n; count++)
      product *= count;

   return product;
}

/* OperationResult applies theOperator to operand1 and operand2.
 *
 * Receive:      theOperator, a character
 *               operand1 and operand2, two doubles
 * Precondition: theOperator is one of +, -, *, /, ^, l, !
 * Return:       the result of applying theOperator to operand1 
 *                 and operand2
 
**********************************************************************
**/

#include <math.h>           // pow(), log10()

double OperationResult(char theOperator, double operand1, double 
operand2)
{
   if (theOperator == '+')
      return operand1 + operand2;
   else if (theOperator == '-')
      return operand1 - operand2;
   else if (theOperator == '*')
      return operand1 * operand2;
   else if (theOperator == '/')
      if (operand2 != 0)
         return operand1 / operand2;
      else
         cerr << "Operation Result: division by 0 -- result 
undefined!\n";
   else if (theOperator == '^')
      return pow(operand1, (int)operand2);
   else if (theOperator == 'l')
      return log10(operand1);
   else if (theOperator == '!')
      return (double)Factorial((int)operand1);
   else
   {
      cerr << "OperationResult: invalid operator "
           << theOperator << " received!\n";
      return 0.0;
   }
}


