Question: C++ Enhance the postfix evaluator by adding the operators below. That means use the original code and modify; don't write an entirely new program. For
C++ Enhance the postfix evaluator by adding the operators below. That means use the original code and modify; don't write an entirely new program. For your upload, just upload the .cpp this time. You don't need to zip a Visual studio Project. I'm assuming you tested in VS though to make sure it runs.
Throw a logic_error (not enough operands) if stack is empty
Unary minus ~ : Change the top of the stack to its opposite e.g. 5.0 --> -5.0 .
Factorial ! : A generalized factorial for nonnegative doubles.
x! = x(x-1)(x-2)...(smallest value > 1), or 1 if 0 <= x <= 1
NOTE: x doesn't have to be an integer under this definition so don't truncate it.
Throw a logic_error exception (not enough operands) if stack is empty.
Also, throw a range_error exception if x < 0.
Sum S :
a k-ary operator with top of stack = k' and k = int(k'). (So use the floor of top value as arity.)
The result will be the sum of all k values underneath the top value (i.e. under k').
Throw an exception (not enough operands) if stack is empty (so no k to look at!) or if stack size < k+1 (prior to popping k').
Note: if k is zero we will still consider the operation to be legal with the result = zero.
That corresponds to an empty summation in Math.
Average $ : like sum but divide the sum by k.
Here a zero for k will be illegal since we can't divide by zero
(Throw a range_error exception, similarly what's done in process for division by zero. )
Exponentiation ^ :
Take v1 to the power v2.
Throw a range_error exception in any case where pow(v1,v2) would give back infinity or NaN.
(It's possible to code this by actually doing the operation and detecting a result of infinity or NaN.)
Also, throw an exception (not enough operands) if stack size < 2.
If you encounter any cases or subtleties not covered by the instructions above, let me know and I'll clarify!
To test your code use sample expressions involving multiple operators
For example: 10 3 2 + 1 3 ~ 3 S 6 3 3 $ 0.5 ^ 1 + ! -
Stack:
10 3 2
10 5 (after +)
10 5 1 3
10 5 1 -3 (after ~)
10 5 1 -3 3
10 3 (after S)
10 3 6 3 3
10 4 (after $)
10 4 0.5
10 2 (after ^)
10 2 1
10 3 (after +)
10 6 (after !)
4 (after -)
Code:
/* Stack Demo Evalutes a postfix arithmetic expression via a stack. This version only handles the four binary operators +,-,*,/ */
#include
using namespace std;
bool isOperator(const string &tok, int &arity); double toDouble(const string &s); double evalBinaryOp(double x, double y, const string &op); void process(const string &tok, stack
int main() {
string tok, exp; bool done = false; // read each line of input as a postfix expression and output the corresponding value do { cout << "Please type in a postfix expr or Enter to quit: "; getline(cin, exp); // get next expression to process if (exp == "") done = true; // user wants to quit else { try { istringstream in(exp); // create a stringstream object stack
// Return true iff tok appears to be an operator, meaning a one character that's not a digit. // Pass back arity via reference paraeter arity. bool isOperator(const string &tok, int &arity) { arity = 0; if (tok.length() != 1) return false; // only single character ops are supported char op = tok[0]; if (op >= '0' && op <= '9') return false; // it's a number arity = 2; // Assume it's a binary op. This will need to be updated. return true; }
// Convert s to a double. // If conversion fails, throw a logic_error exception. double toDouble(const string &s ) { istringstream in(s); double x; in >> x; if (!in) throw logic_error("Bad operand"); return x; }
// Return result of evaluating the the binary operation op applied to x and y // if op doesn't represent a known operation then throw an exception. double evalBinaryOp(double x, double y, const string & op) {
if (op == "+") return x + y; if (op == "-") return x - y; if (op == "*") return x * y; if (op == "/") { if (y == 0) throw range_error("Can't divide by zero!"); return x / y; } throw logic_error("Unsupported operator!"); }
/* Process the next token. The token might be an operator or an operand.
In the case of an operator: pop appropriate number of operands from the stack, evaluate the operation and push answer onto the stack. The current implementation only supports binary operators, which means exactly two values will be popped from the stack.
In the case of an operand. convert it to a double and push onto the stack. */ void process(const string & tok, stack
double answer = evalBinaryOp(v1, v2, tok); // e.g. add two numbers stk.push(answer); // push answer onto the stack } else { double d = toDouble(tok); // convert token to a double stk.push(d); // push the value onto stack } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
