码迷,mamicode.com
首页 > 编程语言 > 详细

python 逆波兰式

时间:2016-02-04 18:31:07      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

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

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

 

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 逆波兰式

标签:

原文地址:http://www.cnblogs.com/hhh5460/p/5182081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!