開發乙個簡單的python計算器
實現加減乘除及拓號優先順序解析
使用者輸入 1 - 2 * ( (60-30 +(-40/5) * (9-25/3 + 7 /399/42998 +10 * 568/14 )) - (-43)/ (16-32) )等類似公式後,必須自己解析裡面的(),+,-,,/符號和公式(不能呼叫eval等類似功能偷懶實現),運算後得出結果,結果必須與真實的計算器所得出的結果一致
初版(執行出現bug,兩個負號同時出現未成功消去,待改進):
__author__ = 'administrator'
import re
# c = ' 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
cal_str = input('please input your calculating formula:')
cal_str = cal_str.replace(' ', '')
re_str_brackets = re.compile('\([^()]+\)')
re_str_multiply = re.compile('(\-?\d+)(\.\d+)?\*(\-?\d+)(\.\d+)?')
re_str_divide = re.compile('(\-?\d+)(\.\d+)?\/(\-?\d+)(\.\d+)?')
re_str_multiply_and_divide = re.compile('(\-?\d+)(\.\d+)?\*(\-?\d+)(\.\d+)?|(\-?\d+)(\.\d+)?\/(\-?\d+)(\.\d+)?')
re_str_add = re.compile('(\-?\d+)(\.\d+)?\+(\-?\d+)(\.\d+)?')
re_str_add_and_minus = re.compile('(\-?\d+)(\.\d+)?\+(\-?\d+)(\.\d+)?|(\-?\d+)(\.\d+)?\-(\-?\d+)(\.\d+)?')
re_str_minus = re.compile('(\-?\d+)(\.\d+)?\-(\-?\d+)(\.\d+)?')
def del_brackets(formula_str):
return formula_str.strip('()')
def extract_multiplication_and_divison(formula_str):
formula_str = re_str_multiply_and_divide.search(formula_str).group()
if re.search(re_str_multiply, formula_str):
str_list = formula_str.split('*')
result = round((float(str_list[0]) * float(str_list[1])), 8)
elif re.search(re_str_divide, formula_str):
str_list = formula_str.split('/')
result = round((float(str_list[0]) / float(str_list[1])), 8)
result = ''.format(result)
if re.search('^\-', str_list[0]) and re.search('^\-', str_list[1]):
result = '+' + str(result)
return result
else:
return str(result)
def extract_plus_and_minus(formula_str):
formula_str = re_str_add_and_minus.search(formula_str).group()
if re.search(re_str_add, formula_str):
str_list = formula_str.split('+')
result = round((float(str_list[0]) + float(str_list[1])), 8)
elif re.search(re_str_minus, formula_str):
if re.search('^\-',formula_str):
str_list = re.findall('(\-?\d+)(\.\d+)?',formula_str)
result = round((float(str_list[0]) + float(str_list[1])), 8)
else:
str_list = formula_str.split('-')
result = round((float(str_list[0]) - float(str_list[1])), 8)
return str(result)
def xiaohao(formula_str):
if re.search('\+\-|\-\+', formula_str):
formula_str = re.sub('\+\-|\-\+', '-', formula_str)
elif re.search('\-\-', formula_str):
formula_str = re.sub('\-\-', '+', formula_str)
elif re.search('\+\+', formula_str):
formula_str = re.sub('\+\+', '+', formula_str)
return formula_str
while re.search(re_str_brackets, cal_str):
new_str = del_brackets(re.search(re_str_brackets, cal_str).group())
while re.search(re_str_multiply_and_divide, new_str):
result_str_1 = extract_multiplication_and_divison(new_str)
new_str = re.sub(re_str_multiply_and_divide, result_str_1, new_str, 1)
while re.search(re_str_add_and_minus, new_str):
result_str_1 = extract_plus_and_minus(new_str)
new_str = re.sub(re_str_add_and_minus, result_str_1, new_str, 1)
if re.search('\+\-|\-\-', new_str):
new_str = xiaohao(new_str)
cal_str = re.sub(re_str_brackets, new_str, cal_str, 1)
if re.search('\+\-|\-\-', cal_str):
cal_str = xiaohao(cal_str)
while re.search(re_str_multiply_and_divide, cal_str):
result_str_1 = extract_multiplication_and_divison(cal_str)
cal_str = re.sub(re_str_multiply_and_divide, result_str_1, cal_str, 1)
while re.search(re_str_add_and_minus, cal_str):
result_str_1 = extract_plus_and_minus(cal_str)
cal_str = re.sub(re_str_add_and_minus, result_str_1, cal_str, 1)
if re.search('\+\-|\-\-', cal_str):
cal_str = xiaohao(cal_str)
print(cal_str)
Python 實現簡易計算器功能
效果圖如下 建立橫條型框架 def frame root,side w frame root 在螢幕上建立一塊矩形區域 w.pack side side,expand yes,fill both expand 1時,使能fill屬性 0時,關閉fill屬性 fill both,當gui窗體大小發生變...
python正則實現計算器功能
coding utf 8 author gogh time 2017 12 4 20 16 email 361910002 qq.com import re def operator update formula 對formula公式進行 去除空字元,更新運算子處理 formula formula....
C 實現計算器功能
注意 判斷數值和表示式 的合理性。include include include define max len 200 判斷字元是否合理 0 9 bool checkinput char c if c 0x2e c 0x2b c 0x2d c 0x2a c 0x2f return false boo...