course.wilkes.edu/CS125Labs
Welcome to the site for CS 125 labs!

 

calculate2.cpp

Prev | Next | Lab 6

// 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() ...

Prev | Next | Lab 6

 
  Home

Help

Lab 0

Lab 1

Lab 2

Lab 3a

Lab 3b

Lab 4

Lab 5

Lab 6

Lab 7

Lab 8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13



Membership

Login




Last update: Friday, October 13, 2000 at 1:37:24 PM.
visitors to this page.