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?
Maybe you're talking about the Reverse Polish Notation? If yes you can find on wikipedia a very detailed step-to-step example for the conversion; if not I have no idea what you're asking :(
You might also want to read my answer to another question where I provided such an implementation: C++ simple operations (+,-,/,*) evaluation class
(a–b)/c*(d + e – f / g)
remember scanning the expression from leftmost to right most start on parenthesized terms follow the WHICH COMES FIRST rule... *, /, % are on the same level and higher than + and -.... so (a-b) = -bc prefix (a-b) = bc- for postfix another parenthesized term: (d + e - f / g) = do move the / first then plus '+' comes first before minus sigh '-' (remember they are on the same level..) (d + e - f / g) move / first (d + e - (/fg)) = prefix (d + e - (fg/)) = postfix followed by + then - ((+de) - (/fg)) = prefix ((de+) - (fg/)) = postfix
(-(+de)(/fg)) = prefix so the new expression is now -+de/fg (1) ((de+)(fg/)-) = postfix so the new expression is now de+fg/- (2)
(a–b)/c* hence
(a-b)/c*(d + e – f / g) = -bc prefix [-ab]/c*[-+de/fg] ---> taken from (1) / c * do not move yet so '/' comes first before '*' because they on the same level, move '/' to the rightmost : /[-ab]c * [-+de/fg] then move '*' to the rightmost
(a-b)/c*(d + e – f / g) = bc- for postfix [ab-]/c*[de+fg/-]---> taken from (2) so '/' comes first before '' because they on the same level, move '/' to the leftmost: [ab-]c[de+fg/-]/ then move '' to the leftmost [ab-] c [de+fg/-]/ = remove the grouping symbols= a b - c d e + f g / - / * --> Postfix
step 1:
(a-b)/c*(d+e- /fg))
step 2:
(a-b)/c*(+de - /fg)
step 3:
(a-b)/c * -+de/fg
Step 4:
-ab/c * -+de/fg
step 5:
/-abc * -+de/fg
step 6:
*/-abc-+de/fg
This is prefix notation.
here is an java implementation for convert infix to prefix and evaluate prefix expression (based on rajdip's algorithm)
If there's something about what infix and prefix mean that you don't quite understand, I'd highly suggest you reread that section of your textbook. You aren't doing yourself any favors if you come out of this with the right answer for this one problem, but still don't understand the concept.
Algorithm-wise, its pretty darn simple. You just act like a computer yourself a bit. Start by puting parens around every calculation in the order it would be calculated. Then (again in order from first calculation to last) just move the operator in front of the expression on its left hand side. After that, you can simplify by removing parens.
I saw this method on youtube hence posting here.
given infix expression : (a–b)/c*(d + e – f / g)
reverse it :
read characters from left to right.
maintain one stack for operators
credits : youtube