需求:
实现加减乘除及拓号优先级解析
用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式
注意:不能调用eval等类似功能偷懒实现
分割线:
# -*- coding: utf-8 -*-
# @Time : 2018/10/26 22:00
# @Author : 赵伟江
importre
my_count ='1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
my_count = re.sub("\ ","",my_count)#去除字符串中所有空格
# print(my_count)
bracket = re.compile("\(([^()]+)\)")#选出最内部的括号
mul_sub = re.compile("(-?\d+)(\.\d+)?(\*-)(\d+)(\.\d+)?")#选出 a*(-b)
dev_sub = re.compile("(-?\d+)(\.\d+)?(/-)(\d+)(\.\d+)?")#选出 a/(-b)
defadd_sub(arg):
'''
将+ - 变 - ,将 - - 变 +
:paramarg:
:return:
'''
ifre.findall("\+-",arg):
arg = arg.replace("+-","-")
returnadd_sub(arg)
elifre.findall("--",arg):
arg = arg.replace("--","+")
returnadd_sub(arg)
returnarg
defmul_dev_sub(arg):
'''
定义一个将"*-"和"/-"中的"-"移动到最左边,最后将"+-"和"--"整合,返回一个没有算式符号重叠的字符串
:paramarg:
:return:
'''
ifre.findall("\*-",arg):
ret1 = mul_sub.search(arg).group()
ret2 = ret1.replace(ret1,"-"+ ret1.replace("*-","*"))
arg = arg.replace(ret1,ret2)
returnmul_dev_sub(arg)
elifre.findall("/-",arg):
res1 = dev_sub.search(arg).group()
res2 = res1.replace(res1,"-"+ res1.replace("/-","/"))
arg = arg.replace(res1,res2)
returnmul_dev_sub(arg)
returnadd_sub(arg)
my_mul = re.compile("(-?\d+)(\.\d+)?(\*)(-?\d+)(\.\d+)?")#判断有乘法或者除法
my_div = re.compile("(-?\d+)(\.\d+)?(/)(-?\d+)(\.\d+)?")#判断有乘法或者除法
my_add = re.compile("(-?\d+)(\.\d+)?(\+)(-?\d+)(\.\d+)?")#判断有两个数之间加减
my_sub = re.compile("(-?\d+)(\.\d+)?(-)(-?\d+)(\.\d+)?")#判断有两个数之间加减
deftwo_count(arg):
'''
定义一个只有数字a和数字b的加减乘除算法,返回单个计算结果
:paramarg:
:return:
'''
iflen(my_mul.findall(arg)) !=:
left_num =float(arg[:(arg.index(re.search("\*",arg).group()))])
right_num =float(arg[(arg.index(re.search("\*",arg).group())+1):])
returnstr(left_num*right_num)
eliflen(my_div.findall(arg)) !=:
left_num =float(arg[:(arg.index(re.search("/",arg).group()))])
right_num =float(arg[(arg.index(re.search("/",arg).group())+1):])
returnstr(left_num/right_num)
eliflen(my_add.findall(arg)) !=:
left_num =float(arg[:(arg.index(re.search("\+",arg).group()))])
right_num =float(arg[(arg.index(re.search("\+",arg).group())+1):])
returnstr(left_num+right_num)
eliflen(my_sub.findall(arg)) !=:
ifarg[] =="-":
arg = arg[1:]
left_num =float(arg[:(arg.index(re.search("-",arg).group()))])
right_num =float(arg[(arg.index(re.search("-",arg).group())+1):])
returnstr(-left_num-right_num)
else:
left_num =float(arg[:(arg.index(re.search("-",arg).group()))])
right_num =float(arg[(arg.index(re.search("-",arg).group())+1):])
returnstr(left_num-right_num)
mul_div = re.compile("(-?\d+)(\.\d+)?(/|\*)(-?\d+)(\.\d+)?")#判断有乘法或者除法
add_sub_re = re.compile("(-?\d+)(\.\d+)?(-|\+)(-?\d+)(\.\d+)?")#判断有两个数之间加减
defsingle_count(arg):
'''
单一字符串,包含加减乘除,返回单一字符串运行结果
:paramarg:
:return:
'''
iflen(mul_div.findall(arg)) !=:
ret1 = two_count(mul_div.search(arg).group())
arg = arg.replace(mul_div.search(arg).group(),ret1)
returnsingle_count(arg)
eliflen(add_sub_re.findall(arg)) !=:
ret1 = two_count(add_sub_re.search(arg).group())
arg = arg.replace(add_sub_re.search(arg).group(),ret1)
returnsingle_count(arg)
returnarg
defi_count(arg):
iflen(re.findall("\(",arg)) !=:
ret1 = bracket.search(arg).group()
ret2 = single_count(ret1[1:(len(ret1)-1)])
arg = arg.replace(ret1,ret2)
arg = mul_dev_sub(arg)
returni_count(arg)
eliflen(re.findall("\(",arg)) ==:
arg = single_count(arg)
returnarg
领取专属 10元无门槛券
私享最新 技术干货