calculate.cpp
// calculate.cpp provides a simple 4-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 op1 and op2;
// output(screen): the result of the expression (op1 operation op2).
//****************************************************************
#include <iostream> // cin, cout, <<, >>, good(), ...
#include <string> // string
#include <cassert> // assert()
using namespace std;
// ... Replace this line with the prototype of Apply()
int main()
{
const string MENU = "\nPlease enter:\n"
"\t+, to perform addition;\n"
"\t-, to perform subtraction;\n"
"\t*, to perform multiplication;\n"
"\t/, to perform division;\n"
"--> ";
double op1, op2; // operands 1 and 2
double result; // result of operation kept here
char operation; // operation is +, -, * or /
cout << endl << "Welcome to the 4-function calculator!"
<< endl << MENU;
cin >> operation;
assert(operation == '+' || operation == '-' ||
operation == '*' || operation == '/');
cout << endl << "Now enter your operands: ";
cin >> op1 >> op2;
assert(cin.good());
assert(operation != '/' || op2 != 0);
result = Apply(operation, op1, op2);
cout << endl << op1 << operation << op2
<< " = " << result << endl;
return 0;
}
// ... Replace this line with the definition of Apply()
|