Infix-to-postfix expression and computation - all O(∩_∩)O with stack processing

1. Infix expression to postfix expression

① Two basic rules: (1) Unconditional digital output

                             (2) Symbols are pushed into the stack for corresponding processing

② Symbol processing rules:

(A simple memory is that only the strongest can stand at the peak. If they are not strong enough, the original bosses will run away and abscond with the money. When the bosses gigglerun away, they can suppress the younger brothers. giggle)

    (1) When the priority of the current symbol is greater than the priority of the symbol at the top of the stack, the current symbol is pushed onto the stack

            Example: current symbol * , stack top symbol + , push stack

                

The situation of the stack after pushing the stack
 
* (top of stack)
+ (top of the original stack)

    (2) The priority of the current symbol When the priority of the  symbol at the top of the stack is less than or equal to the priority of the symbol on the top of the stack, the elements in the stack are first popped off until the priority of the current top of the stack is  less than  the priority of the symbol to be processed. The input pending symbol is -  

Priority relation: * == /      >    + == -

the original stack h later stack
*
 
+

-

③ Bracket processing

    (1) Left parenthesis   unconditionally push the stack;

    (2) Right parenthesis      ) The left parentheses are matched one by one starting from the top of the stack, and all symbols between the pair of parentheses will be popped out of the stack in turn.

④ Other supplements:



2. Calculation of postfix expressions

The above processing stores the postfix expressions in an array.

Rule: Scan from left to right,

    (1) The operand (number) is unconditionally pushed onto the stack

    (2) When an opcode (symbol) is encountered, two numbers are popped from the stack (the second operand is popped first, and the first operand is popped later)

Because division produces decimals, it is best to store the operands as floating-point numbers.

(The code hasn't come out yet, please post it later ( ̄︶ ̄)↗ )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161724&siteId=291194637