• python学习笔记 day19 作业讲解-使用正则表达式实现计算器


    1. 实现能计算类似 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
    s='1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
    s=s.split()
    s=''.join(s)
    print(s)
    ret=re.findall('((?:[+-]?d+(?:[+-*/]d+)+))',s)   #提取最里面的括号
    print(ret)
    for i in ret:
        i=re.search('-?d+(?:[+-*/]d+)+',i)     #提取到的带括号的表达式去除掉括号,只剩下表达式
        i=i.group()
        while re.search('d+[*/]d+',i):
            expression=re.search('(d+)([*/])(d+)', i)
            num_1=expression.group(1)
            sign=expression.group(2)
            num_2=expression.group(3)
            if sign=='/':
                result=int(num_1)/int(num_2)
            else:
                result = int(num_1) * int(num_2)
            i=re.sub('(d+)([*/])(d+)',str(result), i)
            print(i)
        while re.search('d+[+-]d+',i):
            expression = re.search('(d+)([+-])(d+)', i)
            num_1 = expression.group(1)
            sign = expression.group(2)
            num_2 = expression.group(3)
            if sign == '+':
                result = int(num_1) + int(num_2)
            else:
                result = int(num_1) - int(num_2)
            i = re.sub('(d+)([+-])(d+)', str(result), i)
            print(i)

    还没写好,但是我有思路了,就是 不断循环,先匹配括号,然后提取括号中的表达式,然后对这个表达式从左往右遍历,匹配*/,先计算乘除,将计算的结果代替原来匹配的正则规则,就是用计算出来的结果代替表达式,然后乘除匹配完之后匹配加减,一直算 然后括号算完 就是循环结束之后 ,就只剩下普通的计算了 和原来思路一样,明天应该能写出来,,,,

     我!终于!写出来了!!!

    #_+_coding:utf-8_*_
    #author:    xuanxuan
    #Time :     2018/9/13  12:43
    
    import re
    s='1 - 2 * ( (60-30 +(-40/5) * (9-2*5/2 + 9 /3*2/2*2 +10 * 2/4 )) - (-4*3)/ (16-5*2) )'
    s=s.split()   # 按照空格分割,组成一个列表 str--->list
    s=''.join(s)  # 将list-->str ,去除字符串的空格
    print('原始表达式:',s)
    while re.search('(-?d+(?:.d+)?(?:[+-*/]-?d+(?:.d+)?)+)',s):   #看这个表达式中存不存在括号  #(-?d+(?:[+-*/]-?d+)+)
        inner_expression = re.search('(-?d+(?:.d+)?(?:[+-*/]-?d+(?:.d+)?)+)',s).group()    # 原来的表达式中 最里层括号内的表达式
        expression=re.search('-?d+(?:.d+)?(?:[+-*/]-?d+(?:.d+)?)+',inner_expression).group()   # 把最内层括号中的表达式取出来
    
        while re.search('[*/]',expression):
            sign=re.search('[*/]',expression).group()
            if sign=='*':
                num_1=re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)',expression).group(1)
                num_2=re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)',expression).group(2)
                result=float(num_1) * float(num_2)
                expression=re.sub('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)',str(result),expression,1)
            elif sign=='/':
                num_1 = re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)', expression).group(1)
                num_2 = re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)', expression).group(2)
                result = float(num_1) / float(num_2)
                expression = re.sub('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)', str(result), expression,1)
    
        while re.search('-?d+(?:.d+)?([+-])',expression):
            sign = re.search('(-?d+(?:.d+)?)([+-])', expression).group(2)
            if sign=='+':
                num_1=re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)',expression).group(1)
                num_2=re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)',expression).group(2)
                result=float(num_1) + float(num_2)
                expression=re.sub('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)',str(result),expression,1)
            elif sign=='-':
                num_1 = re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)', expression).group(1)
                num_2 = re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)', expression).group(2)
                result = float(num_1) - float(num_2)
                expression = re.sub('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)', str(result), expression,1)
        s = re.sub('(-?d+(?:.d+)?(?:[+-*/]-?d+(?:.d+)?)+)', expression, s,1)
    
    while re.search('[*/]',s):
        sign=re.search('[*/]',s).group()
        if sign=='*':
            num_1=re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)',s).group(1)
            num_2=re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)',s).group(2)
            result=float(num_1)*float(num_2)
            s=re.sub('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)','+'+str(result),s,1)
        else:
            num_1 = re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)', s).group(1)
            num_2 = re.search('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)', s).group(2)
            result = float(num_1) / float(num_2)
            s = re.sub('(-?d+(?:.d+)?)[*/](-?d+(?:.d+)?)', '+'+str(result), s, 1)
    while re.search('[+-]',s):
        sign=re.search('[+-]',s).group()
        if sign=='+':
            num_1=re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)',s).group(1)
            num_2=re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)',s).group(2)
            result=float(num_1)+float(num_2)
            s=re.sub('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)',str(result),s,1)
        else:
            num_1 = re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)', s).group(1)
            num_2 = re.search('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)', s).group(2)
            result = float(num_1) - float(num_2)
            s = re.sub('(-?d+(?:.d+)?)[+-](-?d+(?:.d+)?)', str(result), s, 1)
    print(s)

    运行结果:

    talk is cheap,show me the code
  • 相关阅读:
    光盘和U盘
    解决时间同步
    僵尸进程 和 孤儿进程
    Centos虚拟机设置网络模式
    常用CDN 和 后台管理模板
    微信小程序wxs如何使用
    kubernetes/client-go--使用 Clientset 获取 Kubernetes 资源对象
    samplecontroller
    volcano
    DNS欺骗
  • 原文地址:https://www.cnblogs.com/xuanxuanlove/p/9637885.html
Copyright © 2020-2023  润新知