python,實現計算器程序,加減乘除混合運算加括號,完善實現


作業:要求實現一個由鍵盤輸入表達式,計算結果的小程序.

import re
#detect error input
s = input("Expression:")
s = re.sub('\s',"",s)
if re.findall('[^0-9\+\-\*/\)\(]',s):
    print("Error input,Stop!")
    exit()
    
def judge1(sign):
    if sign == '*':
        obj = re.compile('\*')
        
    elif sign == '/':
        obj = re.compile('/')
    return(obj)
def judge2(sign):
    if sign == '+':
        obj = re.compile('\+')
        
    elif sign == '-':
        obj = re.compile('\-')
    return(obj)   
    
def Format(s):
    s = s.replace(' ','')
    s = s.replace('++','+')
    s = s.replace('+-','-')
    s = s.replace('-+','-')
    s = s.replace('--','+')
    return(s)
    
def calculate(s):


    while re.search('[\*,/]',s):
        s = Format(s)
        obj = judge1(re.search('[\*,/]',s).group())
        res = obj.split(s)
        first = re.search('[\d,\.]+$',res[0]).group()
        second = re.search('^[\d,\.,\-]+',res[1]).group()
        if re.search('\.',first):
            fir = float(first)
        else:
            fir = int(first)
        if re.search('\.',second):
            sec = float(second)
        else:
            sec = int(second)
        if re.search('[\*,/]',s).group() == '*':
            
            result = str((fir * sec))
            s = re.sub('%s\*%s'%(first,second),result,s)
            print('s=',s)
                
        elif re.search('[\*,/]',s).group() == '/':
            result = str(fir / sec)
            s = re.sub('%s/%s'%(first,second),result,s)
            print('s=',s)
            if re.search('^\-[\d,\.]+',s):
                return(s)
        s = Format(s)
 #   return(s)
    while re.search('[\+,\-]',s):
        s = Format(s)
        obj = judge2(re.search('[\+,\-]',s).group())
        res = obj.split(s)
        first = re.search('[\d,\.]+$',res[0]).group()
        second = re.search('^[\d,\.,\-]+',res[1]).group()
        if re.search('\.',first):
            fir = float(first)
        else:
            fir = int(first)
        if re.search('\.',second):
            sec = float(second)
        else:
            sec = int(second)
        if re.search('[\+,\-]',s).group() == '+':
            
            result = str(fir + sec)
            s = re.sub('%s\+*%s'%(first,second),result,s)
            print('s=',s)
                
        elif re.search('[\+,\-]',s).group() == '-':
            result = str(fir - sec)
            
            s = re.sub('%s\-%s'%(first,second),result,s)
            print('=',s)
            if re.search('^\-[\d,\.]+',s):
                return(s)
        s= Format(s)
    return(s)
        
def first_step(s):
    while re.search('\([^()]+\)',s):
        res = re.search('\([^()]+\)',s)
        s_temp = res.group()
        print(s_temp)
        [first,second] = s.split(s_temp)
        ret = re.search('\([^()]+\)',s)
        tmp = re.search('[^()]+',ret.group())
        print(tmp.group())
        temp = str(calculate(tmp.group()))
        print('temp=',temp)
        
        s = first + temp + second
        print('first=',first)
        print('second=',second)
        print('temp=',temp)
        print('new s=',s)
#        if re.search('[\+,\-]{1}',s):
#            break
    result = calculate(s)
    print("result=",result)
 
       
        
first_step(s)

花了兩天時間終於完成這個小作業了,其實沒什么難度,就是正則表達式的靈活運用,由於邏輯比較復雜,測試花費了很多時間.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM