Brijesh Roy
Brijesh Roy

Reputation: 63

Infix to post-fix expression

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.

Upvotes: 0

Views: 1132

Answers (1)

Lucero
Lucero

Reputation: 60256

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

Upvotes: 1

Related Questions