【Python基础】day14——【模块】re、计算器逻辑设计思路

 

集合正则表达式,对计算表达式进行格式化处理,并计算最终结果:

import re

def format_string(string):
    string = string.replace("--", "+")
    string = string.replace("-+", "-")
    string = string.replace("++", "+")
    string = string.replace("+-", "+")
    string = string.replace("*-", "*")
    string = string.replace("*+", "*")
    string = string.replace("/+", "/")
    string = string.replace(' ', '')
    return string

def calc_mul_div(string):
    regular='\d+\.?\d*([*/]|\*\*)[\-]?\d+\.?\d*'
    while re.findall(regular, string):
        expression = re.search(regular, string).group()

        if expression.count("*")==1:
            x,y = expression.split("*")
            mul_result = str(float(x) * float(y))
            string = string.replace(expression, mul_result)
            string = format_string(string)

        if expression.count("/"):
            x,y = expression.split("/")
            div_result = str(float(x) / float(y))
            string = string.replace(expression, div_result)
            string = format_string(string)

        if expression.count('*')==2:
            x,y=expression.split('**')
            pow_result=1
            for i in range(int(y)):
                pow_result*=int(x)
            string=string.replace(expression,str(pow_result))
            string=format_string(string)
        return string

def calc_add_sub(string):
    add_sub= regular='[\-]?\d+\.?\d*([+-][\-]?\d+\.?\d*'

    while re.findall(add_sub, string):
        add_sub_ex = re.search(add_sub, string).group()

        if add_sub_ex.count("+")==1:
            x,y = add_sub_ex.split("+")
            add_result = str(float(x) + float(y))
            string = string.replace(add_sub_ex, add_result)
            string = format_string(string)

        if add_sub_ex.count("-"):
            sub_list = re.search(add_sub, string)
            for sub_str in sub_list:
                numbers = sub_str.split("-")
                if len(numbers) == 3:
                    result = 0
                    for v in numbers:
                        if v:
                            result -= float(v)
                else:
                    x,y = numbers
                    result = float(x) - float(y)
                string = string.replace(sub_str, "+" + str(result))
            string = format_string(string)
        return string

def check_expression(string):
    check_result = True
    if not string.count("(") == string.count(")"):
        print("表达错误,括号示闭合!")
        check_result = False
    if re.findall('[a-z]+', string.lower()):
        print("表达式错误,包含非法字符!")
        check_result = False
    return check_result

source='1 - 2 * ((60-30 +(-40/5) * (-9-2-5-4*5-6/3-40/8+5*9) - (-4*3)/ (16-3*2)))'

if check_expression(source):
    print("source: ", source)
    print("eval result: ", eval(source))
    source = format_string(source)
    print(source)

    while source.count("(") > 0:
        strs = re.search('\([^()]*\)', source).group()
        replace_str = calc_mul_div(strs)
        replace_str = calc_add_sub(replace_str)
        source = format_string(source.replace(strs,replace_str[1:-1]))
    else:
        replace_str = calc_mul_div(source)
        replace_str = calc_add_sub(replace_str)
        source = source.replace(source, replace_str)
    print("my result: ", source.replace("+",""))

执行结果:

source:  1 - 2 * ((60-30 +(-40/5) * (-9-2-5-4*5-6/3-40/8+5*9) - (-4*3)/ (16-3*2)))
eval result:  -29.4
1-2*((60-30+(-40/5)*(-9-2-5-4*5-6/3-40/8+5*9)-(-4*3)/(16-3*2)))

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值