// calculate.cpp is a simple 5-function calculator.
//
// Begun by: Joel Adams, for Hands On C++.
// Completed by:
// Date:
// Purpose:
//
// Specification:
// input(keyboard): a char, stored in operation;
// two reals, stored in variables op1 and op2;
// output(screen): the result of the expression (op1 operation op2).
//******************************************************************
#include <iostream> // cin, cout, ...
#include <string> // string
#include <cassert> // assert()
using namespace std;
char GetMenuChoice(const string MENU);
double Apply(char operation, double op1, double op2);
// ... replace this line with the prototype of Power() ...
int main()
{
const string MENU = "\nPlease enter:\n"
"\ta, to perform addition;\n"
"\tb, to perform subtraction;\n"
"\tc, to perform multiplication;\n"
"\td, to perform division; and\n"
"\te, to perform exponentiation.\n"
"--> ";
double op1, op2;
double result;
cout << endl << "Welcome to the 5-function calculator!" << endl;
char operation = GetMenuChoice(MENU);
cout << endl << "Now enter your operands: ";
cin >> op1 >> op2;
assert(cin.good());
result = Apply(operation, op1, op2);
cout << endl << "The result is " << result << endl;
return 0;
}
char GetMenuChoice(const string MENU)
{
cout << MENU;
char choice;
cin >> choice;
return choice;
}
double Apply(char operation, double op1, double op2)
{
switch (operation)
{
case 'a':
return op1 + op2;
case 'b':
return op1 - op2;
case 'c':
return op1 * op2;
case 'd':
return op1 / op2;
case 'e':
// return Power(op1, int(op2));
default:
cerr << endl << "Invalid operation " << operation
<< " received by calculator!" << endl;
return 0.0;
}
}
// ... replace this line with the definition of Power() ...