Python3.5計算機の複雑な式の演算

21262 ワード

1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))
  Python  ,                  ,      ,      
             ( +-*/()),        
Python3     +    
#coding=utf-8
#Version:python3.5.2
#Tools:Pycharm
__author__ = "Colby"
import re,os,sys
'''
1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))
'''
def format_mark(express):
    '''
        :param express: 
    :return: 
    '''
    express = express.replace('+-', '-')
    express = express.replace('-+', '-')
    express = express.replace('++', '+')
    express = express.replace('--', '+')
    express = express.replace('*+', '*')
    express = express.replace('+*', '*')
    express = express.replace('+/', '/')
    express = express.replace('/+', '/')
    return express
def com_jiajian(express):
    '''
    :param express: 
    :return: 
    '''
    expr = express
    sub_expr = re.search(r"\-?\d+\.?\d*[\+\-]\d+\.?\d*", expr)
    if not sub_expr:
        return expr
    else:
        sub_expr2 = sub_expr.group()
        # print('sub_expr1',sub_expr1,'19   express:',div_express)
        if len(sub_expr2.split('+')) > 1:
            n1, n2 = sub_expr2.split('+')
            result = float(n1)+float(n2)
        else:
            n1, n2 = sub_expr2.split('-')
            result = float(n1) - float(n2)
        re_sub_expr = re.sub(r"\-?\d+\.?\d*[\+\-]\d+\.?\d*", str(result), expr, count=1)
        #       
        print('',re_sub_expr)
        bb = com_jiajian(str(re_sub_expr))
        return bb
def com_chengchu(expr_div):
    '''
    :param expr_div: 
    :return: 
    '''
    expr=expr_div
    sub_expr = re.search(r"\d+\.?\d*[\/\*]\-?\d+\.?\d*",expr)
    if not sub_expr:
        return expr
    else:
        sub_expr2 = sub_expr.group()
        if len(sub_expr2.split('/')) > 1:
            n1, n2 = sub_expr2.split('/')
            result = float(n1)/float(n2)
        if len(sub_expr2.split('*')) > 1:
            n1, n2 = sub_expr2.split('*')
            result = float(n1)*float(n2)
        else:
            #pass            pass
        re_sub_expr=re.sub(r"\d+\.?\d*[\/\*]\-?\d+\.?\d*",str(result),expr,count=1)
        #      
        print('',re_sub_expr)
        bb=com_chengchu(format_mark(re_sub_expr))
        return bb
def compute(express):
    express = com_chengchu(format_mark(express))
    express = com_jiajian(format_mark(express))
    return express
def delkuohao(express):
    #           ,        ,      
    res=re.compile(r'[()]')
    sub_expr1 = re.search('(\([\+\-\*\/\.0-9]+\))', express)
    if not sub_expr1:
        return express
    else:
        sub_expr1=sub_expr1.group()
        #delkuohao(express)
        #        sub_expr2=sub_expr1[1:len(sub_expr1)-1]
        sub_expr3=compute(sub_expr2)
        sub_expr3 = re.sub('(\([\+\-\*\/\.0-9]+\))', str(sub_expr3),express,count=1)
        print('',sub_expr3)
        delkuohao_expr=delkuohao(format_mark(sub_expr3))
        return delkuohao_expr
if __name__=="__main__":
    #while True:
    #express=input("")
    print('
================================') print('\033[33m \033[0m') print('================================') #express ='1-2*((60-30+(-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))' while True: express = input('\033[32m , | ( :q)\033[0m') express = re.sub('\s*', '', express) if len(express) == 0: continue elif express == 'q': sys.exit(' ') elif re.search('[^0-9\.\-\+\*\/\(\)]',express): print('\033[31m !!!\033[0m') else: express = express.replace(' ', '') print('',express) ''' ''' express2 = delkuohao(express) # express2 = compute(format_mark(express2)) # print('\033[31m :%s'%express,'=', str(express2),'\033[0m')
 、      :
================================
        
================================
      ,     | (  :q)1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))
       : 1-2*((60-30+(-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2)))
    : -8.0
    : 1-2*((60-30+-8.0*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2)))
    : 9-10.0/3+7/3*99/4*2998+10*568/14
    : 9-3.3333333333333335+7/3*99/4*2998+10*568/14
    : 9-3.3333333333333335+2.3333333333333335*99/4*2998+10*568/14
    : 9-3.3333333333333335+231.00000000000003/4*2998+10*568/14
    : 9-3.3333333333333335+57.75000000000001*2998+10*568/14
    : 9-3.3333333333333335+173134.50000000003+10*568/14
    : 9-3.3333333333333335+173134.50000000003+5680.0/14
    : 9-3.3333333333333335+173134.50000000003+405.7142857142857
    : 5.666666666666666+173134.50000000003+405.7142857142857
    : 173140.1666666667+405.7142857142857
    : 173545.88095238098
    : 1-2*((60-30-8.0*173545.88095238098)-(-4*3)/(16-3*2)))
    : 60-30-1388367.0476190478
    : 30.0-1388367.0476190478
    : -1388337.0476190478
    : 1-2*(-1388337.0476190478-(-4*3)/(16-3*2)))
    : -12.0
    : 1-2*(-1388337.0476190478--12.0/(16-3*2)))
    : 16-6.0
    : 10.0
    : 1-2*(-1388337.0476190478+12.0/10.0))
    : -1388337.0476190478+1.2
    : -1388335.8476190479
    : 1-2*-1388335.8476190479)
    : 1--2776671.6952380957)
    : 2776672.6952380957)
   :1-2*((60-30+(-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))) = 2776672.6952380957)