479 views
1 votes
1 votes

Q:in the infix to postfix conversion of this, which operator is computed first?

                                    a+b-c*d/e

a) +    b)*    c)-    d)/

ANS:

according to precedence and assosiativity * shouyld be computed first of all in the expression but answer is given as +

are they trying to say it because the postfix ab+cd*e/- is the postfix!!!! but the word 'computed' is confusing

1 Answer

Best answer
4 votes
4 votes

Infix expression : a + b - c *d / e

if we apply infix to post-fix algorithm on it the a/c to this algorithm 

Operator should get into the stack not Operand 

Inside Stack only Higher Priority operator can sit on Lower Priority Operator

if Higher Priority Operator is already in the stack then we first Pop-off the Higher Priority Operator from stack and the push the Lower priority Operator.

if we get any operand in expression we just keep writing them a/c to the above rules.

+ - * /

first step is see expression a + b - c *d / e

we first got a we don't do anything with it because it is a operand  just write it down separate a

then we got + and initially stack is empty so push it on to the stack

then we got b don't do anything just write it down as it is b

Total postfix Expression till now ab

then we got "-" a/c to precedence both have same precedence but a/c to associativity they are Left associative means + has higher priority here and as i said Lower Priority operator can't sit on higher priority operator so we have to pop off the "+" from stack and write it down with ab it will be  ab+ now stack is empty and we can push " - " now onto the stack.

then we got c don't do anything with it write it down as it is c

then we got "*" which has higher precedence then "-" so it can sit on it.

then we got d don't do anything with it write it down as it is d

Total postfix Expression till now ab+cd

now we got "/" which has same precedence as "*" has now we look at associativity in which "*" won because they have Left to  right .so lower associativity operator can'f sit on at higher one so pop off expression would be ab+cd*

then we got e don't do anything with it write it down as it is e

Total Postfix Expression till now ab+cd*e

in stack we got / and - so pop off and get the final postfix expression

Final postfix Expression ab+cd*e/-

This is how Algorithm works but you can compute it without this algorithm as well.

selected by

Related questions

0 votes
0 votes
1 answer
2
akash28 asked Feb 23, 2023
785 views
Convert the following infix expression to postfix and prefixA^B^C^D
1 votes
1 votes
1 answer
4
Shubham Aggarwal asked Sep 1, 2018
1,871 views
A postfix expression P is given with n binary operators. To evaluate the operator using stack, how many PUSH and POP operations are needed? A) PUSH:n, POP:n ...