Python學習之路-python基礎課 (day6)

來源:互聯網
上載者:User

標籤:--   使用者輸入   com   ide   block   lock   公式   技術   value   

 

作業:

開發一個簡單的python計算機

  1. 實現加減乘除及拓號優先順序解析
  2. 使用者輸入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等類似公式後,必須自己解析裡面的(),+,-,*,/符號和公式(不能調用eval等類似功能偷懶實現),運算後得出結果,結果必須與真實的計算機所得出的結果一致

計算機源碼:

#!/usr/bin/env python# _*_ coding:utf-8 _*_# Author: Moimport redef compute_multipy_divide(arg):    value = arg[0]    mch = re.search(‘\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*‘,value)    if not mch:        return    content = re.search(‘\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*‘,value).group()    if len(content.split(‘*‘)) > 1:        v1,v2 = content.split(‘*‘)        get_value = float(v1) * float(v2)    else:        v1,v2 = content.split(‘/‘)        get_value = float(v1) / float(v2)    left,right = re.split(‘\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*‘,value,1)    new_value = ‘%s%s%s‘ %(left,get_value,right)    arg[0] = new_value    compute_multipy_divide(arg)def compute_add_substract(arg):    while True:        if ‘--‘ in arg[0] or ‘++‘ in arg[0] or ‘-+‘ in arg[0] or ‘+-‘ in arg[0]:            arg[0] = arg[0].replace(‘--‘,‘+‘)            arg[0] = arg[0].replace(‘++‘,‘+‘)            arg[0] = arg[0].replace(‘-+‘,‘-‘)            arg[0] = arg[0].replace(‘+-‘,‘-‘)        else:            break    if arg[0].startswith(‘-‘):        arg[1] += 1        arg[0] = arg[0].replace(‘-‘,‘&‘)        arg[0] = arg[0].replace(‘+‘,‘-‘)        arg[0] = arg[0].replace(‘&‘,‘+‘)        arg[0] = arg[0][1:]    value = arg[0]    mch = re.search(‘\d+\.*\d*[\+\-]{1}\d+\.*\d*‘,value)    if not mch:        return    content = re.search(‘\d+\.*\d*[\+\-]{1}\d+\.*\d*‘,value).group()    if len(content.split(‘+‘)) >1:        v1,v2 = content.split(‘+‘)        get_value = float(v1) + float(v2)    else:        v1,v2 = content.split(‘-‘)        get_value = float(v1) - float(v2)    left,right = re.split(‘\d+\.*\d*[\+\-]{1}\d+\.*\d*‘,str(value),1)    new_value = ‘%s%s%s‘ %(left,get_value,right)    arg[0] = new_value    compute_add_substract(arg)def compute(expression):    inp = [expression,0]    compute_multipy_divide(inp)    compute_add_substract(inp)    count = divmod(inp[1],2)    result = float(inp[0])    if count[1] == 1:        result = result * (-1)    return resultdef excute(expression):    if not re.search(‘\(([\+\-\*\/]*\d+\.*\d*){2,}\)‘,expression):        result = compute(expression)        return result    content = re.search(‘\(([\+\-\*\/]*\d+\.*\d*){2,}\)‘,expression).group()    new_content = content[1:len(content)-1]    new_list = re.split(‘\(([\+\-\*\/]*\d+\.*\d*){2,}\)‘,expression,1)    print(‘計算前:‘,expression)    left = new_list[0]    right = new_list[2]    final = compute(new_content)    print(‘計算:%s=%s‘ % (new_content, final))    new_expression = ‘%s%s%s‘ %(left,final,right)    print(‘計算後:‘, new_expression)    print(‘上次計算結束‘.center(50,‘*‘))    return excute(new_expression)def run():    while True:        print(‘ Oldboy Calc ‘.center(50, ‘*‘))        str_input = input(‘(quit=q)>>:‘).strip()        if str_input == ‘q‘: exit()        if len(str_input) == 0 : continue        if len(str_input) >0 :            no_space_str = re.sub(‘\s*‘,‘‘,str_input)        final = excute(no_space_str)        print(final)if __name__ == ‘__main__‘:    run()
View Code

 

計算結果:

****************** Oldboy Calc *******************(quit=q)>>:1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )計算前: 1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))計算:-40/5=-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))**********************上次計算結束**********************計算前: 1-2*((60-30+-8.0*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))計算:9-2*5/3+7/3*99/4*2998+10*568/14=173545.88095238098計算後: 1-2*((60-30+-8.0*173545.88095238098)-(-4*3)/(16-3*2))**********************上次計算結束**********************計算前: 1-2*((60-30+-8.0*173545.88095238098)-(-4*3)/(16-3*2))計算:60-30+-8.0*173545.88095238098=-1388337.0476190478計算後: 1-2*(-1388337.0476190478-(-4*3)/(16-3*2))**********************上次計算結束**********************計算前: 1-2*(-1388337.0476190478-(-4*3)/(16-3*2))計算:-4*3=-12.0計算後: 1-2*(-1388337.0476190478--12.0/(16-3*2))**********************上次計算結束**********************計算前: 1-2*(-1388337.0476190478--12.0/(16-3*2))計算:16-3*2=10.0計算後: 1-2*(-1388337.0476190478--12.0/10.0)**********************上次計算結束**********************計算前: 1-2*(-1388337.0476190478--12.0/10.0)計算:-1388337.0476190478--12.0/10.0=-1388335.8476190479計算後: 1-2*-1388335.8476190479**********************上次計算結束**********************2776672.6952380957****************** Oldboy Calc *******************(quit=q)>>:q
View Code

 

Python學習之路-python基礎課 (day6)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.