python 计算器练习

import re
def compute_add_sub(s):
    p = re.compile(r'(.*?)([+-]?\d*\.*\d+[+-]\d+\.*\d*)(.*)')
    match = p.search(s)
    if match:
        pre = match.groups()[0]
        middle = match.groups()[1]
        end = match.groups()[2]
        l = re.match(r'([+-]?\d*\.*\d+)([+-])(\d+\.*\d*)',middle).groups()
        result = 0
        if l[1] == '+':
            result = float(l[0]) +  float(l[2])
        elif l[1] == '-':
            result = float(l[0]) - float(l[2])
        s = ("%s%s%s") % (pre,result,end)
        print s
        return compute_add_sub(s)
    else:
        return  s

def compute_mul_div(s):
    p = re.compile(r'(.*?)(\d*\.*\d+[*/][+-]*\d+\.*\d*)(.*)')
    match = p.search(s)
    if match:
        pre = match.groups()[0]
        middle = match.groups()[1]
        end = match.groups()[2]
        l = re.match(r'(\d*\.*\d+)([*/][+-]*)(\d+\.*\d*)',middle).groups()
        print l,'int mul operation'
        result = 0
        if l[1] == '*' or l[1] == '*+' :
            result = float(l[0]) * float(l[2])
        elif l[1] == '/' or l[1] == '/+' :
            result = float(l[0]) / float(l[2])
        elif l[1] == '*-':
             result = -float(l[0]) * float(l[2])
        elif l[1] == '/-':
             result = -float(l[0]) / float(l[2])


        s = ("%s%s%s") % (pre,result,end)
        return compute_mul_div(s)
    else:
        return  s

def complute(inpp):
    p = re.compile(r'[*/]{1}')
    print inpp,'in compute'
    result = compute_mul_div(inpp)
    print result,'after mul div'

    result = result.replace('+-','-')
    result = result.replace('-+','-')
    result = result.replace('--','+')
    result = result.replace('++','+')
    result = compute_add_sub(result)
    print result,'in compute result'
    return result

def dealExpression(inpp):
    p = re.compile(r'(.*)\(([^)]*)\)(.*)')
    match = p.match(inpp)

    if match:
        pre = match.groups()[0]
        middle = match.groups()[1]
        end = match.groups()[2]
        result =  complute(middle)
        expression = '%s%s%s' % (pre,str(result),end)
        print expression,'express'
        expression = expression.replace('+-','-')
        expression = expression.replace('-+','-')
        expression = expression.replace('--','+')
        expression = expression.replace('++','+')
        return dealExpression(expression)

    else:
        inpp = inpp.replace('+-','-')
        inpp = inpp.replace('-+','-')
        inpp = inpp.replace('--','+')
        inpp = inpp.replace('++','+')
        print 'else...............'
        print inpp,'else inpp'
        return complute(inpp)


if __name__ == '__main__':

    #inpp = "1-2*-30/-12*(-20+200*-3/-200*-300-100)"
    inpp = '1 - 2 * ( (60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
   # inpp = '1 - 2 * ( (60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2+2*8) )'
    print eval(inpp)
    print  inpp
    inpp = re.sub(r'\s*','',inpp)
    print dealExpression(inpp)

 

posted on 2015-12-16 10:52  zhaobin022  阅读(239)  评论(0编辑  收藏  举报