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)

 

转载于:https://www.cnblogs.com/Anec/p/9723566.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值