本文实例讲述了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程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】