//************************************************************************** // // Copyright (c) 1997-1999. // Richard D. Irwin, Inc. // // This software may not be distributed further without permission from // Richard D. Irwin, Inc. // // This software is distributed WITHOUT ANY WARRANTY. No claims are made // as to its functionality or purpose. // // Authors: James P. Cohoon and Jack W. Davidson // Date: 7/1/98 // Purpose: Compute a simple arithmetic expression // Revised: Keith Shomper 9/25/03 // - reformatted for use in class lecture // //************************************************************************** #include using namespace std; int main() { int LeftOperand, RightOperand; // Integers to combine in simple expression int Result; // Result of expression (i.e., the answer) // Expression operator--valid operators are +, -, / and * char Operator; // prompt for simple expression cout << "Please enter a simple expression (number operator number): "; cin >> LeftOperand >> Operator >> RightOperand; // compute desired operation switch (Operator) { case '+': Result = LeftOperand + RightOperand; break; case '-': Result = LeftOperand - RightOperand; break; case '*': Result = LeftOperand * RightOperand; break; // the divide case requires we check to make sure the RightOperand is // not zero, since division by zero is undefined case '/': if (RightOperand != 0) Result = LeftOperand / RightOperand; else { cout << LeftOperand << " / " << RightOperand << " cannot be computed:" << " denominator is zero." << endl; return 1; } break; default: cout << Operator << " is unrecognized operation." << endl; return 1; } // display result cout << LeftOperand << " " << Operator << " " << RightOperand << " equals " << Result << endl; return 0; }