思路:
优先级处理
思路一:正则表达式+递归(计算时间慢)
思路二:堆栈的方式

队列:先进先出
电梯--》队列
上电梯(入队123):第三个人3,第二个人2,第一个人1
>>> li = []
>>> li.insert(0,'p1')#进队列
>>> li.insert(0,'p2')
>>> li.insert(0,'p3')
>>> li
['p3', 'p2', 'p1']
或者用append('元素')
下电梯(出队123):第一个人,第二个人,第三个人
>>> li.pop()#出队列
'p1'
>>> li.pop()
'p2'
>>> li.pop()
'p3'
>>>
如果用append方法,就用li.pop(0)
堆栈:先进后出
衣服箱子--》堆栈
装衣服(入栈123):第三件衣服3,第二件衣服2,第一件衣服1
>>> li = []
>>> li.insert(0,'a1')#进栈
>>> li.insert(0,'a2')
>>> li.insert(0,'a3')
>>> li
['a3', 'a2', 'a1']
>>>

取衣服(出栈321):第三件衣服,第二件衣服,第一件衣服
>>> li.pop(0)#出栈
'a3'
>>> li.pop(0)
'a2'
>>> li.pop(0)
'a1'
>>>

数字栈
运算符栈
拼接成表达式运算

实例:
#!/usr/bin/env python
#_*_coding:utf-8_*_

import re
#判断是数字还是符号的函数
def is_symbo(element):#遍历元素
    res = False#判断运算符
    symbol=['+','-','*','/','(',')']
    if element in symbol:
        res=True
    return res

#exp优先级比较
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 '>'
        # if wait_sym in level2:#top_sym='*' wait_sym='*'
        #     return '>'
        # elif top_sym in level3:#top_sym='-' wait_sym='('
        #     return '<'
        # elif top_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):
    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,重新赋值给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#pop完了结束
        exp=init_l.pop(0)#列表左侧弹元素
        #print('==>',exp)#打印列表每个元素
        if len(expression_l) ==0 and re.search('^\-\d+\.*\d*$',exp):#-开头,数字结尾,匹配负数
            expression_l.append(exp)
            continue#直接进行下次循环,不分割-1

        #判断负数出现的情况
        if len(expression_l) > 0:
            if re.search('[\+\-\*\/\(]$',expression_l[-1]):
                expression_l.append(exp)
                continue#不做切分,-2这步


        new_l = [i for i in re.split('([\+\-\*\/\(\)])',exp) if i]#切分结果
        #print(new_l)
        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_symbo(ele)
        #print(ret)
        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)

    #分到数字栈,运算符栈
    #主函数,处理格式化后的列表
    jieguo=main(expression_l)#数字栈
    print('最终结果是:%s' % jieguo[0][0])

 结果:

--------------------
数字栈 []
运算符栈 []
待入栈运算符 -1
--------------------
数字栈 [-1.0]
运算符栈 []
待入栈运算符 -
--------------------
数字栈 [-1.0]
运算符栈 ['-']
待入栈运算符 2
--------------------
数字栈 [-1.0, 2.0]
运算符栈 ['-']
待入栈运算符 *
--------------------
数字栈 [-1.0, 2.0]
运算符栈 ['-', '*']
待入栈运算符 (
--------------------
数字栈 [-1.0, 2.0]
运算符栈 ['-', '*', '(']
待入栈运算符 (
--------------------
数字栈 [-1.0, 2.0]
运算符栈 ['-', '*', '(', '(']
待入栈运算符 -60
--------------------
数字栈 [-1.0, 2.0, -60.0]
运算符栈 ['-', '*', '(', '(']
待入栈运算符 +
--------------------
数字栈 [-1.0, 2.0, -60.0]
运算符栈 ['-', '*', '(', '(', '+']
待入栈运算符 30
--------------------
数字栈 [-1.0, 2.0, -60.0, 30.0]
运算符栈 ['-', '*', '(', '(', '+']
待入栈运算符 +
from calculate res is [-60.0|+|30.0] -30.0
--------------------
数字栈 [-1.0, 2.0, -30.0]
运算符栈 ['-', '*', '(', '(', '+']
待入栈运算符 (
--------------------
数字栈 [-1.0, 2.0, -30.0]
运算符栈 ['-', '*', '(', '(', '+', '(']
待入栈运算符 -40
--------------------
数字栈 [-1.0, 2.0, -30.0, -40.0]
运算符栈 ['-', '*', '(', '(', '+', '(']
待入栈运算符 /
--------------------
数字栈 [-1.0, 2.0, -30.0, -40.0]
运算符栈 ['-', '*', '(', '(', '+', '(', '/']
待入栈运算符 5
--------------------
数字栈 [-1.0, 2.0, -30.0, -40.0, 5.0]
运算符栈 ['-', '*', '(', '(', '+', '(', '/']
待入栈运算符 )
from calculate res is [-40.0|/|5.0] -8.0
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0]
运算符栈 ['-', '*', '(', '(', '+']
待入栈运算符 *
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0]
运算符栈 ['-', '*', '(', '(', '+', '*']
待入栈运算符 (
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(']
待入栈运算符 -9
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(']
待入栈运算符 -
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-']
待入栈运算符 2
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0, 2.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-']
待入栈运算符 *
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0, 2.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '*']
待入栈运算符 -5
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0, 2.0, -5.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '*']
待入栈运算符 /
from calculate res is [2.0|*|-5.0] -10.0
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0, -10.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '/']
待入栈运算符 30
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -9.0, -10.0, 30.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '/']
待入栈运算符 -
from calculate res is [-10.0|/|30.0] -0.3333333333333333
from calculate res is [-9.0|-|-0.3333333333333333] -8.666666666666666
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-']
待入栈运算符 7
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 7.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-']
待入栈运算符 /
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 7.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '/']
待入栈运算符 3
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 7.0, 3.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '/']
待入栈运算符 *
from calculate res is [7.0|/|3.0] 2.3333333333333335
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 2.3333333333333335]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '*']
待入栈运算符 99
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 2.3333333333333335, 99.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '*']
待入栈运算符 /
from calculate res is [2.3333333333333335|*|99.0] 231.00000000000003
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 231.00000000000003]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '/']
待入栈运算符 4
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 231.00000000000003, 4.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '/']
待入栈运算符 *
from calculate res is [231.00000000000003|/|4.0] 57.75000000000001
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 57.75000000000001]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '*']
待入栈运算符 2998
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -8.666666666666666, 57.75000000000001, 2998.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '-', '*']
待入栈运算符 +
from calculate res is [57.75000000000001|*|2998.0] 173134.50000000003
from calculate res is [-8.666666666666666|-|173134.50000000003] -173143.1666666667
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.1666666667]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '+']
待入栈运算符 10
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.1666666667, 10.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '+']
待入栈运算符 /
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.1666666667, 10.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '+', '/']
待入栈运算符 -568
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.1666666667, 10.0, -568.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '+', '/']
待入栈运算符 /
from calculate res is [10.0|/|-568.0] -0.017605633802816902
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.1666666667, -0.017605633802816902]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '+', '/']
待入栈运算符 14
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.1666666667, -0.017605633802816902, 14.0]
运算符栈 ['-', '*', '(', '(', '+', '*', '(', '+', '/']
待入栈运算符 )
from calculate res is [-0.017605633802816902|/|14.0] -0.0012575452716297787
from calculate res is [-173143.1666666667|+|-0.0012575452716297787] -173143.16792421197
--------------------
数字栈 [-1.0, 2.0, -30.0, -8.0, -173143.16792421197]
运算符栈 ['-', '*', '(', '(', '+', '*']
待入栈运算符 )
from calculate res is [-8.0|*|-173143.16792421197] 1385145.3433936958
from calculate res is [-30.0|+|1385145.3433936958] 1385115.3433936958
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958]
运算符栈 ['-', '*', '(']
待入栈运算符 -
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958]
运算符栈 ['-', '*', '(', '-']
待入栈运算符 (
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958]
运算符栈 ['-', '*', '(', '-', '(']
待入栈运算符 -4
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, -4.0]
运算符栈 ['-', '*', '(', '-', '(']
待入栈运算符 *
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, -4.0]
运算符栈 ['-', '*', '(', '-', '(', '*']
待入栈运算符 -3
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, -4.0, -3.0]
运算符栈 ['-', '*', '(', '-', '(', '*']
待入栈运算符 )
from calculate res is [-4.0|*|-3.0] 12.0
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0]
运算符栈 ['-', '*', '(', '-']
待入栈运算符 /
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0]
运算符栈 ['-', '*', '(', '-', '/']
待入栈运算符 (
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0]
运算符栈 ['-', '*', '(', '-', '/', '(']
待入栈运算符 16
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0, 16.0]
运算符栈 ['-', '*', '(', '-', '/', '(']
待入栈运算符 -
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0, 16.0]
运算符栈 ['-', '*', '(', '-', '/', '(', '-']
待入栈运算符 3
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0, 16.0, 3.0]
运算符栈 ['-', '*', '(', '-', '/', '(', '-']
待入栈运算符 *
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0, 16.0, 3.0]
运算符栈 ['-', '*', '(', '-', '/', '(', '-', '*']
待入栈运算符 2
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0, 16.0, 3.0, 2.0]
运算符栈 ['-', '*', '(', '-', '/', '(', '-', '*']
待入栈运算符 )
from calculate res is [3.0|*|2.0] 6.0
from calculate res is [16.0|-|6.0] 10.0
--------------------
数字栈 [-1.0, 2.0, 1385115.3433936958, 12.0, 10.0]
运算符栈 ['-', '*', '(', '-', '/']
待入栈运算符 )
from calculate res is [12.0|/|10.0] 1.2
from calculate res is [1385115.3433936958|-|1.2] 1385114.1433936958
--------------------
数字栈 [-1.0, 2.0, 1385114.1433936958]
运算符栈 ['-', '*']
待入栈运算符 +
from calculate res is [2.0|*|1385114.1433936958] 2770228.2867873916
from calculate res is [-1.0|-|2770228.2867873916] -2770229.2867873916
--------------------
数字栈 [-2770229.2867873916]
运算符栈 ['+']
待入栈运算符 3
from calculate res is [-2770229.2867873916|+|3.0] -2770226.2867873916
最终结果是:-2770226.2867873916

 

posted on 2016-11-22 17:36  Beirut  阅读(347)  评论(1编辑  收藏  举报