Problem description:
A prefix expression is also called a polish expression.
Example of prefix fix:
(4 + 2) * (3 + 6) => * + 4 2 + 3 6
(3 + 4/2)-5 =>-+ 3/4 2 5
Thought 1 (recursion ):
1. Scan from left to right
2. Because the prefix expression contains binary operators without parentheses, operators can be solved recursively. For example, multiplication:
Case '*':
Return exp () * exp ();
Idea 2 (stack ):
1. Maintain the digital Stack
2. Scan from the right to the left. When a number is displayed, it is written into the stack. When a symbol is displayed, two numbers are generated. After calculation, the new number is reentered into the stack,The prefix expression and suffix expression are symmetric.So you can directly solve the problem in the opposite direction by the suffix method.
Reprinted, please indicate the reference from:
Http://www.cnblogs.com/breakthings/p/4051773.html
Prefix expression solving