python数据结构之转后缀表达式计算(栈的应用)
此只支持十以内的计算,所以如果需要通用的话还需改进!!!
from Stack import *
def funcations(n):
po=[]
stack=Stack()
for i in range(len(n)):
po.append(n[i])
for token in po:
if token in ‘0123456789‘:
stack.push(int(token))
else:
operation_1=stack.pop()
operation_2=stack.pop()
result=math(operation_1,operation_2,token)
stack.push(int(result))
return stack.get_stack()
def math(op1,op2,token):
if token==‘+‘:
return op1+op2
elif token==‘-‘:
return op2-op1
elif token==‘*‘:
return op1*op2
else:
return op2/op1
if __name__ == ‘__main__‘:
n=‘543*-‘
print(funcations(n)) 相关推荐
qidiantianxia 2020-10-21
wolfjin 2020-09-10
HMHYY 2020-06-28
苦咖啡flask 2020-06-18
playis 2020-06-16
xuanwenchao 2020-06-14
sqliang 2020-06-14
TesterJingel 2020-06-10
RocketJ 2020-06-09
powderhose 2020-06-08
rongxionga 2020-06-08
Burgesszheng 2020-06-07
huangyx 2020-05-29
RuoShangM 2020-05-14
数据库之扑朔迷离 2020-05-06
huanghong 2020-05-03