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?
-
And what is solution that you looked up? – miracle173 Mar 07 '17 at 09:03
-
Edited question – Darien Springer Mar 07 '17 at 09:10
-
Your brackets are wrong in the original equation, you open 4 and close 3, can you correct? – lioness99a Mar 07 '17 at 09:11
-
transform both back to infix notation and check what's the difference – miracle173 Mar 07 '17 at 09:11
-
I guess my confusion is that when I write them out in infix it seems like that ultimately both come up with the same result. – Darien Springer Mar 07 '17 at 09:18
2 Answers
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
- 11,049
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.
- 3,846