|
|
|
@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
# 四则运算行棋 计算器
|
|
|
|
|
"""|||mid_to_aft:将中缀表达式转化为后缀表达式l;
|
|
|
|
|
aft_to_cal:有参数l,将后缀表达式进行计算;
|
|
|
|
|
mid_to_cal:直接将中缀表达式进行计算。|||"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Calculator(object): # 用一个类应用于计算
|
|
|
|
|
def __init__(self, equ):
|
|
|
|
|
self.s = equ
|
|
|
|
|
|
|
|
|
|
def mid_to_aft(self):
|
|
|
|
|
pref = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3} # 符号字典
|
|
|
|
|
l = ''
|
|
|
|
|
A = []
|
|
|
|
|
i = 0
|
|
|
|
|
while True:
|
|
|
|
|
loc = self.s[i]
|
|
|
|
|
# 可能是一个两位以上的数字
|
|
|
|
|
if loc in list('0123456789'):
|
|
|
|
|
while i < len(self.s) - 1 and self.s[i + 1] in list('0123456789'): # 不是最后一个数字且为连续数字
|
|
|
|
|
loc += self.s[i + 1]
|
|
|
|
|
i += 1 # 刚好到连续的最后一个数字
|
|
|
|
|
l = l + loc + ' '
|
|
|
|
|
elif loc in list('()+-*/^'):
|
|
|
|
|
if A == [] or loc == '(':
|
|
|
|
|
A.append(loc)
|
|
|
|
|
elif loc == ')':
|
|
|
|
|
while A[-1] != '(':
|
|
|
|
|
l = l + A.pop() + ' '
|
|
|
|
|
A.pop()
|
|
|
|
|
elif A[-1] == '(':
|
|
|
|
|
A.append(loc)
|
|
|
|
|
else:
|
|
|
|
|
if pref[loc] > pref[A[-1]]:
|
|
|
|
|
A.append(loc)
|
|
|
|
|
else:
|
|
|
|
|
while A[-1] != '(':
|
|
|
|
|
l = l + A.pop() + ' '
|
|
|
|
|
if A == []:
|
|
|
|
|
break
|
|
|
|
|
A.append(loc)
|
|
|
|
|
i += 1
|
|
|
|
|
if i == len(self.s):
|
|
|
|
|
while A != []:
|
|
|
|
|
if len(A) == 1:
|
|
|
|
|
l += A.pop()
|
|
|
|
|
else:
|
|
|
|
|
l = l + A.pop() + ' '
|
|
|
|
|
break
|
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
|
def aft_to_cal(self, l):
|
|
|
|
|
A = []
|
|
|
|
|
i = 0
|
|
|
|
|
while True:
|
|
|
|
|
# print('第{}个元素:A = '.format(i+1),A)
|
|
|
|
|
loc = l[i]
|
|
|
|
|
if loc in list('0123456789'):
|
|
|
|
|
while l[i + 1] in list('0123456789'): # 此时肯定是数字
|
|
|
|
|
loc += l[i + 1]
|
|
|
|
|
i = i + 1
|
|
|
|
|
A.append(float(loc))
|
|
|
|
|
i += 1
|
|
|
|
|
elif loc == ' ':
|
|
|
|
|
i += 1
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
if loc == '+':
|
|
|
|
|
A.append(A.pop() + A.pop())
|
|
|
|
|
elif loc == '-':
|
|
|
|
|
A.append(-A.pop() + A.pop())
|
|
|
|
|
elif loc == '*':
|
|
|
|
|
A.append(A.pop() * A.pop())
|
|
|
|
|
elif loc == '/':
|
|
|
|
|
A.append((1 / A.pop()) * A.pop())
|
|
|
|
|
else:
|
|
|
|
|
last = A.pop()
|
|
|
|
|
A.append(pow(A.pop(), last))
|
|
|
|
|
i += 1
|
|
|
|
|
if i >= len(l) - 1:
|
|
|
|
|
break
|
|
|
|
|
result = A.pop()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def mid_to_cal(self):
|
|
|
|
|
pref = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}
|
|
|
|
|
l = ''
|
|
|
|
|
A = []
|
|
|
|
|
i = 0
|
|
|
|
|
while True:
|
|
|
|
|
loc = self.s[i]
|
|
|
|
|
# 可能是一个两位以上的数字
|
|
|
|
|
if loc in list('0123456789'):
|
|
|
|
|
while i < len(self.s) - 1 and self.s[i + 1] in list('0123456789'): # 不是最后一个数字且为连续数字
|
|
|
|
|
loc += self.s[i + 1]
|
|
|
|
|
i += 1 # 刚好到连续的最后一个数字
|
|
|
|
|
l = l + loc + ' '
|
|
|
|
|
elif loc in list('()+-*/^'):
|
|
|
|
|
if A == [] or loc == '(':
|
|
|
|
|
A.append(loc)
|
|
|
|
|
elif loc == ')':
|
|
|
|
|
while A[-1] != '(':
|
|
|
|
|
l = l + A.pop() + ' '
|
|
|
|
|
A.pop()
|
|
|
|
|
elif A[-1] == '(':
|
|
|
|
|
A.append(loc)
|
|
|
|
|
else:
|
|
|
|
|
if pref[loc] > pref[A[-1]]:
|
|
|
|
|
A.append(loc)
|
|
|
|
|
else:
|
|
|
|
|
while A[-1] != '(':
|
|
|
|
|
l = l + A.pop() + ' '
|
|
|
|
|
if A == []:
|
|
|
|
|
break
|
|
|
|
|
A.append(loc)
|
|
|
|
|
i += 1
|
|
|
|
|
if i == len(self.s):
|
|
|
|
|
while A != []:
|
|
|
|
|
if len(A) == 1:
|
|
|
|
|
l += A.pop()
|
|
|
|
|
else:
|
|
|
|
|
l = l + A.pop() + ' '
|
|
|
|
|
break
|
|
|
|
|
i = 0
|
|
|
|
|
while True:
|
|
|
|
|
loc = l[i]
|
|
|
|
|
if loc in list('0123456789'):
|
|
|
|
|
while l[i + 1] in list('0123456789'): # 此时肯定是数字
|
|
|
|
|
loc += l[i + 1]
|
|
|
|
|
i = i + 1
|
|
|
|
|
A.append(float(loc))
|
|
|
|
|
i += 1
|
|
|
|
|
elif loc == ' ':
|
|
|
|
|
i += 1
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
if loc == '+':
|
|
|
|
|
A.append(A.pop() + A.pop())
|
|
|
|
|
elif loc == '-':
|
|
|
|
|
A.append(-A.pop() + A.pop())
|
|
|
|
|
elif loc == '*':
|
|
|
|
|
A.append(A.pop() * A.pop())
|
|
|
|
|
elif loc == '/':
|
|
|
|
|
A.append((1 / A.pop()) * A.pop())
|
|
|
|
|
else:
|
|
|
|
|
last = A.pop()
|
|
|
|
|
A.append(pow(A.pop(), last))
|
|
|
|
|
i += 1
|
|
|
|
|
if i >= len(l) - 1:
|
|
|
|
|
break
|
|
|
|
|
result = A.pop()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''def eval(expression):
|
|
|
|
|
c = eval(expression)
|
|
|
|
|
return c'''
|
|
|
|
|
|
|
|
|
|
'''中缀转后缀计算器设计结束'''
|