zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    C# Lambda表达式 (转)
    用C#读取txt文件的方法(转)
    c#中stringbuilder的使用(转)
    c# 日期和时间的获取(转)
    C# List<T>用法 泛型 (转)
    indent format codes
    格式化输入输出 小结
    putty connection manager 一些问题的整理
    linux 网络的一些书籍
    Oracle学习笔记
  • 原文地址:https://www.cnblogs.com/Anec/p/9723566.html
Copyright © 2011-2022 走看看