zoukankan      html  css  js  c++  java
  • 计算器的算法Python实现

    重点有:

    1. 将输入的表达式转化为逆波兰表达式
    2. 操作符进出栈处理
    3. 检测小数或多位数的情况
    4. 本算法实现采用从左向右计算

    想想学数据结构的时候还是大一下学期,转眼·····岁月是把猪食料。说点重点,计算器用到的知识点主要就是栈的应用,操作符进、出栈的处理,以及碰到括号的时候处理的方式。如果你C语言用的比较顺手,建议用C语言处理,能对栈的知识加强理解和应用。

    本算法实现时,先将乘除运算处理,之后剩余的加减从左向右一次计算。但还是希望你看下关于波兰表达式的知识。

    Python代码如下:

    import re
    you_put=input("输入你要计算的算式:\n")
    num_stack=[]#用于对数字的存储
    str_stack=[]#用于对操作符的存储
    i=0
    def cal(x,y,st):
        res=0
        x=float(x)
        y=float(y)
        if st is '+':
            res=x+y
        elif st is '-':
            res=x-y
        elif st is '*':
            res=x*y
        else:
            res=x/y
        return res
    def process(num_stack,str_stack):
        st = str_stack.pop()
        num_stack[len(num_stack) - 2] = cal(num_stack[len(num_stack) - 2],\
        num_stack[len(num_stack) - 1], st)
        num_stack.pop()
    while i<len(you_put):
        #判断扫描到的s[i]是操作数
        if you_put[i].isdigit():
            #当数值为多位数时,采用正则匹配
            num=(re.search(r'(\d+)',you_put[i:])).group()
            length=len(num)
            #先将'*'或者'/'计算出结果。例如:1-2*3-->1-6
            if len(num_stack)>0 and str_stack[len(str_stack)-1] in ['*','/']:
                num=cal(num_stack[len(num_stack)-1],num,str_stack[len(str_stack)-1])
                num_stack[len(num_stack)-1]=num
                str_stack.pop()
            else:
                num_stack.append(num)
            i+=length
            continue
        if you_put[i] is '(':
            #扫描到的s[i]是开括号'(',将s[i]压栈;
            str_stack.append(you_put[i])
            i+=1
            continue
        if you_put[i] in ['+','-','*','/','(']:
            #栈顶为'(' 或 扫描到的操作符优先级比栈顶操作符高
            if you_put[i] in ['*','/','(']:
                str_stack.append(you_put[i])
                i+=1
                continue
            else:
                if len(str_stack)==0:
                    str_stack.append(you_put[i])
                    i += 1
                    continue
                elif str_stack[len(str_stack)-1] in ['*','/']:
                    process(num_stack,str_stack)
                    str_stack.append(you_put[i])
                    i+=1
                    continue
                else:
                    str_stack.append(you_put[i])
                    i+=1
                    continue
        if you_put[i] is ')':
            while str_stack[len(str_stack)-1] is not '(':
                process(num_stack, str_stack)
            str_stack.pop()    #弹出'('
            i+=1
    while len(num_stack)>1:
        #因为之前已经将'*','/'计算过,所以现在仅剩'+','-'
        temp=0
        st = str_stack.pop(temp)
        num_stack[temp+1] = cal(num_stack[temp],num_stack[temp+1], st)
        num_stack.pop(temp)
    print(num_stack[0])

    运行结果截图如下:

    测试较少,如有错误或不妥之处,请多指出。

  • 相关阅读:
    hdu 5723 Abandoned country 最小生成树 期望
    OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉
    OpenJ_POJ C16D Extracurricular Sports 打表找规律
    OpenJ_POJ C16B Robot Game 打表找规律
    CCCC 成都信息工程大学游记
    UVALive 6893 The Big Painting hash
    UVALive 6889 City Park 并查集
    UVALive 6888 Ricochet Robots bfs
    UVALive 6886 Golf Bot FFT
    UVALive 6885 Flowery Trails 最短路
  • 原文地址:https://www.cnblogs.com/xsmile/p/7668775.html
Copyright © 2011-2022 走看看