Thought analysis and code implementation of infix to suffix expression

Time:2021-4-17

Infix expression to suffix expression

As you can see, the suffix expression is suitable for calculation, but it is not easy for people to write it, especially when the expression is very long. Therefore, in development, we need to convert infix expression into suffix expression.

The specific steps are as follows:

  1. Initialize two stacks: the operator stack S1 and the stack S2 storing the intermediate results;
  2. Scan infix expression from left to right;
  3. When the operands are encountered, they are pressed to S2;
  4. When an operator is encountered, compare its priority with the top of S1 stack operator:
    1. If S1 is empty or the top of stack operator is left bracket “(“), the operator will be put on the stack directly;
    2. Otherwise, if the priority is higher than that of the top of stack operator, the operator will also be pushed into S1;
    3. Otherwise, the operator at the top of S1 stack will pop up and press into S2, and then go to (4-1) again to compare with the new operator at the top of S1 stack;
  5. When encountering brackets: (1) if it’s left bracket “(“), press S1 directly; (2) if it’s right bracket “)”, pop up the operator at the top of S1 stack in turn, and press S2 until encountering left bracket, then discard this pair of brackets
  6. Repeat steps 2 through 5 until the far right of the expression
  7. Pop up the remaining operators in S1 and press them into S2
  8. Pop up the elements in S2 in turn and output, the reverse order of the result is the suffix expression corresponding to infix expression

Thought analysis and code implementation of infix to suffix expression

>

For example:

The process of transforming infix expression “1 + ((2 + 3) × 4) – 5” into suffix expression is as follows

So the result is"1 2 3 + 4 × + 5 –"

Elements scanned S2 (bottom of stack to top of stack) S1 (bottom of stack to top of stack) explain
1 1 empty Digital, direct stack
+ 1 + S1 is empty and the operator is put on the stack directly
( 1 + ( Left bracket, direct stack
( 1 + ( ( ditto
2 1 2 + ( ( number
+ 1 2 + ( ( + The top of S1 stack is the left bracket, and the operator is directly put on the stack
3 1 2 3 + ( ( + number
) 1 2 3 + + ( Right bracket, pop-up operator until left bracket is encountered
× 1 2 3 + + ( × The top of S1 stack is the left bracket, and the operator is directly put on the stack
4 1 2 3 + 4 + ( × number
) 1 2 3 + 4 × + Right bracket, pop-up operator until left bracket is encountered
1 2 3 + 4 × + -It has the same priority as + so it ejects + and presses in again-
5 1 2 3 + 4 × + 5 number
To the far right 1 2 3 + 4 × + 5 – empty The remaining operators in S1

Thought analysis and code implementation of infix to suffix expression

As for how to think of this, you have to ask the bald guys

First write a method to convert the infix expression to the corresponding list

//Method: convert infix expression to corresponding list

I’m writing a method

//That is, ArrayList [1, +, (, (, 2, +, 3,), *, 4,), -, 5] = > ArrayList [1,2,3, +, 4, *, +, 5, –]

S2 this stack, there is no pop-up stack operation, so do not worry

Here we use ArrayList to replace it, that is, oh

In a lot of books, he is trying to explain the stack, he is

If we insist on using stack, it’s just ridiculous. Here we can use it flexibly, eh

This work adoptsCC agreementReprint must indicate the author and the link of this article

Autumn leaves and summer wind

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]