python 计算器

弄了6个小时,各种出错,伪静态规则出错1次,

def逻辑思维出错1次。哈哈

# 实现加减乘除及拓号优先级解析
# 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式,运算后得出结果,结果必须与真实的计算器所得出的结果一致
import re
equation = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )"
equation = re.sub(" ","",equation)#去除空格
‘‘‘
英文不好,先看翻译
addition --加
subtraction -- 减
multiplication --乘
division --除
‘‘‘
#定义四个基础算法
def addition(a,b):
    return a+b
def subtraction(a,b):
    return a-b
def multiplication(a,b):
    return a*b
def division(a,b):
    return a/b
def operator_substitution(s): #将--或者+-转意为单符号
    s = re.sub("--","+",s,1)
    s = re.sub("\+\-", "-", s, 1)
    return s
obj = re.compile("(^\-)?(\d+\.)?\d+[\*|/]\-?\d+(\.\d+)?")
obj2 = re.compile("(^\-)?(\d+\.)?\d+[\+|\-]\-?\d+(\.\d+)?")
def yunsuan(s):

    if "*" in s or "/" in s:
        prt = re.search(obj,s).group()
        hao = re.match(obj,prt).group()
        if "*" in hao:
            pp = "*"
            k1 = prt.split(pp)
            print(k1)
            de = multiplication(float(k1[0]), float(k1[1]))
        elif "/" in hao:
            pp = "/"
            k1 = prt.split(pp)
            de = division(float(k1[0]), float(k1[1]))
        # de = int(de)
        s = re.sub(obj,str(de),s,1)
        s = operator_substitution(s)
        return s
    elif "+" in s or "-" in s:
        prt = re.search(obj2,s).group()
        hao = re.match(obj2,prt).group()
        if "+" in hao:
            pp = "+"
            k1 = prt.split(pp)
            de = addition(float(k1[0]), float(k1[1]))
        elif "-" in hao:
            pp = "-"
            k1 = prt.split(pp)
            de = subtraction(float(k1[0]),float(k1[1]))
        s = re.sub(obj2, str(de), s, 1)
        s = operator_substitution(s)
        return s

def zhongji(s):
    while "*" in s or "/" in s or "+" in s or "-" in s and s.find("-") !=0:
        s = yunsuan(s)
    return s
def teji(s):
    prt = re.search("\([^()]+\)", s).group()

    krt = re.search("[^(]+[^)]",prt).group()
    if krt.find("-") == 0:
        krt = re.sub("-","",krt)
        krt = -float(zhongji(krt))
    else:
        krt = zhongji(krt)
    s = re.sub("\([^()]+\)",str(krt),s,1)
    s = operator_substitution(s)
    return s


def yesok(s):
    while "(" in s:
        s = teji(s)
        print(s)
    s = zhongji(s)
    return s
print(yesok(equation))
print(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) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式,运算后得出结果,结果必须与真实的计算器所得出的结果一致
import re
equation = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )"
equation = re.sub(" ","",equation)

def addition(a,b):
    return a+b
def subtraction(a,b):
    return a-b
def multiplication(a,b):
    return a*b
def division(a,b):
    return a/b
def operator_substitution(s): #将--或者+-转意为单符号
    s = re.sub("--","+",s,1)
    s = re.sub("\+\-", "-", s, 1)
    return s
obj = re.compile("(^\-)?(\d+\.)?\d+[\*|/]\-?\d+(\.\d+)?")
obj2 = re.compile("(^\-)?(\d+\.)?\d+[\+|\-]\-?\d+(\.\d+)?")
def yunsuan(s):
    if "*" in s or "/" in s:
        prt = re.search(obj,s).group()
        hao = re.match(obj,prt).group()
        if "*" in hao:
            pp = "*"
            k1 = prt.split(pp)
            de = multiplication(float(k1[0]), float(k1[1]))
        elif "/" in hao:
            pp = "/"
            k1 = prt.split(pp)
            de = division(float(k1[0]), float(k1[1]))
        s = re.sub(obj,str(de),s,1)
        s = operator_substitution(s)
        return s
    elif "+" in s or "-" in s:
        prt = re.search(obj2,s).group()
        hao = re.match(obj2,prt).group()
        if "+" in hao:
            pp = "+"
            k1 = prt.split(pp)
            de = addition(float(k1[0]), float(k1[1]))
        elif "-" in hao:
            pp = "-"
            k1 = prt.split(pp)
            de = subtraction(float(k1[0]),float(k1[1]))
        s = re.sub(obj2, str(de), s, 1)
        s = operator_substitution(s)
        return s
def zhongji(s):
    while "*" in s or "/" in s or "+" in s or "-" in s and s.find("-") !=0:
        s = yunsuan(s)
    return s
def teji(s):
    prt = re.search("\([^()]+\)", s).group()
    krt = re.search("[^(]+[^)]",prt).group()
    if krt.find("-") == 0:
        krt = re.sub("-","",krt)
        krt = -float(zhongji(krt))
    else:
        krt = zhongji(krt)
    s = re.sub("\([^()]+\)",str(krt),s,1)
    s = operator_substitution(s)
    return s
def yesok(s):
    while "(" in s:
        s = teji(s)
    s = zhongji(s)
    return s
print(yesok(equation))
print(yesok("1+1-(105/999)"))
print(yesok("8+9*85-(-898/58+666-8989/20)"))

相关推荐