// evalfull.cpp - evaluates a fully-parenthesized expression // NAME(S), DATE #include // for atof function #include // for sscanf #include // for strcmp and strtok #include #include // STL stack class #include // for throwing string exceptions using namespace std; // constants used to identify a token - DO NOT CHANGE enum TokenType {LEFT, RIGHT, ADD, SUBTRACT, MULTIPLY, DIVIDE, NUMBER, OTHER}; TokenType identify(char *t); // balanced - returns true only if parentheses are balanced // in the expression, where expression is an array of C // string tokens like { "(", "4.2", ")" } and numTokens // is the number of tokens in the array (3 in the sample) bool balanced(char *expression[], int numTokens) { stack s; // USE s TO SOLVE THE PROBLEM - it is an STL // (Standard Template Library) structure with // all of the same operations as the stack from // Step 2 of this lab, but it won't get full // and it can store any type - here return false; // REPLACE THIS return WITH ACTUAL IMPLEMENTATION } // DO NOT CHANGE ANYTHING BELOW - BUT DO READ IT // utility function returns one of those constants TokenType identify(char *t) { if (strcmp(t, "(") == 0) return LEFT; if (strcmp(t, ")") == 0) return RIGHT; if (strcmp(t, "+") == 0) return ADD; if (strcmp(t, "-") == 0) return SUBTRACT; if (strcmp(t, "*") == 0) return MULTIPLY; if (strcmp(t, "/") == 0) return DIVIDE; double value; if (sscanf(t, "%g", &value) == 1) return NUMBER; return OTHER; } // evalFull - evaluates a fully-parenthesized expression; // relies on function balanced; // returns result of the expression if it is formed properly // throws string message if expression is not proper double evalFull(char *expression[], int numTokens) { if ( !balanced(expression, numTokens) ) throw string("parentheses are not balanced"); stack numbers; stack ops; double result = 0, leftValue, rightValue; TokenType type, op; for (int i=0; i