Data structure and algorithm _ stack-inverse Polish expression evaluation _ python implementation

First tell you two knowledge points:

  • The prefix expression is also known as Polish. The operator of the prefix expression is located before the operand. The expression here is the mathematical expression that we usually use for mathematical operations.
  • The suffix expression is the inverse Polish expression mentioned in the title. It is different from the usual place of the operation symbol between the operands. The suffix expression places the operator after the operand.

For example: 6-1 + 3 * 2 = 11 is an infix expression.

Corresponding to Polish, the prefix expression is: ['-', '6', '1', '*', '+', '3', '2']

Corresponding inverse Polish, the suffix expression is: ['6', '1', '-', '3', '2', '*', '+']

Three expression evaluation methods

The computer expression of the prefix expression
scans the expression from right to left. When it encounters a number, it pushes the number onto the stack. When it encounters an operator, it pops up the two numbers on the top of the stack, and uses the operator to calculate them accordingly (stack The top element and the second top element), and put the result on the stack; repeat the above process until the leftmost end of the expression, and the value of the final operation is the result of the expression

For example : (3 + 4) × 5-6 The corresponding prefix expression is-× + 3 4 5 6. The steps for evaluating the prefix expression are as follows:

  1. Scan from right to left and push 6, 5, 4, 3 onto the stack
  2. Encounter the + operator, so pop 3 and 4 (3 is the top element of the stack, 4 is the second top element), calculate the value of 3 + 4, get 7, and then put 7 on the stack
  3. Next is the × operator, so pop 7 and 5, calculate 7 × 5 = 35, and push 35 onto the stack
  4. Finally, the-operator calculates the value of 35-6, which is 29, which gives the final result

Infix expression

  1. Infix expressions are common arithmetic expressions, such as (3 + 4) × 5-6

  2. The evaluation of infix expressions is the most familiar to us, but it is not easy to operate for computers (this problem can be seen in the case we talked about earlier). The expression is converted into other expressions to operate (generally converted into suffix expressions.)

Suffix expression

  1. Suffix expression is also called inverse Polish expression, similar to prefix expression, except that the operator is after the operand

  2. For example: (3 + 4) × 5-6 The corresponding suffix expression is 3 4 + 5 × 6 –

  3. Another example:
    Insert picture description here

The computer evaluation of the suffix expression
scans the expression from left to right. When it encounters a number, it pushes the number onto the stack. When it encounters an operator, it pops up the two numbers on the top of the stack and uses the operator to calculate them accordingly ( The second top element and the top element of the stack), and put the result on the stack; repeat the above process until the far right end of the expression, the value obtained by the final operation is the result of the expression

For example: (3 + 4) × 5-6 The corresponding suffix expression is 3 4 + 5 × 6-, the evaluation steps for the suffix expression are as follows:

  1. Scan from left to right and push 3 and 4 onto the stack;
  2. When the + operator is encountered, 4 and 3 are popped (4 is the top element of the stack, 3 is the second top element), the value of 3 + 4 is calculated, and 7 is obtained, and then 7 is pushed onto the stack;
  3. Put 5 on the stack;
  4. Next is the × operator, so 5 and 7 are popped, 7 × 5 = 35 is calculated, and 35 is pushed onto the stack;
  5. Put 6 on the stack;
  6. Finally, the-operator calculates the value of 35-6, which is 29, which gives the final result

It can be seen from the evaluation methods of the above three expressions that the computer implementation of the suffix expression is the most convenient. Using a stack structure for calculation is easy to implement.

Convert infix expression to suffix expression

This is the most complicated step to realize the operation of ordinary expressions. The specific algorithm involves many details:

  1. Initialize two stacks: the operator stack s1 and the stack s2 that stores the intermediate results;

  2. Scan infix expressions from left to right;

  3. When encountering an operand, press it to s2;

  4. When encountering an operator, compare its priority with the top operator of s1 stack

  5. If s1 is empty, or the operator at the top of the stack is the left bracket "(", then this operator is directly pushed onto the stack;

    Otherwise, if the priority is higher than the top-of-stack operator, the operator is also pushed into s1;
    otherwise, the operator on the top of s1 is popped and pushed into s2, and then go to (4-1) and s1 again. The top of the stack operator is compared;

  6. When encountering parentheses: (1) If it is the left parenthesis "(", then press s1 directly (2)
    If it is the right parenthesis ")", then the operator at the top of the s1 stack is popped in sequence, and s2 is pressed until it encounters Up to the left parenthesis, then discard the pair of parentheses and repeat steps 2 to 5 until the far right of the expression

  7. Pop the remaining operators in s1 in sequence and press into s2

  8. The elements in s2 are popped in turn and output, and the reverse order of the result is the suffix expression corresponding to the infix expression

    Specific code:

expression = "6-1+3*2"#中缀表达式
print("原式为中缀表达式 :"+expression)
Expression=list(expression)
s1 = stack()
s2 = list()
oper = Operation()
for item in list(Expression):
    if re.findall(r'\d+',item):
        s2.append(item)
    elif item=='(':
        s1.push(item)
    elif item==')':
        while (s1.peek()!='('):
            s2.append(s1.pop())
        s1.pop()
    else:
        while s1.size()!=0 and oper.getValue(s1.peek())>=oper.getValue(item):
            s2.append(s1.pop())
        s1.push(item)
while s1.size()!=0:
    s2.append(s1.pop())
    
print("转化得到的后缀表达式列表:",end='')
print(s2)

Calculate the result of the suffix expression

This step is relatively simple, you only need to initialize a stack and judge the suffix expressions one by one. The specific algorithm is as follows:
1) Initialize a stack, named stack
2) Use the loop structure to scan the incoming suffix expression expression
3) Determine whether each cycle is a number:
if it is a number, it is pushed into the stack.
If not, it is judged For which operator, perform the corresponding operation and assign the result to result
4) Only one value will be left in the stack at the end, and this value is the operation result of the suffix expression.
Specific code:

'''
计算后缀表达式的结果
'''
#suffixExpression = "10 4 + 5 * 6 -" 
#split = suffixExpression.split(' ')
#stack = []
s = stack()
for item in list(s2):
    if re.findall(r'\d+',item):
        s.push(item)
    else:
        num2 =int( s.pop())
        num1 =int( s.pop())
        res = 0
        if item=='+':
            res = num1 + num2
        elif item=='-':
            res = num1 - num2
        elif item=='*':
            res = num1 * num2
        elif item=='/':
            res = num1 / num2
        else:
            print("符号错误")
        s.push(''+str(res))
        
print("计算得后缀表达式得: %s = %d"%(expression,int(s.pop())))
Published 27 original articles · praised 2 · visits 680

Guess you like

Origin blog.csdn.net/qq_44273739/article/details/105098026