Modify the expression evaluator case study program from this chapter to make a new program that accepts

Question:

Modify the expression evaluator case study program from this chapter to make a new program that accepts as input a fully parenthesized infix expression and returns a string representing an equivalent postfix expression. Postfix expressions are ones where each operator follows its two operands, such as 1 2 + rather than 1 + 2. Postfix expressions are elegant in that they do not need parentheses. For example, the given infix expression:

(9 + (8 * 7 - (6 / 5 ^ 4) * 3) * 2))

is equivalent to the following postfix expression:

9 8 7 * 6 5 4 ^ / 3 * - 2 * +

Your algorithm should read the expression token by token, using a stack to store operators. (You don’t need the values stack from the original case study anymore.) Instead, each time a number is encountered, append it to a string that you are building up. Each time a right parenthesis is encountered, pop the stack to get an operator and append it to the string you are building up. Leave in place the rest of the code to preserve the error checking. Return the string "illegal expression" if an error is encountered.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  answer-question
Question Posted: