I am preparing for an exam where i couldn't understand the convertion of infix notation to polish notation for the below expression:
(a–b)/c*(d + e – f / g)
Can any one tell step by step how the given expression will be converted to prefix?
I am preparing for an exam where i couldn't understand the convertion of infix notation to polish notation for the below expression:
(a–b)/c*(d + e – f / g)
Can any one tell step by step how the given expression will be converted to prefix?
simple google search came up with this. Doubt anyone can explain this any simpler. But I guess after an edit, I can try to bring forward the concepts so that you can answer your own question.
Hint :
Study for exam, hard, you must. Predict you, grade get high, I do :D
Explanation :
It's all about the way operations are associated with operands. each notation type has its own rules. You just need to break down and remember these rules. If I told you I wrote (2*2)/3 as [* /] (2,2,3) all you need to do is learn how to turn the latter notation in the former notation.
My custom notation says that take the first two operands and multiple them, then the resulting operand should be divided by the third. Get it ? They are trying to teach you three things.
In Prefix expression operators comes first then operands : +ab[ oprator ab ]
Infix :
(a–b)/c*(d + e – f / g)
Step 1:
(a - b) = (- ab)
[ '(' has highest priority ]step 2:
(d + e - f / g) = (d + e - / fg)
[ '/' has highest priority ]Step 3:
(-ab )/ c * (- + de / fg)
=/ - abc * (- + de / fg)
Prefix :
* / - abc - + de / fg
This is the algorithm using stack.
Just follow these simple steps.
1.Reverse the given infix expression.
2.Replace '(' with ')' and ')' with '(' in the reversed expression.
3.Now apply standard infix to postfix subroutine.
4.Reverse the founded postfix expression, this will give required prefix expression.
In case you find step 3 difficult consult http://scanftree.com/Data_Structure/infix-to-prefix where a worked out example is also given.
Infix to PostFix using Stack:
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
step through:
(a–b)/c*(d + e – f / g)
Prefix notation (reverse polish has the operator last, it is unclear which one you meant, but the principle will be exactly the same):
(/ f g)
(+ d e)
(- (+ d e) (/ f g))
(- a b)
(/ (- a b) c)
(* (/ (- a b) c) (- (+ d e) (/ f g)))