用正则表达式实现简单计算器

 

#1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )
import re
def check_text(text):  #处理连续运算符
    text = text.replace('++','+')
    text = text.replace('-+','-')
    text = text.replace('+-','-')
    text = text.replace('--','+')
    return text

def mul_div(text):   #处理乘除法
    while re.search(r'[*/]', text):
        textNew = re.search(r'\d+\.?\d*[/*]-?\d+\.?\d*', text).group(0)
        if re.search(r'/', textNew):
            textList = textNew.split('/')
            ret = float(textList[0])/float(textList[1])
            text = text.replace(textNew, str(ret))
        else:
            textList = textNew.split('*')
            ret = float(textList[0]) * float(textList[1])
            text = text.replace(textNew, str(ret))
    return text
def add_sub(text):   #处理加减法
    while re.search(r'-?\d+\.?\d*[+-]-?\d+\.?\d*', text):
        textNew = re.search(r'-?\d+\.?\d*[+-]-?\d+\.?\d*', text).group(0)
        if re.search(r'\+', textNew):
            textList = textNew.split('+')
            ret = float(textList[0]) + float(textList[1])
            text = text.replace(textNew, str(ret))
        else:
            textList = textNew.split('-')
            ret = float(textList[0]) - float(textList[1])
            text = text.replace(textNew, str(ret))
    return text

def deal_paren(text):   #处理最里面的括号
    while True:
        if re.search(r'\([^()]*\)',text):
            lessParen = re.search(r'\([^()]*\)',text).group(0)
            if re.search(r'[*/]', lessParen):  #处理括号内的乘除运算
                textByMd = mul_div(lessParen)
                lessParen = textByMd[1:-1]
            if re.search(r'-?\d+\.?\d*[+-]-?\d+\.?\d*', lessParen): #处理括号内的加减运算
                textByAs = add_sub(lessParen)
                text = re.sub(r'\([^()]*\)', textByAs, text, 1)  #把最里面括号的内容替换成运算结果
                text = check_text(text)
            else:
                text = re.sub(r'\([^()]*\)', lessParen, text, 1)  # 把最里面括号的内容替换成运算结果
                text = check_text(text)

        else:  #处理没有括号的表达式
            text1 = mul_div(text)  #处理表达式最后的乘除法运算
            text2 = check_text(text1)    ##处理连续运算符
            text = add_sub(text2)  #处理表达式最后的加减法运算,返回结果
            break
    return ('计算结果是:',text)   #返回结果

def main():           #函数入口
    text = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
    text = text.replace(" ","")  #去除表达式的空格
    ret = deal_paren(text)
    return ret

ret = main()
print(ret)
test1 = eval('1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )')
print(test1)
print(ret == str(test1))
#思路:
# 计算器
# 首先得到一个字符串
# 去空格
# 没有空格的字符串
# 先算最里层括号里的 : 找括号 ,且括号里没有其他括号
# 得到了一个没有括号的表达式 :只有加减乘除
# 从左到右先找到第一个乘除法 :   # 循环
    # 乘除法第一个数的符号是不必匹配的
    # 找到乘除法如何计算呢:
        # 先判断是乘法还是除法
        # 如果是乘法就以‘*’分割得到的内容是字符串数据类型的数
        # 如果是除法就用'/'分割的内容是字符串数据类型的数
        # 转数据类型之后根据 '*','/'计算结果
        # 结果替换原来字符串中的内容
# 所有的乘除法都做完了
# 计算加减  —— 加减法
    # ++ -- -+ +-都要提前处理
    # 计算过程中所有的数都当成浮点数计算
    # 第一位可以为负数
# 只有一个数了 就可以结束了

 

posted on 2019-05-17 23:34  xufat  阅读(572)  评论(0)    收藏  举报

导航

/* 返回顶部代码 */ TOP