基于堆栈实现计算器

Posted 左岸边

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于堆栈实现计算器相关的知识,希望对你有一定的参考价值。

周项目作业憋两天没有解决问题,只能返回老师代码重新分析。常识解决问题。

以下代码全部摘自培训班老师。

基于堆栈的方法,实现计算器。

代码分析:

#_*_coding:utf-8_*_
import re
def is_symbol(element):#定义运算符函数
    res=False
    symbol=[+,-,*,/,(,)]
    if element in symbol:
        res=True
    return res

def priority(top_sym,wait_sym):#定义运算优先级函数
    # print(‘from the priotry : ‘,top_sym,wait_sym)
    level1=[+,-]
    level2=[*,/]
    level3=[(]
    level4=[)]
    #运算符栈栈顶元素为+-
    if top_sym in level1:
        # if wait_sym in level1:
        #     return ‘>‘
        # elif wait_sym in level2: # top_sym=‘-‘ wait_sym=‘*‘
        #     return ‘<‘
        # elif wait_sym in level3: # top_sym=‘-‘ wait_sym=‘(‘
        #     return ‘<‘
        # elif wait_sym in level4: # top_sym=‘-‘ wait_sym=‘)‘
        #     return ‘>‘
        # else:
        #     return ‘>‘
        if wait_sym in level2 or wait_sym in level3:
            return <
        else:
            return >

    #运算符栈栈顶元素为*/
    elif top_sym in level2:
        # if wait_sym in level1:# top_sym=‘*‘ wait_sym=‘+‘
        #     return ‘>‘
        # elif wait_sym in level2:# top_sym=‘*‘ wait_sym=‘*‘
        #     return ‘>‘
        # elif wait_sym in level3:# top_sym=‘*‘ wait_sym=‘(‘
        #     return ‘<‘
        # elif wait_sym in level4:# top_sym=‘*‘ wait_sym=‘)‘
        #     return ‘>‘
        # else:
        #     return ‘>‘
        if wait_sym in level3:
            return <
        else:
            return >

    #运算符栈栈顶元素为(
    elif top_sym in level3:
        if wait_sym in level4: #右括号)碰到了(,那么左括号应该弹出栈
            return =
        else:
            return <  #只要栈顶元素为(,等待入栈的元素都应该无条件入栈
    #运算符栈栈顶元素为),右括号与左括号是成对出现,当出现右括号时,不会将括号入栈,所以不用考虑此情况

def calculate(num1,symbol,num2):#定义计算函数。三个参数分别为数值1与于运算符和数值2
    res=0
    if symbol == +:
        res=num1+num2
    elif symbol == -:
        res=num1-num2
    elif symbol == *:
        res=num1*num2
    elif symbol == /:
        res=num1/num2
    print(from calculate res is [%s|%s|%s] %s %(num1,symbol,num2,res))
    return res

def init_action(expression):
    # print(expression)
    expression=re.sub( ,‘‘,expression)
    # print(expression)
    init_l=[i for i in re.split((\-\d+\.*\d*),expression) if i]
    # print(‘--->‘,init_l)
    expression_l=[]
    while True:
        if len(init_l) == 0:break
        exp=init_l.pop(0)
        # print(‘==>‘,exp)
        if len(expression_l) == 0 and re.search(^\-\d+\.*\d*$,exp):
            expression_l.append(exp)
            continue
        if len(expression_l) > 0:
            if re.search([\+\-\*\/\(]$,expression_l[-1]):
                expression_l.append(exp)
                continue

        new_l=[i for i in re.split(([\+\-\*\/\(\)]),exp) if i]
        expression_l+=new_l
        # print(expression_l)
    return expression_l

def main(expression_l):
    # print(‘from in the main‘,expression_l)
    number_stack=[]
    symbol_stack=[]
    for ele in expression_l:
        print(-*20)
        print(数字栈,number_stack)
        print(运算符栈,symbol_stack)
        print(待入栈运算符,ele)

        ret=is_symbol(ele)
        if not ret:
            #压入数字栈
            ele=float(ele)
            number_stack.append(ele)
        else:
            #压入运算符栈
            while True:
                if len(symbol_stack) == 0:
                    symbol_stack.append(ele)
                    break
                res=priority(symbol_stack[-1],ele)

                if res == <:
                    symbol_stack.append(ele)
                    break
                elif res == =:
                    symbol_stack.pop()
                    break
                elif res == >:
                    symbol=symbol_stack.pop()
                    num2=number_stack.pop()
                    num1=number_stack.pop()
                    number_stack.append(calculate(num1,symbol,num2))

    else:
        symbol=symbol_stack.pop()
        num2=number_stack.pop()
        num1=number_stack.pop()
        number_stack.append(calculate(num1,symbol,num2))

    return number_stack,symbol_stack

if __name__ == __main__:
    expression=-1 - 2 *((-60+30+(-40/5)*(-9-2*-5/30-7/3*99/4*2998+10/-568/14))-(-4*-3)/(16-3*2))+3
    # expression=‘(1-2*3)-1-2*((-60+30+(-40/5)*(-9-2*-5/30-7/3*99/4*2998+10*568/14))-(-4*-3)/(16-3*2))+3‘
    # expression=‘-1 -3*( -2+3)‘
    expression_l=init_action(expression)

    # print(expression_l)

    l=main(expression_l)
    # print(‘====>‘,l)
    print(最终结果是:%s %l[0][0])

以上是关于基于堆栈实现计算器的主要内容,如果未能解决你的问题,请参考以下文章

替换或删除后台堆栈上现有片段的代码不起作用

Android TalkBack 和片段堆栈

来自后台堆栈的片段 onResume

代码适用于与单个 html 文件不同的堆栈片段

Android - 从堆栈中间删除特定片段不起作用

在后台堆栈中多次防止相同的片段