#还有需要优化的
#!/usr/bin/env python3
Example = '1-2*((60-30+(9-2*5/-3+7/3*99/4*2998+10*568/14)*(-40/5))-(-4*3)/(16-3*2))'
import re
# 乘法函数
def multiplication():
value = 0
for n, i in enumerate(new_value):
if n != 0:
fa = '%.2f'%(float(new_value[n-1]))
fb = '%.2f'%(float(new_value[n]))
value = '%.2f'%(float(fa) * float(fb))
return value
# 除法函数
def division():
value = 0
for n, i in enumerate(new_value):
if n != 0:
fa = '%.2f'%(float(new_value[n - 1]))
fb = '%.2f'%(float(new_value[n]))
value = '%.2f'%(float(fa) / float(fb))
return value
# 加法函数
def addition():
value =0
for n, i in enumerate(new_value):
if n != 0:
fa = '%.2f'%(float(new_value[n - 1]))
fb = '%.2f'%(float(new_value[n]))
value = '%.2f'%(float(fa) + float(fb))
return value
# 减法函数
def subtraction():
value = 0
for n, i in enumerate(new_value):
if n != 0:
fa = '%.2f'%(float(new_value[n - 1]))
fb = '%.2f'%(float(new_value[n]))
value = '%.2f'%(float(fa) - float(fb))
return valuewhile True:
brackets = re.search("\([^()]*\)",Example) #匹配括号内容,'\('中\表示转义,serach函数匹配所有字符串
if brackets:
take_off_brackets = brackets.group().strip('()')#脱掉括号
while True:
element = re.findall('-?\d+\.\d+|\d+', take_off_brackets)
if len(element) > 1: #计算take_off_brackets中的元素数量是否大于1
print(take_off_brackets)
if '*' in take_off_brackets or '/' in take_off_brackets: #判断字符串中带有'*'或者'/'
multiplication_and_division = re.search('(\d+\.\d+|\d+)(\*|\/)(-?\d+\.\d+|-?\d+)',take_off_brackets)#匹配字符串中的乘法和除法,
# 其中'\d+\.\d+'表示匹配浮点,'\d+'表示整数,(\d+\.\d+|\d+)表示匹配浮点或者整数,括号表示这是一组,(\*|\/)表示匹配'*'或者'/',
# (-?\d+\.\d+|-?\d+)中-?\d+\.\d+表示正浮点数或者负浮点数,-?\d+表示正数或者负数
if multiplication_and_division:
mul_and_div = multiplication_and_division.group()
if '*' in mul_and_div:
new_value = mul_and_div.split('*') #将mul_and_div以*隔开生成列表
replace = multiplication()
take_off_brackets = re.sub('(\d+\.\d+|\d+)\*(-?\d+\.\d+|-?\d+)',str(replace),take_off_brackets,1)
# print(take_off_brackets)
continue
elif '/' in mul_and_div:
new_value = mul_and_div.split('/')
replace = division()
take_off_brackets = re.sub('(\d+\.\d+|\d+)\/(-?\d+\.\d+|-?\d+)',str(replace),take_off_brackets,1)
# print(take_off_brackets)
continue
else:
break
else:
break
elif '+' in take_off_brackets or '-' in take_off_brackets:
addition_and_subtraction = re.search('(\d+\.\d+|\d+)(\+|\-)(-?\d+\.\d+|-?\d+)',take_off_brackets)
if addition_and_subtraction:
add_and_sub = addition_and_subtraction.group()
if '+' in add_and_sub:
if '++' in add_and_sub:
new_value = add_and_sub.split('+', 1)
replace = addition()
take_off_brackets = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1)
# print(take_off_brackets)
continue
else:
new_value = add_and_sub.split('+')
replace = addition()
take_off_brackets = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1)
# print(take_off_brackets)
continue
elif '-' in add_and_sub:
if '--' in add_and_sub:
new_value = add_and_sub.split('-', 1)
replace = subtraction()
take_off_brackets = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1)
# print(take_off_brackets)
continue
else:
new_value = add_and_sub.split('-')
replace = subtraction()
take_off_brackets = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1)
# print(take_off_brackets)
continue
else:
break
else:
break
else:
break
else:
# take_off_brackets = brackets.group().strip('()')
aa = re.sub('\+','\+',brackets.group()) #将字符串中的'+'、'-'、'*'、'/'都加上转义符'/'这样替换的时候才能找到
bb = re.sub('\-','\-',aa)
cc = re.sub('\*','\*',bb)
dd = re.sub('\/','\/',cc)
Example = re.sub(dd, str(take_off_brackets).strip('()'), Example, 1)
brackets = re.search("\([^()]*\)", Example)
tt = re.sub('\(','\(',brackets.group())
yy = re.sub('\)','\)',tt)
Example = re.sub(yy,take_off_brackets,Example)
print(Example)
break
else:
while True:
if '*' in Example or '/' in Example:
multiplication_and_division = re.search('(\d+\.\d+|\d+)(\*|\/)(-?\d+\.\d+|-?\d+)', Example)
if multiplication_and_division:
mul_and_div = multiplication_and_division.group()
if '*' in mul_and_div:
new_value = mul_and_div.split('*') #将mul_and_div以*隔开生成列表
replace = multiplication()
Example = re.sub('(\d+\.\d+|\d+)\*(-?\d+\.\d+|-?\d+)',str(replace),Example,1)
print(Example)
continue
elif '/' in mul_and_div:
new_value = mul_and_div.split('/')
replace = division()
Example = re.sub('(\d+\.\d+|\d+)\/(-?\d+\.\d+|-?\d+)',str(replace),Example,1)
print(Example)
continue
else:
break
else:
break
elif '+' in Example or '-' in Example:
addition_and_subtraction = re.search('(\d+\.\d+|\d+)(\+|\-)(-?\d+\.\d+|-?\d+)', Example)
if addition_and_subtraction:
add_and_sub = addition_and_subtraction.group()
if '+' in add_and_sub:
if '++' in add_and_sub:
new_value = add_and_sub.split('+', 1)
replace = addition()
Example = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), Example, 1)
print(Example)
continue
else:
new_value = add_and_sub.split('+')
replace = addition()
Example = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), Example, 1)
print(Example)
continue
elif '-' in add_and_sub:
if '--' in add_and_sub:
new_value = add_and_sub.split('-', 1)
replace = subtraction()
Example = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), Example, 1)
print(Example)
continue
else:
new_value = add_and_sub.split('-')
replace = subtraction()
Example = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), Example, 1)
print(Example)
continue
else:
break
else:
break
0