Infix to post-fix expression

839 views Asked by At

following is the infix expression - A-(B/C+(D%E*F)/G)*H

Can you please tell me what will be it's postfix expression

1.ABC/D%EF*G/+H*-

or

2.ABC/DE%F*G/+H*- .

I am just not able to decide whether it will be D%E or DE% I think that after scanning D, "%" will be moved to stack & then E will be written & then by comparing * & %, % will take place, but on internet it is showing that DE% is right. Right answer with explanation will be appreciated.

1

There are 1 answers

0
Lucero On

Your second option is right.

Operators are not pushed to the stack; they pop two values from the stack, apply the operation, and push the result to the stack. Therefore, since % and * share the same precedence, they are applied left to right:

(D%E*F) =>
Push D
Push E
Modulo
Push F
Multiply