圆月山庄资源网 Design By www.vgjia.com
本文实例讲述了python实现逆波兰计算表达式的方法。分享给大家供大家参考。具体分析如下:
逆波兰表达式又叫做后缀表达式。在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,所以,这种表示法也称为中缀表示。波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示表达式的方法。按此方法,每一运算符都置于其运算对象之后,故称为后缀表示。
# -*- coding: utf-8 -*- symbol_priority = {} symbol_priority[0] = ['#'] symbol_priority[1] = ['('] symbol_priority[2] = ['+', '-'] symbol_priority[3] = ['*', '/'] symbol_priority[4] = [')'] def comparePriority(symbol, RPN_stack, symbol_stack): '''Compare priority between two symbols''' global symbol_priority if len(symbol_stack) > 0: symbol_pop = symbol_stack.pop() else: return for list in symbol_priority.values(): if (symbol in list) and (symbol_pop in list): '''same priority''' symbol_stack.append(symbol_pop) symbol_stack.append(symbol) return elif symbol in list: '''symbol is smaller''' RPN_stack.append(symbol_pop) #recusion call comparePriority(symbol, RPN_stack, symbol_stack) return elif symbol_pop in list: '''symbol is bigger''' symbol_stack.append(symbol_pop) symbol_stack.append(symbol) return else: continue symbol_stack.append(symbol_pop) return def scanEveryone(input_string, RPN_stack, symbol_stack): for ch in input_string: if ch.isdigit(): RPN_stack.append(ch) else: if len(symbol_stack) > 0: if ch == '(': symbol_stack.append(ch) elif ch == ')': while True: symbol_pop = symbol_stack.pop() if symbol_pop == '(': break else: RPN_stack.append(symbol_pop) else: comparePriority(ch, RPN_stack, symbol_stack) else: symbol_stack.append(ch) def scanInput(RPN_stack, symbol_stack): input_string = raw_input() input_string += '#' scanEveryone(input_string, RPN_stack, symbol_stack) def calRPN(RPN_stack): value_stack = [] RPN_stack.append('#') for value in RPN_stack: if value == '#': return value_stack.pop() break if value.isdigit(): value_stack.append(value) else: right_value = value_stack.pop() left_value = value_stack.pop() cal_string = left_value + value + right_value value_stack.append(str(eval(cal_string))) def main(): RPN_stack = [] symbol_stack = [] scanInput(RPN_stack, symbol_stack) print calRPN(RPN_stack) if __name__ == '__main__': main()
calRPN.py
# -*- coding: utf-8 -*- symbol_priority = {} symbol_priority[0] = ['#'] symbol_priority[1] = ['('] symbol_priority[2] = ['+', '-'] symbol_priority[3] = ['*', '/'] symbol_priority[4] = [')'] def comparePriority(symbol, RPN_stack, symbol_stack): '''Compare priority between two symbols''' global symbol_priority if len(symbol_stack) > 0: symbol_pop = symbol_stack.pop() else: return for list in symbol_priority.values(): if (symbol in list) and (symbol_pop in list): '''same priority''' symbol_stack.append(symbol_pop) symbol_stack.append(symbol) return elif symbol in list: '''symbol is smaller''' RPN_stack.append(symbol_pop) #recusion call comparePriority(symbol, RPN_stack, symbol_stack) return elif symbol_pop in list: '''symbol is bigger''' symbol_stack.append(symbol_pop) symbol_stack.append(symbol) return else: continue symbol_stack.append(symbol_pop) return def scanEveryone(input_string, RPN_stack, symbol_stack): for ch in input_string: if ch.isdigit(): RPN_stack.append(ch) else: if len(symbol_stack) > 0: if ch == '(': symbol_stack.append(ch) elif ch == ')': while True: symbol_pop = symbol_stack.pop() if symbol_pop == '(': break else: RPN_stack.append(symbol_pop) else: comparePriority(ch, RPN_stack, symbol_stack) else: symbol_stack.append(ch) def scanInput(RPN_stack, symbol_stack): input_string = raw_input() input_string += '#' scanEveryone(input_string, RPN_stack, symbol_stack) def calRPN(RPN_stack): value_stack = [] RPN_stack.append('#') for value in RPN_stack: if value == '#': return value_stack.pop() break if value.isdigit(): value_stack.append(value) else: right_value = value_stack.pop() left_value = value_stack.pop() cal_string = left_value + value + right_value value_stack.append(str(eval(cal_string))) def main(): RPN_stack = [] symbol_stack = [] scanInput(RPN_stack, symbol_stack) print calRPN(RPN_stack) if __name__ == '__main__': main()
希望本文所述对大家的Python程序设计有所帮助。
标签:
python,逆波兰,表达式
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]