python 实现计算器功能

开发一个简单的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 = '{:.8f}'.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)

你可能感兴趣的:(脚本)