- Caculate Infix expression by convert to postfix expression then caculate in postfix
- Infix expression condition:
- Number is positive
- Doesn't Contain parenthesis
- Programing languages Assembly Mips
- Simulator: Mars
-
Print operands as they arrive.
-
If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack.
-
If the incoming symbol is a left parenthesis, push it on the stack.
-
If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses.
-
If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
-
If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator. If the association is right to left, push the incoming operator.
-
If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack.
-
At the end of the expression, pop and print all operators on the stack. (No parentheses should remain.)
- Scan the Postfix string from left to right.
- Initialise an empty stack.
- If the scannned character is an operand, add it to the stack. If the scanned character is an operator, there will be atleast two operands in the stack.
- If the scanned character is an Operator, then we store the top most element of the stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp. Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.
- Repeat this step till all the characters are scanned.
- After all characters are scanned, we will have only one element in the stack. Return topStack.
Input Infix is: 3+3+3*9*9-3*9
Postfix is: 3 3 +3 9 *9 *+3 9 *-
Result is: 222