Convert Infix Expression into the Post fix Expression. (3 + 5) * 2 / (2 ^ 3 - 1)
Question
Convert Infix Expression into the Post fix Expression. (3 + 5) * 2 / (2 ^ 3 - 1)
Solution
Sure, here are the steps to convert the infix expression (3 + 5) * 2 / (2 ^ 3 - 1) into postfix expression:
-
First, we need to understand the precedence of operators. The precedence order is as follows: Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right).
-
Start scanning the infix expression from left to right.
-
If the scanned character is an operand, output it.
-
Else,
- If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty), push it.
- Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. Push the scanned operator to the stack.
-
If the scanned character is an '(', push it to the stack.
-
If the scanned character is an ')', pop and output from the stack until an '(' is encountered. Remove both '(' and ')'.
-
Repeat steps 3-6 until infix expression is scanned.
-
Pop and output from the stack until it is not empty.
Let's apply these steps to our expression (3 + 5) * 2 / (2 ^ 3 - 1):
-
Scan
(3 + 5), according to the rule, push(to the stack, output3, push+to the stack, output5, encounter), pop and output+from the stack, remove(and). Now the output is35+. -
Scan
* 2, push*to the stack (because*has higher precedence than+), output2. Now the output is35+2. -
Scan
/ (2 ^ 3 - 1), push/to the stack (because/has the same precedence as*), push(to the stack, output2, push^to the stack (because^has higher precedence than/), output3, encounter-, pop and output^from the stack (because^has higher precedence than-), push-to the stack, output1, encounter), pop and output-from the stack, remove(and). Now the output is35+2*23^1-. -
Pop and output from the stack until it is not empty, we get
/ *. Now the output is35+2*23^1-/.
So, the postfix expression of (3 + 5) * 2 / (2 ^ 3 - 1) is 35+2*23^1-/.
Similar Questions
Convert infix expression to postfix 4*8/2+(9+8-7^2)-5*9
The postfix expression of the infix expression 4 * ( 3 + 2 - 6 ) / 8A4 3 2 + 6 - * 8 /B4 * 3 2 + 6 - 8 /C4 3 2 + * 6 8 / -D4 * 3 2 6 - + 8 /
Select the expression(s) that result in a SyntaxErrorSyntaxError.8 / (3 / (2 / 3)))6 + -24 **5 * (3 + 2)
Write an algorithm for converting infix expression into postfix expression.
Rewrite the expression with fraction exponent.y = 1(2x+3)3
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.