Having a hard time translating from infix to postfix

202 Views Asked by At

Trying to translate this infix expression: $(7 - 3) * (5 + ((8 * 4) - 9))$ in to a postfix expression. Based on my understanding the answer is $7\; 3 - 8\; 4 * 9 - 5 + *$ but when I look up solutions I get this answer: $7\; 3 - 5\; 8\; 4\; *\; 9 - + * $ Is there anything I am doing wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

Working from the inner parentheses outward and removing parentheses as they become redundant:

$$(7-3)*(5+((8*4)-9))$$ $$(73-)*(5+((8*4)-9))$$ $$(73-)*(5+((84*)-9))$$ $$(73-)*(5+((84*)9-))$$ $$(73-)*(5+(84*9-))$$ $$(73-)*(5(84*9-)+)$$ $$(73-)*(584*9-+)$$ $$(73-)(584*9-+)*$$ $$73-584*9-+*$$

It seems that you used the commutativity of addition to add $5$ to right hand side of the expression instead of the left. So your answer and the book's answer correspond to the same numerical value, but the book's answer would be more correct in the case that the operators involved happened to be noncommutative.

0
On

We have

$$ (a \times b) = a \; b \; \otimes$$ and similar for other operators. So $$(7 - 3) \times (5 + ((8 \times 4) - 9))\\ =(7 - 3) (5 + ((8 \times 4) - 9))\;\otimes\\ =(7\;3 \;\ominus)(5\;((8\times4)-9)\;\oplus)\;\otimes\\ =(7\;3 \;\ominus)(5\;((8\times4)\;9 \;\ominus)\;\oplus)\;\otimes\\ =(7\;3 \;\ominus)(5\;((8\;4 \otimes)\;9 \;\ominus)\;\oplus)\;\otimes\\$$ now remove the paranthesis and replace the circled symbol by the uncircled to get $$7\;3 \;-5\;8\;4 \times9\;-\;+\;\times$$ Your expression $$7\;3 \;-8\;4 \times9\;-\;5\;+\;\times$$ gives $$(7 - 3) \times ( ((8 \times 4) - 9)+5)$$ in infix notation