# -*- coding: utf-8 -*-
import re
Parentheses = re.compile(r'\([^()]*\)') # find out the expression with parentheses but no parentheses inside
Mul_Dev = re.compile(r'(\d+\.?\d*)[*/]([+-]?\d+\.?\d*)') # find out the expression with '*' or '/' only
Add_Sub = re.compile(r'(\d+\.?\d*)[+-](\d+\.?\d*)') # find out the expression with '+' or '-' only
def compute_mul_dev(arg):
"""
calculate the subexpression which has '*' or '/'
"""
exp = Mul_Dev.search(arg[0])
if not exp:
return
else:
sub_exp = exp.group()
if len(sub_exp.split('*')) > 1:
n1, n2 = sub_exp.split('*')
ret = str(float(n1) * float(n2))
elif len(sub_exp.split('/')) > 1:
n1, n2 = sub_exp.split('/')
ret = str(float(n1) / float(n2))
arg[0] = Mul_Dev.sub(ret, arg[0], 1)
compute_mul_dev(arg)
def compute_add_sub(arg):
"""
calculate the expression only has '+' or '-'
"""
while True:
if arg[0].__contains__('--') \
or arg[0].__contains__('+-') \
or arg[0].__contains__('-+') \
or arg[0].__contains__('++'):
arg[0] = arg[0].replace('--', '+')
arg[0] = arg[0].replace('-+', '-')
arg[0] = arg[0].replace('+-', '-')
arg[0] = arg[0].replace('++', '+')
else:
break
if arg[0].startswith('-'):
arg[1] += 1
arg[0] = arg[0].replace('-', '&')
arg[0] = arg[0].replace('+', '-')
arg[0] = arg[0].replace('&', '+')
arg[0] = arg[0][1:]
exp = Add_Sub.search(arg[0])
if not exp:
return
sub_exp = exp.group()
if len(sub_exp.split('+')) > 1:
n1, n2 = sub_exp.split('+')
ret = float(n1) + float(n2)
elif len(sub_exp.split('-')) > 1:
n1, n2 = sub_exp.split('-')
ret = float(n1) - float(n2)
arg[0] = Add_Sub.sub(str(ret), arg[0], 1)
compute_add_sub(arg)
def compute(expression):
"""
calculate the expression not has parentheses
"""
exp = [expression, 0]
compute_mul_dev(exp)
compute_add_sub(exp)
cnt = divmod(exp[1], 2)
result = float(exp[0])
if (cnt[1] == 1):
result = -1 * result
return result
def execute(expression):
"""
calculate the expression in parentheses
"""
if not Parentheses.search(expression):
return expression
sub_exp = Parentheses.search(expression).group()
compute_exp = sub_exp[1:-1]
ret = compute(compute_exp)
new_exp = Parentheses.sub(str(ret), expression, 1)
return execute(new_exp)
if __name__ == "__main__":
exp = '-1-4*(3-10*(3*2-1*9/(3-4*5+ 4/2))) + 10.5 - (4+7)'
no_space_exp = re.sub(' ', '', exp)
ret = execute(no_space_exp)
fnl = compute(ret)
print('fnl result: ', fnl)
Python計(jì)算器之正則表達(dá)式實(shí)現(xiàn)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- Python 第四篇:生成器、迭代器、裝飾器、遞歸函數(shù)與正則表達(dá)式 Python迭代器和生成器 Python 迭代...
- 讀了《用 Python 的魔術(shù)方法做出更好的正則表達(dá)式 API》 后深受啟發(fā)。一直覺得Python的re模塊用起來...
- 人生中最后一個(gè)長假很快就要結(jié)束了。 1月25日,放假回家的第二天,我接到舅舅打來電話:“這應(yīng)該是你最后一個(gè)假期了吧...