zoukankan      html  css  js  c++  java
  • 正则与计算器

    import re
    
    def repeat_func(s):
        #去掉重复的+——号
        repeat = re.findall('+-|--|++-+', s)
        if len(repeat) > 0:
            for i in repeat:
                if i == '--' or i == '++':
                    s = s.replace(i, '+')
                if i == '+-' or s == '-+':
                    s = s.replace(i, '-')
        return s
    
    def mul_devid(content):
        # 计算乘除
        pattern1 = '-?d+.?d*(?:*|/)-?d+.?d*'
        while 1:
            first = re.search(pattern1, content)
            #匹配乘除
            if first:
                if '*' in first.group():
                    new1 = first.group().split('*')
                    new = float(new1[0]) * float(new1[1])
                    new = str(new)
                else:
                    new1 = first.group().split('/')
                    new = float(new1[0]) / float(new1[1])
                    new = str(new)
    
                res=re.search(r'-d+.?d*(?:*|/)-d+.?d*',content)
                #如果两个负数相乘或相除,添加正号
                if res :
                    new='+' + new
                content = repeat_func(content)
                content = content.replace(first.group(), new)
    
            else:
                break
        return content
    
    def add_minus(content):
        # 计算加减
        pattern2 = '-?d+.?d*(?:+|-)d+.?d*'
        while 1:
            first = re.search(pattern2, content)
            if first:
                #根据不同的模式分别计算
                choice1 = re.search('(-d+.?d*-d+.?d*)', first.group())
                if choice1:
                    new1 = first.group().split('-')
                    new = -(float(new1[1]) + float(new1[2]))
    
                choice2 = re.search('(-d+.?d*+d+.?d*)|(d+.?d*+d+.?d*)', first.group())
                if choice2:
                    new1 = first.group().split('+')
                    new = float(new1[1]) + float(new1[0])
    
                choice3 =  re.search('d+.?d*-d+.?d*', first.group())
                if choice3:
                    new1 = first.group().split('-')
                    new = float(new1[0]) - float(new1[1])
                #
                # content=repeat_func(content)
                content = content.replace(first.group(), str(new))
    
            else:
                break
    
        return content
    
    def calculate(content):
        content = mul_devid(content)
        content = add_minus(content)
        return content
    
    def bracket(s):
        while 1:
            first = re.search(r'([^()]+)', s)
            if first:
                new=calculate(first.group())
                s=s.replace(first.group(),new[1:-1])
                s=repeat_func(s)
            else: break
        end=calculate(s)
        return end
    
    s='1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
    
    print(eval(s))
    print(bracket(s))
  • 相关阅读:
    js开发规范,在php上也适用
    [读码时间] 仿腾讯微博效果
    [读码时间] 完美拖拽
    [读码时间] div闪烁
    [读码时间]用键盘控制div
    [读码时间] 自定义右键菜单
    [读码时间] 跟随鼠标移动(大图展示)
    [读码时间] 阻止右键菜单
    [读码时间] 按下键盘显示keyCode
    面试(5)
  • 原文地址:https://www.cnblogs.com/mona524/p/7096193.html
Copyright © 2011-2022 走看看