做逆波兰表达式的时候,代码运行后什么反应也没有,没有报错没有终止,shell里面可以输入,但是也没有任何反应,怎么回事呢?
class stack:
item = None
def __init__(self):
self.item = []
def push(self, i):
self.item.append(i)
def pop(self):
while not self.empty():
return self.item.pop()
def top(self):
try:
return self.item[-1]
except:
return None
def empty(self):
return len(self.item) == 0
stack1 = stack()
stack2 = stack()
formula = 'a+b'
deal_formula = list(formula)
for i in deal_formula:
if i == "(":
stack1.push(i)
elif i == ")":
while stack1.top() != "(":
stack2.push(stack1.top())
stack1.pop()
else:
stack1.pop()
continue
elif i == "*" or "/":
while stack1.top() == '*' or '/':
stack2.push(stack1.top())
stack1.pop()
else:
stack1.push(i)
continue
elif i == "+" or "-":
while stack1.top() == "(" or None:
stack1.push(i)
continue
else:
stack2.push(stack1.top())
stack1.pop()
else:
stack2.push(i)
while not stack1.empty():
stack2.push(stack1.top())
stack1.pop()
print stack2.item
if not stack2.item:
print 'wd'
else:
print '??'