Infix to Postfix and Evaluate
Infix to Postfix and Evaluate
Infix to Postfix
Standard infix expressions can be converted to postfix. We develop an algorithm for this conversion in several
steps. The basic idea is to move operands directly to the postfix string but to store operators temporarily on a
stack. Each operator waits on the stack until its second operand is processed. In this way the operator will
appear after the operands. Since full order of operations requires careful stack manipulation, we begin with
only the notion of left association.
2b. Precedence
Next, we deal with precedence. We can’t simply go left-to-right on "3 + 4 * 5". We must compare the
operator on the top of the stack to the next operator. In this example, the stack has "+" and the next operator
is "*". Do not pop; instead push the "*" and continue processing the string. The algorithm will eventually
pop and append the "*" and then the "+", resulting in the correct order "3 4 5 * +".
In the example above, isStrictlyLower("*", "+") returns ______. Don’t pop. Push ____
In the example above, isHigherOrEqual("+", "*") returns ______. Don’t pop. Push ____
Obviously, your precedence method needs to handle all the possible combinations of operators. Start writing
them here:
2c. Parentheses, etc. "([{<"
What do you for parentheses, etc? When you encounter a left parenthesis, push it. This begins the logical
bottom of a stack within a stack. Continue processing as above. The left parenthesis remains on the stack
until its matching right parenthesis is encountered, after which the left is popped.
Assignment
First, implement the algorithm above in the method infixToPostfix. You will need to implement one of
the precedence methods. You may find it useful to call your PostfixEval.isOperator
Write the infix expressions you want to convert in a list in the main.
After the conversion works, call your working PostfixEval.eval on each postfix expression. Your final
program should output all three columns, as shown in the test run.
Submit InfixPostfixEval. The teachers will upload PostfixEval and ParenMatch as support files.
Test Run
Your final program should output all three columns.
First, for some points, improve infixToPostfix so that it produces the correct postfix expressions for
powers and factorials. Now you have four levels of precedence, +-, */%, ^, and !. Here are two examples for
^ and !:
1 + 3 ! à 1 3 ! +
1 - 2 ^ 3 à 1 2 3 ^ -
Second, for more points, improve infixToPostfix so that it 1) checks for matching parentheses and 2)
checks for non-algebraic characters. Presumably you already have a working
ParenMatch.checkParen(). You will have to make a new method to check for non-algebraic
characters.
Each check, if they fail, will throw a new Exception consisting of the offending expression and a short
error message. Here is an example of each:
3 + 2 > ERROR in parentheses
3 ? 2 ERROR non-arithmetic symbol
Because we aren’t using try-catch blocks, you will have to modify the header to throw the Exception, like
this:
If the infix expression passes both checks, produce the postfix expression and evaluate it. You already wrote a
method in a different class that evaluates a postfix expression.
Submit Infix_Extension. The teachers will upload the ParenMatch and the PostfixEval classes
as support files. The autograder will run your infixToPostfix method, not your main.