Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents for each step of conversion.
A/B+C*(D-E)
Answers
Answered by
2
When you write an arithmetic expression such as B * C, the form of the expression provides you with information so that you can interpret it correctly. In this case we know that the variable B is being multiplied by the variable C since the multiplication operator * appears between them in the expression. This type of notation is referred to as infix since the operator is in between the two operands that it is working on.
Consider another infix example, A + B * C. The operators + and * still appear between the operands, but there is a problem. Which operands do they work on? Does the + work on A and B or does the * take B and C? The expression seems ambiguous.
In fact, you have been reading and writing these types of expressions for a long time and they do not cause you any problem. The reason for this is that you know something about the operators + and *. Each operator has a precedence level. Operators of higher precedence are used before operators of lower precedence. The only thing that can change that order is the presence of parentheses. The precedence order for arithmetic operators places multiplication and division above addition and subtraction. If two operators of equal precedence appear, then a left-to-right ordering or associativity is used.
Let’s interpret the troublesome expression A + B * C using operator precedence. B and C are multiplied first, and A is then added to that result. (A + B) * C would force the addition of A and B to be done first before the multiplication. In expression A + B + C, by precedence (via associativity), the leftmost + would be done first.
Although all this may be obvious to you, remember that computers need to know exactly what operators to perform and in what order. One way to write an expression that guarantees there will be no confusion with respect to the order of operations is to create what is called a fully parenthesized expression. This type of expression uses one pair of parentheses for each operator. The parentheses dictate the order of operations; there is no ambiguity. There is also no need to remember any precedence rules.
The expression A + B * C + D can be rewritten as ((A + (B * C)) + D) to show that the multiplication happens first, followed by the leftmost addition. A + B + C + D can be written as (((A + B) + C) + D) since the addition operations associate from left to right.
Consider another infix example, A + B * C. The operators + and * still appear between the operands, but there is a problem. Which operands do they work on? Does the + work on A and B or does the * take B and C? The expression seems ambiguous.
In fact, you have been reading and writing these types of expressions for a long time and they do not cause you any problem. The reason for this is that you know something about the operators + and *. Each operator has a precedence level. Operators of higher precedence are used before operators of lower precedence. The only thing that can change that order is the presence of parentheses. The precedence order for arithmetic operators places multiplication and division above addition and subtraction. If two operators of equal precedence appear, then a left-to-right ordering or associativity is used.
Let’s interpret the troublesome expression A + B * C using operator precedence. B and C are multiplied first, and A is then added to that result. (A + B) * C would force the addition of A and B to be done first before the multiplication. In expression A + B + C, by precedence (via associativity), the leftmost + would be done first.
Although all this may be obvious to you, remember that computers need to know exactly what operators to perform and in what order. One way to write an expression that guarantees there will be no confusion with respect to the order of operations is to create what is called a fully parenthesized expression. This type of expression uses one pair of parentheses for each operator. The parentheses dictate the order of operations; there is no ambiguity. There is also no need to remember any precedence rules.
The expression A + B * C + D can be rewritten as ((A + (B * C)) + D) to show that the multiplication happens first, followed by the leftmost addition. A + B + C + D can be written as (((A + B) + C) + D) since the addition operations associate from left to right.
Answered by
2
Solution:
Steps of conversion infix to postfix expression:
1) It can be done by using a stack to store operators and then pop them in correct order of precedence.
2)Fix the priority of the operators,normally we take
3. - (unary operator)
2. /,*
1. +,-(subtraction)
Move__current token__stack__output
1_____A____empty____A
2_____/_____ /______A
3_____B_____/____AB
4_____+____+___AB/
5____C_____+____AB/C
6___*____*,+____AB/C
7____(____(,*,+___AB/C
8___D___(,*,+____AB/CD
9___-___(-,*,+___AB/CD
10____E___(-,*,+___AB/CDE
11____)_____(,-,*,+____AB/CDE
12____)_____*,+,____AB/CDE-
13_________+,____AB/CDE-*
14_________+,____AB/CDE-*+
Is the final post fix expression
AB/CDE-*+
Steps of conversion infix to postfix expression:
1) It can be done by using a stack to store operators and then pop them in correct order of precedence.
2)Fix the priority of the operators,normally we take
3. - (unary operator)
2. /,*
1. +,-(subtraction)
Move__current token__stack__output
1_____A____empty____A
2_____/_____ /______A
3_____B_____/____AB
4_____+____+___AB/
5____C_____+____AB/C
6___*____*,+____AB/C
7____(____(,*,+___AB/C
8___D___(,*,+____AB/CD
9___-___(-,*,+___AB/CD
10____E___(-,*,+___AB/CDE
11____)_____(,-,*,+____AB/CDE
12____)_____*,+,____AB/CDE-
13_________+,____AB/CDE-*
14_________+,____AB/CDE-*+
Is the final post fix expression
AB/CDE-*+
Similar questions
Social Sciences,
7 months ago
Computer Science,
7 months ago
English,
7 months ago
Accountancy,
1 year ago
Math,
1 year ago
Science,
1 year ago
Economy,
1 year ago