python - 计算器 程序练习

v1.0 计算器(数据内不含括号方式:)

import re

def jisuan(a,b,c):
    sun_count = 0
    if c =="+":
        sun_count = str(float(a[b.index(c)]) + float(a[b.index(c) + 1]))
    elif c =="-":
        sun_count = str(float(a[b.index(c)]) - float(a[b.index(c) + 1]))
    elif c == "*":
        sun_count = str(float(a[b.index(c)]) * float(a[b.index(c) + 1]))
    elif c == "/":
        sun_count = str(float(a[b.index(c)]) / float(a[b.index(c) + 1]))
    a.remove(a[b.index(c)])
    a.remove(a[b.index(c)])
    a.insert(b.index(c), sun_count)
    b.remove(b[b.index(c)])

#测试数据:
# '100.5+4*5/2-3*2*2/4+9'
# '100.5+4/2-3*9*2-4+9'
# '10+15/5+2-9*2-100'
# '1+2+3+4+5+6+7+8+9'
# '100-1-3-5-6-7-8-77-6-5'
# '3*6*7*9*34*45*99'

n='100.5+4/2-3*9*2-4+9'
n2 = eval(n)
print("eval函数执行结果:",n2)

a = re.findall(r"\d+\.\d+|\d+",n)
b = re.findall(r"[+|\-|\*|\/]",n)
# print(a)
# print(b)

while b:
        if '*'in b or '/'in b:
            for i in b:
                if i == '*':
                    jisuan(a,b,"*")
                elif i == "/":
                    jisuan(a, b, "/")
        else:
            if b[0] == "+":
                jisuan(a, b, "+")
            elif b[0] == '-':
                jisuan(a, b, "-")

print("程序执行结果:",a[0])

 

v2.0  计算器

import re

def jisuan(a,b,c):
    sun_count = 0
    if c =="+":
        sun_count = str(float(a[b.index(c)]) + float(a[b.index(c) + 1]))
    elif c =="-":
        sun_count = str(float(a[b.index(c)]) - float(a[b.index(c) + 1]))
    elif c == "*":
        sun_count = str(float(a[b.index(c)]) * float(a[b.index(c) + 1]))
    elif c == "/":
        sun_count = str(float(a[b.index(c)]) / float(a[b.index(c) + 1]))
    a.remove(a[b.index(c)])
    a.remove(a[b.index(c)])
    a.insert(b.index(c), sun_count)
    b.remove(b[b.index(c)])

def xunhuan(a,b):
    while b:
        if '*' in b or '/' in b:
            for i in b:
                if i == '*':
                    jisuan(a, b, "*")
                elif i == "/":
                    jisuan(a, b, "/")
        else:
            if b[0] == "+":
                jisuan(a, b, "+")
            elif b[0] == '-':
                jisuan(a, b, "-")


#测试数据:
# '100.5+4*5/2-3*2*2/4+9'
# '100.5+4/2-3*9*2-4+9'
# '10+15/5+2-9*2-100'
# '1+2+3+4+5+6+7+8+9'
# '100-1-3-5-6-7-8-77-6-5'
# '3*6*7*9*34*45*99'
# '1-2*((60-30+(40/5+3)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(4*3)/(16-3*2))'
# '1-2*(60-30+(40/5+3))'
# '1-2*(60-30+(40/5+3)+(9+22))'
# '3*(4+50)-(3*2*2/4+9)*(((3+4)-4))'
# '3*(4+50)-((100+40)*5/2-3*2*2/4+9)*(((3+4)-4))'

n='1-2*(60-30+(40/5+3)+(9+22))'
# print(n)
while 1:
    if "("in n or ")" in n:
        kh = re.search(r"\([^(]([\d+\.\d|\-\d+\.\d+|\-|\*|\/]+)[^)]\)",n)
        # print(kh.group())
        if kh.group():
            a = re.findall(r"\d+\.\d+|\d+",kh.group())
            b = re.findall(r"[+|\-|\*|\/]",kh.group())
            # print(a)
            # print(b)
            xunhuan(a,b)
            # print(a[0])
            sou = re.compile(r"\([^(]([\d+\.\d|\-\d+\.\d+|\-|\*|\/]+)[^)]\)")
            n = n.replace(kh.group(),str(a[0]))
            # print(n)
            # print("-"*40)
    else:
        a = re.findall(r"\d+\.\d+|\d+", n)
        b = re.findall(r"[+|\-|\*|\/]", n)
        xunhuan(a, b)
        print("程序执行结果:",a[0])
        break
n2 = eval(n)
print("eval函数执行结果:",n2)

 

posted @ 2018-09-29 14:38  Anec  阅读(203)  评论(0编辑  收藏  举报