python计算器思路,python自己做计算器

题目:

exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'

如题自己写计算器,算出以上str中的结果,注意不能用eval函数,哈哈。

大致思路:定义3个函数,

函数1:计算+-*/,最终正则匹配的记过,都要使用此函数计算。

函数2:计算类似1-2+3*4/5,正则先匹配*/,在匹配+-,然后调用函数1算,得出来结果replace掉正则匹配,然后掉函数2递归,

函数3:搞最后的大的exp,先正则匹配出括号内的,去调函数2计算,计算的结果在replace,然后调用函数3递归。

使用到的方法:递归、正则

实现代码如下:最终计算的结果和eval结果比较一致,说明没问题

import re

def jianfa(exp):

'''此函数:用于减法计算'''

if exp.count("-")>2:

exp=exp.replace("--","+")

a,b=exp.split("+")

a,b=float(a),float(b)

return a+b

elif exp.count("-")>1:

if "--" in exp:

exp=exp.replace("--","+")

a,b=exp.split("+")

a,b=float(a),float(b)

return a+b

else:

exp=exp.strip("-")

a,b=exp.split("-")

a,b=float(a),float(b)

return 0-(a+b)

else:

exp=exp.strip("-")

a,b=exp.split("-")

a,b=float(a),float(b)

return a-b

def jisuanqi(exp):

'''此函数:用于加减乘除计算'''

if "*" in exp:

a,b=exp.split("*")

a,b=float(a),float(b)

return a*b

elif "/" in exp:

a,b=exp.split("/")

a,b=float(a),float(b)

return a/b

elif "+" in exp:

a,b=exp.split("+")

a,b=float(a),float(b)

return a+b

elif "-" in exp:

return jianfa(exp)

def compute_replace(exp,ret):

'''此函数:用于调用jisuanqi,然后得出的记过然后替换正则匹配的结果'''

new_ret = jisuanqi(ret)

new_ret = str(new_ret)

new_exp = exp.replace(ret, new_ret)

return new_exp

def fmt(exp):

'''此函数:用于去除以下特殊的符号,避免正则匹配出错'''

while "++" in exp or "--" in exp or "+-" in exp or "-+" in exp:

exp= exp.replace("++","+")

exp= exp.replace("--","+")

exp= exp.replace("+-","-")

exp= exp.replace("-+","-")

return exp

def func_compute(exp):

'''此函数:用于计算如1+2*/5这样的连续算法,先匹配*/在匹配+-,然后调用jisuanqi计算,并递归'''

exp=fmt(exp)

if "*" in exp or "/" in exp:

ret = re.search('\d+(\.\d+)?[*/]-?\d+(\.\d+)?',exp).group()

new_exp=compute_replace(exp,ret)

t_n_exp = func_compute(new_exp) #计算出的结果,再去递归调用本函数

return t_n_exp

elif "+" in exp or "-" in exp:

if exp.startswith("-") and exp.count("-") ==1 and exp.count("+")==0: #考虑如果最后结果为负数,走这个判断

return exp

ret = re.search('-?\d+(\.\d+)?[+-]\d+(\.\d+)?',exp).group()

new_exp = compute_replace(exp, ret)

t_n_exp = func_compute(new_exp) #计算出的结果,再去递归调用本函数

return t_n_exp

else:

return exp

def match(exp):

'''此函数:正则匹先配出括号内的公式,然后去调用上面函数计算,并递归'''

exp=exp.replace(" ","")

if "(" in exp:

match_exp=re.search("\([^()]+\)",exp).group()

n_match_exp=match_exp.strip("(")

n_match_exp = n_match_exp.strip(")")

ret=func_compute(n_match_exp)

new_exp=exp.replace(match_exp,ret)

c_new_exp=match(new_exp)

return c_new_exp

else:

return func_compute(exp)

exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'

ret=match(exp)

print(ret)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值