python 逆波兰式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 逆波兰式相关的知识,希望对你有一定的参考价值。

逆波兰式,也叫后缀表达式

技巧:为简化代码,引入一个不存在的运算符#,优先级最低。置于堆栈底部

 

class Stack(object):
    ‘‘‘堆栈‘‘‘
    def __init__(self):
        self._stack = []
        
    def pop(self):
        return self._stack.pop()
    
    def push(self, x):
        self._stack.append(x)
    

 

 一、表达式无括号

def solve(bds):
    ‘‘‘不带括号,引入#运算符‘‘‘
    pro = dict(zip(^*/+-#, [3,2,2,1,1,0]))
    out = []
    s = Stack()
    s.push(#)
    for x in bds:
        if x in ^*/+-:
            t = s.pop()
            while pro[x] <= pro[t]:
                out.append(t)
                t = s.pop()

            s.push(t)
            s.push(x)
        else:
            out.append(x)
        
    while not s.is_null():
        out.append(s.pop())
        
    return out[:-1]

bds1 = a+b/c^d-e# abcd^/+e-
print(bds1, ‘‘.join(solve(bds1)))

 

二、表达式有括号

def solve(bds):
    ‘‘‘带括号,引入#运算符‘‘‘
    pro = dict(zip(^*/+-#, [3,2,2,1,1,0]))
    out = []
    s = Stack()
    s.push(#)
    for x in bds:
        if x == (:            # ①左括号 -- 直接入栈
            s.push(x)
        elif x == ):          # ②右括号 -- 输出栈顶,直至左括号(舍弃)
            t = s.pop()
            while t != (:
                out.append(t)
                t = s.pop()
        elif x in ^*/+-:      # ③运算符 -- 从栈顶开始,优先级不小于x的都依次弹出;然后x入栈
            while True:
                t = s.pop()
                if t == (:    # 左括号入栈前优先级最高,而入栈后优先级最低!
                    s.push(t)
                    break
                if pro[x] <= pro[t]:
                    out.append(t)
                else:
                    s.push(t)
                    break
            s.push(x)
        else:                   # ④运算数 -- 直接输出
            out.append(x)
        
    while not s.is_null():
        out.append(s.pop())
        
    return out[:-1]
        
bds1 = a+b/c^d-e          # abcd^/+e-
bds2 = (a+b)*c-(d+e)/f    # ab+c*de+f/-

print(bds1, ‘‘.join(solve(bds1)))
print(bds2, ‘‘.join(solve(bds2)))

 

以上是关于python 逆波兰式的主要内容,如果未能解决你的问题,请参考以下文章