Prefix expression, infix expression, postfix expression

(1) Three forms
of expression: infix expression: operator is placed between two operands, such as: (2+1)*3;
postfix expression: does not contain parentheses, operator is placed in two operations After the object, all calculations are performed strictly from left to right in the order in which the operators appear (the precedence rules of operators are no longer considered, such as: 2 1 + 3 *;
prefix expression: same as postfix expression, excluding Brackets, operators are placed in front of the two operands, such as: * + 2 1 3.
(2) Expression calculation:
Since there are no brackets in the suffix expression, there is no need to distinguish the priority, and the calculation is performed strictly from left to right , So calculating a suffix expression is much simpler than a computer infix expression.
The algorithm idea of ​​converting an infix expression to a suffix expression:
·When reading a number, send it directly to the output queue
·When reading an operator At t,
a. Pop all operators with priority higher than or equal to t from the
stack and send them to the output queue; bt
pushes to the stack. When reading the left parenthesis, always push it to the stack.
When reading the right parenthesis , Pop all the operators above the first left parenthesis near the top of the stack in turn, send them to the output queue, and then discard the left parenthesis.

The specific method of using postfix expressions to calculate:
·Create a stack S
·From left to right Read the suffix expression, read the number, convert it into a value and push it into the stack S, read the operator, pop two numbers from the stack to Y and X respectively, and then computer in the form of "X operator Y" The result is output, and then added to the stack S.
If the suffix expression has not been read, repeat the above process, and finally output the value at the top of the stack to end

Guess you like

Origin blog.csdn.net/geggegeda/article/details/4148238