正则表达式实现计算器

#_author:来童星
#date:2019/11/10
import re
source='1-2 * ((60-30+(-40 / 5) * (9-2 * 5 / 3+ 7/3 * 99/4 *2998+10*568/14))-(-4*3)/(16-3*2))'
#做检查
def check(s):
flag=True
if re.findall('[a-zA-Z]',source):#if re.findall('[a-z]',s.lower()):
print('表达式错误,包含非法字符')
flag=False
if not s.count('(')==s.count(')'):
print('表达式错误,括号未闭合')
flag=False
return flag
#做格式化处理
def format(s):
s=s.replace(' ','')
s=s.replace('++','+')
s=s.replace('-+','-')
s = s.replace('++', '+')
s = s.replace('+-', '-')
s = s.replace('--', '+')
s = s.replace('*+', '*')
s = s.replace('/+', '/')
return s
def cal_mul_div(s):#(2+0.5*3.9)
# 从字符串中获取一个乘法或除法的表达式
regular='[\-]?\d+\.?\d* [*/] [\-]?\d+\.?\d*'
#(30+6*2+9/3)
#如果还能找到乘或除法表达式
while re.search(regular,s):# 或者re.findall()都可以
# 获取表达式
express=re.search(regular,s).group()# (1)6*2 (2) 9/3
#检测是乘法还是除法
if express.count('*')==1 :
#获取要计算的两个数
x, y = express.split('*')
#计算结果
mul_result=str(float(x) * float(y))
#将计算的表达式替换为计算结果值
s=s.replace(express,mul_result)# s=(30+12+9/3)
#格式化
s = format(s)
#如果是除法
if express.count('/'):
# 获取要计算的两个数
x, y = express.split('/')
# 计算结果
div_result = str(float(x) / float(y))
s=s.replace(express, div_result)# s=(30+12+3)
s=format(s)
return s # s=(30+12+3)

def cal_add_sub(s):#(30+12-23+24-3)
regular_add='[\-]?\d+\.?\d* + [\-]?\d+\.?\d*'
regular_sub='[\-]?\d+\.?\d* - [\-]?\d+\.?\d*'
#开始加法,一定要加上while循环,如果不加只能处理一次
while re.findall(regular_add,s):
#把所有的加法都算完,获取所有加法表达式
add_list=re.findall(regular_add,s)
for add_str in add_list:
#获取两个加法的数
x, y = add_str.split('+')
add_result='+'+str(float(x)+float(y))
s=s.replace(add_str,add_result)
s=format(s)
# 开始减法
while re.findall(regular_sub, s):
# 把所有的减法都算完,获取所有减法表达式
sub_list = re.findall(regular_sub, s)
for sub_str in sub_list:
numbers=sub_str.split('-')
#-3-5的情况split会返回3个值
if len(numbers)==3:
result=0
for v in numbers: # 例如是-3-5 通过- 分为[] 3 5,
if v:# 如果3在number中,
result-=float(v) #则3变为-3,然后5进来,result=-3-5=-8
str(result) ##加了str()将结果转化为字符串
else:
x,y=numbers
result=str(float(x)-float(y))##加了str()将结果转化为字符串
#替换字符串
s = s.replace(sub_str,'+' + result) # +不是连接两种数据类型相同的吗???
s = format(s)
# 获取两个减法的数
# x, y = sub_str.split('-')
# sub_result = '+' + str(float(x) + float(y))
# s = s.replace(sub_str, sub_result)
# s = format(s)
#
# x,y=ret1.split('[*/]',ret1)
# #检测是加法还是减法
# if '+' in ret1:
# ret2=float(x)+float(y)
# str(ret2)
# s.replace(ret1,ret2)
# else:
# ret2=float(x)-float(y)
# str(ret2)
# s.replace(ret1,ret2)
#
# return s#(17)

if check(source):
print('source',source)
print('eval result',eval(source))
strs=format(source)
print('source', source)

# 处理带括号的
while source.count('(')>0:# re.search('\(')
#去括号,得到括号的字符串,结果如(30+6/3)
strs=re.search('\([^()]*\)',source).group()
#将括号的表达式进行乘除运算
replace_strs=cal_mul_div(strs)
# 将括号的表达式进行加减运算
replace_strs=cal_add_sub(replace_strs) # '(17)'
#将括号的字符串替换为计算结果,结果包含(),替换时去掉():[1:-1]
source=format(source.replace(strs,replace_strs[1:-1]))

#处理不带括号的,没有括号就到最后单一表达式了
else:
replace_strs = cal_mul_div(source)
replace_strs = cal_add_sub(replace_strs)
print('my result',source.replace('+',''))



posted @ 2019-11-10 22:09  Stary_tx  阅读(1514)  评论(0编辑  收藏  举报