• 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)
  • 相关阅读:
    scp命令报错-bash: scp: command not found
    shell比较两个字符串是否相等
    bat脚本:自动压缩n天前的文件【转载】
    shell bash判断文件或文件夹是否存在
    linux文件分割(将大的日志文件分割成小的)【转载】
    TCP/IP模型各个层次的功能和协议
    nginx初级安装配置
    Heartbeat+DRBD+MySQL高可用方案【转】
    【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
    oracle的exp和imp命令的使用【转载】
  • 原文地址:https://www.cnblogs.com/Anec/p/9723566.html
Copyright © 2020-2023  润新知