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)))
三、根据后缀表达式求值
def solve5(bds):
'''根据后缀表达式求值'''
jishuan = {
'^': lambda x,y: x**y,
'*': lambda x,y: x*y,
'/': lambda x,y: x/y,
'+': lambda x,y: x+y,
'-': lambda x,y: x-y
}
s = Stack()
for x in bds:
if x in '^*/+-':
num2, num1 = s.pop(), s.pop()
r = jishuan[x](float(num1), float(num2))
s.push(r)
else:
s.push(x) return s.pop() bds1 = '2+9/3^2-5' # 2932^/+5- -2
bds2 = '(1+2)*3-(4+5)/6' # ab+c*de+f/- 7.5 print(bds1, '=', solve5(solve(bds1)))
print(bds2, '=', solve5(solve(bds2))) #print(bds1, '=', eval(bds1))
print(bds2, '=', eval(bds2))
python 逆波兰式的更多相关文章
- javascript:逆波兰式表示法计算表达式结果
逆波兰式表示法,是由栈做基础的表达式,举个例子: 5 1 2 + 4 * + 3 - 等价于 5 + ((1 + 2) * 4) - 3 原理:依次将5 1 2 压入栈中, 这时遇到了运算符 + ...
- Haskell解决逆波兰式
摘自<Haskell趣学指南- Learn You a Haskell for Great Good> {- 逆波兰式(revese polish notation, RPN): 操作符出 ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- HDU1237 简单的计算器 【堆】+【逆波兰式】
简单的计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- c# 逆波兰式实现计算器
语文不好,不太会组织语言,希望不要太在意. 如题,先简要介绍一下什么是逆波兰式 通常我们在写数学公式的时候 就是a+b+c这样,这种表达式称为中缀表达式,逆波兰式又称为后缀表达式,例如a+b 后缀 ...
- codechef Transform the Expression 转换成逆波兰式
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- NYOJ 35 表达式求值(逆波兰式求值)
http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- HDU1237 简单计算器 【栈】+【逆波兰式】
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
随机推荐
- 数据类型.md
数据类型 整型 数据类型 含义(有符号) tinyint(m) 1个字节 范围(-128~127) smallint(m) 2个字节 范围(-32768~32767) mediumint(m) 3个字 ...
- linux 根据端口号查看占用进程的pid
1.netstat -nap | grep 端口号 2.第一种的可以查看端口号对应的pid,但是会出现pid为空的情况,这种时候用:lsof -i:端口号 3.根据pid查看端口号 netstat - ...
- MyBatis(3)-映射文件
本次博文有疑问,请先看MyBatis(1)-简单入门 和 MyBatis(2)-全局配置文件! 如在有疑问,请留言或者咨询博主,博主每天都在!谢谢! 映射文件: 主要是在xxxmapper.xml文件 ...
- MAC 下 安装redis 并配置 php redis 扩展
下载 redis redis-3.1.2.tgz sudo tar -xzf redis-3.1.2.tgz cd redis-3.1.2 sudo phpize grep: /usr/include ...
- SpringBoot实战(一)之构建RestFul风格
RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例 论文可参考:https://www.ics.uci.edu/~fielding/pubs/di ...
- SQL server数据异地备份到OSS
背景需求: 某部门需要将windows机器上的SQL server数据做一个异地备份,经过对现有的软硬件资源评估,决定使用阿里云的OSS存储 方法:利用SQLserver自带的维护计划做指定数据库的备 ...
- PAT——1043. 输出PATest
给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按“PATestPATest....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...
- 【luogu P1156 垃圾陷阱】 题解
题目链接:https://www.luogu.org/problemnew/show/P1156 设\(dp[i][j]\)表示前i堆到达高度j时的所活最长时间 那么一旦到当前状态能到达满足的时间和高 ...
- [LuoguP4711]分子质量(小模拟+玛丽题)
--这个题我居然可以把他\(1A\)--真是让我不知其可\(qwq\) \(Link\) \(emmmm\)好像发现了什么固定的套路(?)-- 大概就是这种题总会有 1.读入数 方案:快读即可. 2. ...
- HDU 2087 剪花布条 (简单KMP或者暴力)
剪花布条 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...