码迷,mamicode.com
首页 > 编程语言 > 详细

python 计算器

时间:2020-01-30 17:30:36      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:bst   rac   port   规则   定义   nbsp   python   pytho   print   

弄了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)"))

 

python 计算器

标签:bst   rac   port   规则   定义   nbsp   python   pytho   print   

原文地址:https://www.cnblogs.com/bdua/p/12242995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!