/*
File name: hw4.cpp
Author: Bo Bayles
E-mail address: bmb3h6@umr.edu
Description: This program gives is a pay-per-use calculator. It
  presents the user with a choice of functions, performs those
  functions, then presents a bill.
*/

#include <iostream>
using namespace std;
//Include iostream and use standard namespace for cin and cout.

int main()
{
  //Begin program execution.

  const float cAdd = 0.25, cSubtract = 0.3, cMultiply = 0.5, cDivide = 0.8;
  //Declare and initialize constants for the prices.
  
  char cChoice;
  //Declare a char for the user choice.
  
  float fCost = 0, fOp1, fOp2, fExp = 1, fNumber = 0;
  //Declare floats for the total cost, user operands, the result of
  //the exponent calculations, and the number of calculations.
  //Also initialize those that aren't initialized by the user.

  do
  //Start a do-while loop so the loop executes at least once.
  
  {
    cout << "1. (A)dd" << endl
         << "2. (S)ubtract" << endl
         << "3. (M)ultiply" << endl
         << "4. (D)ivide" << endl
         << "5. (E)xponent" << endl
         << "6. (B)ill me" << endl
         << "7. (Q)uit" << endl;

	cout << "\n Please enter your choice for operations and I will bill "
	  << "you when you're ready." << endl;
      cin >> cChoice;
      //Output the menu and prompt the user for the choice.

    switch (cChoice)
    //start the switch statement using cChoice to decide what to do.
    {
	  case '1':
	  case 'A':
	  case 'a':
	          cout << "Addition: Enter the first operand: " << endl;
	          cin >> fOp1;

	          cout << "Enter the second operand: " << endl;
	          cin >> fOp2;

	          cout << fOp1 << " + " << fOp2 << " = " << (fOp1 + fOp2)
	               << endl;

	          fCost = fCost + cAdd;
	          fNumber++;
	          break;
      //See if the choice was 1, A, or a, and if so, get the two operands,
      //then output the calculation and its result. Then add the price
      //of an addition calculation to the total cost, then increment
      //the number of calculations.
      //The rest of the cases follow this pattern.
      
	  case '2':
	  case 'S':
	  case 's':
              cout << "Subtraction: Enter the first operand: " << endl;
	          cin >> fOp1;

	          cout << "Enter the second operand: " << endl;
	          cin >> fOp2;

	          cout << fOp1 << " - " << fOp2 << " = " << (fOp1 - fOp2)
	               << endl;

	          fCost = fCost + cSubtract;
	          fNumber++;

	          break;

	  case '3':
	  case 'M':
	  case 'm':
              cout << "Multiplication: Enter the first operand: " << endl;
	          cin >> fOp1;

	          cout << "Enter the second operand: " << endl;
	          cin >> fOp2;

	          cout << fOp1 << " * " << fOp2 << " = " << (fOp1 * fOp2)
                   << endl;

	          fCost = fCost + cMultiply;
	          fNumber++;

	          break;

	  case '4':
	  case 'D':
	  case 'd':
              cout << "Division: Enter the first operand: " << endl;
	          cin >> fOp1;

              cout << "Enter the second operand (not 0): " << endl;
              cin >> fOp2;

	          if (fOp2 != 0)
              {
              cout << fOp1 << " / " << fOp2 << " = " << (fOp1 / fOp2)
	               << endl;
              }
              else
                cout << "Cannot divide by zero." << endl;
              //Make sure not to divide by zero.
              
	          fCost = fCost + cDivide;
	          fNumber++;

	          break;

	  case '5':
	  case 'E':
	  case 'e':
              cout << "Exponent: Enter the base: " << endl;
	          cin >> fOp1;

              cout << "Enter the power: " << endl;
              cin >> fOp2;
              
              if (fOp1 == 0)
              {
                cout << fOp1 << " ^ " << fOp2 << " = 0" << endl;

                fCost = fCost + cMultiply;
                fNumber++;

                break;
              }
              //If the base is 0, the result will be 0, so output that,
              //then add the cost of one multiplication.
              
              else if (fOp2 == 0)
              {
                cout << fOp1 << " ^ " << fOp2 << " = 1" << endl;

                fCost = fCost + cMultiply;
                fNumber++;

                break;
	          }
	          //If the power is 0, the result will be 1, so output that,
	          //then add the cost of one multiplication.
	          
		      else if (fOp2 > 0)
		      {
		        for (float x = fOp2; x > 0; x--)
		        {
			      fExp = fExp * fOp1;

			      fCost = fCost + cMultiply;
                }
                cout << fOp1 << " ^ " << fOp2 << " = " << fExp
			         << endl;

                fNumber++;

	            break;
		      }
              //If the power is more than 0, multiply the base by itself
              //the number of times specified by the power. Add the cost
              //of multiplication each time, and increment the number
              //of calculations done.
		      
		      else if (fOp2 < 0)
		      {
		        for (float x = (-1 * fOp2); x > 0; x--)
		        {
			      fExp = fExp * fOp1;

                  fCost = fCost + cMultiply;
                }

                fCost = fCost + cDivide;
                fNumber++;
		      
		        cout << fOp1 << " ^ " << fOp2 << " = 1 / " << fExp
		           << endl;
		        
                break;
              }
              //If the power is less than 0, take the counter times -1
              //to make it count a positive number of times, then proceed
              //like in the last case. Then output 1 / the result.
              
      case '6':
      case 'B':
      case 'b':
              cout << "Billing information:" << endl;
              cout << "Number of calculations: " << fNumber << endl;
              cout << "Total cost: $ "<< fCost << endl;
              if (fNumber != 0)
              {
                cout << "Average cost of calculation: $" <<
                (fCost / fNumber) << endl;
              }
              break;
              //Output number of calculations done, their cost, and
              // divide cost by number if number is not 0 to avoid
              //a run-time error.
              
      case '7':
      case 'Q':
      case 'q':
              cout << "\n Exiting..." << endl;
    }

  }
  while (cChoice != '7' && cChoice != 'Q' && cChoice !='q');
  //Continue as long as the user didn't enter 7, Q, or q.

  return 0;
}

