conversion from infix to prefix

后端 未结 14 1279
有刺的猬
有刺的猬 2020-12-09 10:24

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)         


        
相关标签:
14条回答
  • 2020-12-09 10:45

    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

    0 讨论(0)
  • 2020-12-09 10:50

    (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

    1. (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

      • / [-ab]c[-+de/fg] = remove the grouping symbols = */-abc-+de/fg --> Prefix
    2. (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

    0 讨论(0)
  • 2020-12-09 10:52
    (a–b)/c*(d + e – f / g)
    

    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.

    0 讨论(0)
  • 2020-12-09 10:52

    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.

    1. To become comfortable with different notations. The example I gave is what you will find in assembly languages. operands (which you act on) and operations (what you want to do to the operands).
    2. Precedence rules in computing do not necessarily need to follow those found in mathematics.
    3. Evaluation: How a machine perceives programs, and how it might order them at run-time.
    0 讨论(0)
  • 2020-12-09 10:52

    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.

    0 讨论(0)
  • 2020-12-09 10:53

    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.

    0 讨论(0)
提交回复
热议问题