Frage

Im supposed to convert the following to postfix form: (A + B * C) / (D - E * F)

I got this for an answer: ABC*+DEF*-/

Is this correct? There are a number of questions after that will all be incorrect if I'm using the wrong postfix form. If I am wrong, can you show me why? Thanks for any help.

War es hilfreich?

Lösung

I know this is an old submission, but here goes

This is the correct form. You can easily check it by iterating through the postfix yourself and converting it back to infix, like this, starting with an empty stack.

A is the first element in the array, and it is a number, so push it onto the stack. The same holds true for B andC. Therefore your stack is nowA,B,C`.

The next token is an operator(*) and it takes two operands. Therefore, pop the top two operands from the stack, or B and C. Combine the two, separated by the operator, and push it onto the stack. To simplify the algorithm, just put parentheses around everything. Your stack is now A,(B*C).

Your next token is another binary operator(+). Repeat the same process as above, and you get your stack as (A+(B*C)).

Repeat the process for the rest of it, and you'll get an expression equivalent to (A+B*C)/(D-E*F)

Andere Tipps

yes , by iterating back is a method to check this but you can also convert this expression from starting from infix to postfix. expression:-(A+BC)/(D-EF)start with first symbol in the expression.

symbol                stack(operator)              postfix(operand)
 (                        (
  A                       (                           A 
  +                       (+                          A
  B                       (+                          AB
  *                       (+*                         AB
  C                       (+*                         ABC
  )                       (+*)                        ABC
     as bracket closes every symbol inside bracket popout(LIFO)
                                                      ABC*+
  /                       /                           ABC*+
  (                       /(                          ABC*+
  D                       /(                          ABC*+D
  -                       /(-                         ABC*+D
  E                       /(-                         ABC*+DE
  *                       /(-*                        ABC*+DE
  F                       /(-*                        ABC*+DEF
  )                       /(-*)                       ABC*+DEF
                            /                         ABC*+DEF*-
   as all symbol have been done operator left in stack will popout(LIFO)
                                                      ABC*+DEF*-/

as higher priority operator come it can be put in stack with already lower priority operator but in opposite case if higher priority operator in stack and lower priority symbol comes then higher priority operator will pop out from stack to postfix expression . Note :- No two same priority operator can stay together in stack as second operator come first operator of same priority will pop out .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top