Sample Solution to Lab #3
///////////////////////////////////////////////////////////////////////////// // Student name: Nigel Tufnel // Student ID: 123-45-6789 // Course: CS161 Summer 97 // Assignment: #3 // Due date: 7-22-97 // // Description: evaluate.cpp // // This program prompts the user for an expression, // then evaluates it and displays the result. The // expression must be in the form: // // number operator number operator number // // Ex: 5 - 2 * 4 // // In the example above, 5-2*4 evaluates to -3. // /////////////////////////////////////////////////////////////////////////// #include <iostream.h> // for cin/cout #include <iomanip.h> // for setprecision()
// some definitions for Booleans typedef int Boolean; const int TRUE = 1; const int FALSE = 0; ///////////////////////////////////////////////////////////////////////////// // Function: getExpression // This function prompts the user for an expression and returns // the operands and operators to the caller. The user's input // is of the form: number operator number operator number // // Inputs: None // // Outputs: a float representing the first operand (reference) // a char representing the first operator (reference) // a float representing the second operand (reference) // a char representing the second operator (reference) // a float representing the third operand (reference) // void getExpression(float &operand1, char &operator1, float &operand2, char &operator2, float &operand3); ///////////////////////////////////////////////////////////////////////////// // Function: evaluateExpression // This function evaluates the expression that the user inputted. // It checks to see in which order the expression should be // evaluated (left to right, right to left) // // Inputs: a float representing the first operand (value) // a char representing the first operator (value) // a float representing the second operand (value) // a char representing the second operator (value) // a float representing the third operand (value) // // Outputs: a single float representing the result of // evaluating the expression // float evaluateExpression(float operand1, char operator1, float operand2, char operator2, float operand3); ///////////////////////////////////////////////////////////////////////////// // Function: performOperation // This function evaluates a simple expression containing // a left operand, an operator, and a right operand // // Inputs: a float representing the left operand (value) // a char representing the operator (value) // a float representing the right operand (value) // // Outputs: a single float representing the result // of evaluating the simple expression // float performOperation(float operand1, char operater, float operand2); ///////////////////////////////////////////////////////////////////////////// // Function: operatorsAreValid // This function verifies that the two operators input by // the user are valid. Valid operators are: + - * / // // Inputs: a char representing the first operator (value) // a char representing the second operator (value) // // Outputs: a Boolean representing the outcome of the verification // TRUE - both operators are valid // FALSE - either one or both operators are invalid // Boolean operatorsAreValid(char operator1, char operator2); //////////////////////////////////////////////////////////////////////////////// // Function: printResults // This function prints the expression entered by the user // and also the results of evaluating the expression // // Inputs: a float representing the first operand (value) // a char representing the first operator (value) // a float representing the second operand (value) // a char representing the second operator (value) // a float representing the third operand (value) // a float representing the result (value) // // Outputs: None // void printResult(float operand1, char operator1, float operand2, char operator2, float operand3, float result); // program entry point void main(void) { char operator1, operator2; // operators input by the user float operand1, operand2, operand3; // operands input by the user float result; // result after evaluating expression Boolean valid; // indicates valid/invalid operators // get expression from the user getExpression(operand1, operator1, operand2, operator2, operand3); // check to see if the operators are valid valid = operatorsAreValid(operator1, operator2); // if they are valid, continue with program if (valid == TRUE) { // evaluate the expression, assigning the value to result result = evaluateExpression(operand1, operator1, operand2, operator2, operand3); // display expression and result printResult(operand1, operator1, operand2, operator2, operand3, result); } } void getExpression(float &operand1, char &operator1, float &operand2, char &operator2, float &operand3) { // prompt the user for an expression in the appropriate form cout << "Enter an expression in the form: "; cout << "num op num op num (ex. 5 * 3 - 8)" << endl; // read the user's expression into the proper variables cin >> operand1 >> operator1 >> operand2 >> operator2 >> operand3; } float evaluateExpression(float operand1, char operator1, float operand2, char operator2, float operand3) { float tempResult; // holds the intermediate result float finalResult; // holds the final result // if the second operator is multiply or divide, and the // first operator is addition or subtraction, then it is safe // to evaluate the expression from right to left // The code in BOLD was added after the class on Tuesday night if ( (operator2 == '*' || operator2 == '/') && (operator1 == '+' || operator1 == '-') ) { // evaluate second operator first tempResult = performOperation(operand2, operator2, operand3); // now, evaluate first operator using tempResult as second operand finalResult = performOperation(operand1, operator1, tempResult); } // otherwise, we should evaluate from left to right else { // evaluate first operator first tempResult = performOperation(operand1, operator1, operand2); // now, evaluate second operator using tempResult as first operand finalResult = performOperation(tempResult, operator2, operand3); } // return the value of the final result to the caller return finalResult; } float performOperation(float operand1, char operater, float operand2) { // This will hold the value of the expression so we can // pass it back to the caller float result; // Depending on what 'operater' is, we will perform 1 of the 4 // possible operations switch (operater) { // multiplication: multiply the two operands together case '*': result = operand1 * operand2; break; // addition: add the two operands together case '+': result = operand1 + operand2; break; // subtraction: subtract the second operand from the first operand case '-': result = operand1 - operand2; break; // division: divide the first operand by the second operand case '/': // If the denominator is 0, we will display an error message // and set the result to 0.0 if (operand2 == 0.0f) { cout << "ERROR! Attempting to divide by 0" << endl; result = 0.0f; } else result = operand1 / operand2; break; // If we get anything other than the 4 valid operators, // display a message showing the invalid operator, and set // the result to 0.0 default: cout << "Invalid operator: " << operater << endl; result = 0.0f; break; } // return the result of the evaluation to the caller return result; } Boolean operatorsAreValid(char operator1, char operator2) { // check if the first operator is a valid one if ( (operator1 != '*') && (operator1 != '/') && (operator1 != '+') && (operator1 != '-') ) { // display message cout << "ERROR: " << operator1 << " is not a valid operator" << endl; // return result to caller return FALSE; } // check if the second operator is a valid one if ( (operator2 != '*') && (operator2 != '/') && (operator2 != '+') && (operator2 != '-') ) { // display message cout << "ERROR: " << operator2 << " is not a valid operator" << endl; // return result to caller return FALSE; } // both are valid, so return TRUE return TRUE; } void printResult(float operand1, char operator1, float operand2, char operator2, float operand3, float result) { // display the expression that the user inputted as well as // the result of evaluating it cout << "The expression " << setprecision(5); cout << operand1 << " " << operator1 << " "; cout << operand2 << " " << operator2 << " " << operand3; cout << " evaluates to " << setprecision(5) << result << endl; }Back to Outline