zoukankan      html  css  js  c++  java
  • 实现能计算类似1

     1 # 实现能计算类似
     2 s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
     3 # 等类似公式的计算器程序
     4 ss = ''
     5 for i in s:
     6     if i != ' ':
     7         ss += i
     8 import re
     9 ret = re.split('[()/]',re.search('(-?d+[/]d+)',ss).group())
    10 for i in range(ret.count('')):ret.remove('')
    11 fin = float(ret[0])/float(ret[1]) #(-40/5)
    12 ret1 = re.split('[(*-+/]',re.search('[*/](d+[-]d+[*]d+[/]d+',ss).group())
    13 for i in range(ret1.count('')):ret1.remove('')
    14 fin1 = float(ret1[0])-float(ret1[1])*float(ret1[2])/float(ret1[3])#*(9-2*5/3
    15 ret2 = re.split('[+*/]',re.search('[+]d+[/]d+[*]d+[/]d+[*]d+',ss).group())
    16 for i in range(ret2.count('')):ret2.remove('')
    17 fin2 = float(ret2[0])/float(ret2[1])*float(ret2[2])/float(ret2[3])*float(ret2[4]) #+7/3*99/4*2998
    18 ret3 = re.split('[+*/)]',re.search('[+]d+[*]d+[/]d+)',ss).group())
    19 for i in range(ret3.count('')):ret3.remove('')
    20 fin3 = float(ret3[0])*float(ret3[1])/float(ret3[2]) #+10*568/14)
    21 ret4 = re.split('[(-]',re.search('(d+[-](?[-]?d+)?',ss).group())
    22 for i in range(ret4.count('')):ret4.remove('')
    23 fin4 = float(ret4[0])-float(ret4[1]) #(60-30
    24 ret5 = re.split('[()*]',re.search('([-]?d+[*](?[-]?d+)?)',ss).group())
    25 for i in range(ret5.count('')):ret5.remove('')
    26 fin5 = float(ret5[0])*float(ret5[1]) #(-4*3)
    27 ret6 = re.split('[/()-*]',re.search('[/]((?[-]?d+[-](?[-]?d+)?[*](?[-]?d+)?))',ss).group())
    28 for i in range(ret6.count('')):ret6.remove('')
    29 fin6 = float(ret6[0])-float(ret6[1])*float(ret6[2]) #/(16-3*2))
    30 ret7 = re.match('(?[-]?d+)?',ss).group()
    31 fin7 = float(ret7) #1
    32 ret8 = re.split('[-*(]',re.search('(?[-]?d+)?[*]((',ss).group())
    33 for i in range(ret8.count('')):ret8.remove('')
    34 fin8 = float(ret8[0])
    35 fin9 = fin7-fin8*(fin4+fin*(fin1+fin2+fin3))-fin5/fin6
    36 print(fin9)
    37 print(eval('1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'))
    代码
    # 实现能计算类似
    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 = '1+1+1+'
    # 等类似公式的计算器程序
    import re
    def cpu_md(ss):#计算乘除
        while True:
            try:
                ret = re.search('d+.?d*[*/][-]?d+.?d*', ss).group()
                if ret:
                    if '*' in ret:
                        ret1 = re.split('[*]', ret)
                        ret2 = float(ret1[0]) * float(ret1[1])
                        ss = re.sub('d+.?d*[*/][-]?d+.?d*', str(ret2), ss, 1)
                    if '/' in ret:
                        ret1 = re.split('[/]', ret)
                        ret2 = float(ret1[0]) / float(ret1[1])
                        ss = re.sub('d+.?d*[*/][-]?d+.?d*', str(ret2), ss, 1)
                else:
                    return ss
            except:
                return ss
    def cpu_mdi(ss):#计算括号内加减eg:(1+2-4)
        while True:
            try:
                ret = re.search('[-]?d+.?d*[-+]d+.?d*[^*+-/)]', ss).group()
                if '+' in ret:
                    ret1 = re.split('[+]', ret)
                    ret2 = float(ret1[0]) + float(ret1[1])
                    ss = re.sub('[-]?d+.?d*[-+]d+.?d*[^*+-/)]', str(ret2), ss, 1)
                elif '-' in ret:
                    if not ret.startswith('-'):
                        ret1 = re.split('[-]', ret)
                        for i in range(ret1.count('')):ret1.remove('')
                        ret2 = float(ret1[0]) - float(ret1[1])
                        ss = re.sub('[-]?d+.?d*[-+]d+.?d*[^*+-/)]', str(ret2), ss, 1)
                else:
                    return ss
            except:
                return ss
    def d90(ss):#去括号eg:(-10)===-10
        while True:
            try:
                ret = re.search('([-]?d+.?d+)',ss).group()
                ret1 = re.split('[()]',ret)
                ret1.remove('')
                ret1.remove('')
                ss = re.sub('([-]?d+.?d+)',ret1[0],ss,1)
            except:
                return ss
    def jj(ss):
        while True:
            if '+-' in ss:ss = ss.replace('+-','-')
            elif '-+' in ss:ss = ss.replace('-+','-')
            elif '++' in ss:ss = ss.replace('++','+')
            elif '--' in ss:ss = ss.replace('--','+')
            else:return ss
    i = 0
    # s = input('>>>').strip()
    s = s.replace(' ','')
    while i < 5:
        s = cpu_md(s)
        s = cpu_mdi(s)
        s = d90(s)
        s = jj(s)
        i += 1
    # s = '2+2'
    
    print(s)
    View Code
  • 相关阅读:
    params可变参数
    using释放资源
    第二章:深入C#数据类型
    体检套餐项目解析
    堆栈
    C#必看:《第17章节QQ管理系统》
    C#必看:《第15章节学生管理系统》
    ACM hdu 1008 Elavator
    jq尺寸和位置总结篇(width scrollTop position offset)
    find children slice
  • 原文地址:https://www.cnblogs.com/fenglin0826/p/7307600.html
Copyright © 2011-2022 走看看