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)
  • 相关阅读:
    Simple Automated Backups for MongoDB Replica Sets
    [转] matlab获取时间日期
    Matlab与C++混合编程 编写独立外部应用程序时出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误
    Visual Studio 控制台应用程序 同时使用OpenCV和matlab mat文件操作
    [转] Matlab与C++混合编程(依赖OpenCV)
    OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决
    LinkedBlockingQueue多线程测试
    rdlc报告vs2008编辑正常,在vs2012在对错误的编辑
    SD3.0四个协议解读
    链队列
  • 原文地址:https://www.cnblogs.com/Anec/p/9723566.html
Copyright © 2011-2022 走看看