#计算器开发需求
#
#1、实现加减乘除及拓号优先级解析
#2、用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,
#必须自己解析里面的(),+,-,*,/符号和公式,运算后得出结果,结果必须与真实的计算器所得出的结果一致
import re
import functools
def minus_operator_handler(formula):
‘‘‘处理一些特殊的符号‘‘‘
minus_operator = re.split("-",formula)
calc_list = re.findall("[0-9]",formula)
if minus_operator[0] == ‘‘:#第一值肯定是负号
clac_list[0] = ‘-%s‘ % calc_list[0]
res = functools.reduce(lambda x,y:float(x) - float(y),calc_list)
print("\33[33;1m减号[%s]处理结果:\033[0m" % formula,res)
return res
def remove_duplicates(formula):
formula = formula.replace("++","+")
formula = formula.replace("+-","-")
formula = formula.replace("-+","-")
formula = formula.replace("--","+")
formula = formula.replace("- -","+")
return formula
def compute_mutiply_dividend(formula):
‘‘‘算乘除,传进来的是字符串‘‘‘
operators = re.findall("[*/]",formula)
calc_list = re.split("[*/]",formula)
res = None
for index,i in enumerate(calc_list):
if res:
if operators[index-1] == "*":
res *= float(i)
else operators[index-1] == "/"
res /= float(i)
else:
res = float(i)
print("\033[31;1m[%s]运算结果=\033[0m" %formula, res)
return res
def handle_minus_in_list(operators_list,calc_list):
for index,i in enumerate(calc_list):
if i == ‘‘:#它其实代表负号,改成负号
calc_list[index+1] = i + calc_list[index+1].strip()
def handle_special_occactions(plus_and_minus_operators,multiply_and_dividend):
for index,i in enumerate(multiply_and_dividend):
i = i.strip()
if i.endswith("*") or i.endswith("/"):
multiply_and_dividend[index] = multiply_and_dividend[index] + plus_and_minus_operators[index] + multiply_and_dividend[index+1]
del multiply_and_dividend[index+1]
del plus_and_minus_operators[index]
return plus_and_minus_operators,multiply_and_dividend
def compute(formula):
‘‘‘这里计算的是不带括号的公式‘‘‘
formula = formula.strip("()")#去除外面包的括号
formula = remove_duplicates(formula)#去除外重复的+-号
plus_and_minus_operators = re.findall("[+-]",formula)
multiply_and_dividend = re.split("[+-]",formula)#取出乘除公式
if len(multiply_and_dividend[0].strip()) == 0:#代表这肯定是个减号
multiply_and_dividend[1] = plus_and_minus_operators[0] + multiply_and_dividend[1]
del multiply_and_dividend[0]
del plus_and_minus_operators[0]
plus_and_minus_operators,multiply_and_dividend=handle_special_occactions(plus_and_minus_operators,multiply_and_dividend)
for index,i in enumerate(multiply_and_dividend):
if re.search("[*/]",i):
sub_res = compute_mutiply_dividend(i)
multiply_and_dividend[index] = sub_res
#开始运算+,-
print(multiply_and_dividend,plus_and_minus_operators)
total_res = None
for index,item in enumerate(multiply_and_dividend):
if total_res:#代表不是第一次循环
if plus_and_minus_operators[index-1] == ‘+‘:
total_res += float(item)
elif plus_and_minus_operators[index-1] == ‘-‘:
total_res -= float(item)
else:
total_res = float(item)
print("\033[32;1m[%s]运算结果:\033[0m" %format,total_res)
return total_res
def calc(formula):
‘‘‘计算程序入口,主要逻辑是先计算括号里的值,算出来后再计算乘除,再算加减‘‘‘
parenthesise_flag = Ture
calc_res = None#初始化运算结果为None
while parenthesise_flag:
m = re.search("\[^()*\]",formula)#找到最里层的括号
if m:
#print("先算括号里的值:",m.group())
sub_res = compute(m.group())
formula = formula.replace(m.group(),str(sub_res))
else:
print(‘\033[41;1m----没括号了...---\033[0m‘)
print(‘\n\n033[42;1m最终结果:\033[0m‘,compute(formula))
parenthesise_flag = False#代表公式里的括号已经都被剥除
if __name__ == ‘__main__‘: