conversion from infix to prefix

后端 未结 14 1322
有刺的猬
有刺的猬 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:41

    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 ]

                           = (+ de - / fg )
    
              ['+','-' has same priority but left to right associativity]
    
                           = (- + de / fg)
    

    Step 3: (-ab )/ c * (- + de / fg) = / - abc * (- + de / fg)

                                     = * / - abc - + de / fg 
    

    Prefix : * / - abc - + de / fg

提交回复
热议问题