(数据结构)[python](11)----中缀转后缀表达式

前缀表达式 中缀表达式 后缀表达式

中缀表达式后缀表达式前缀表达式
a+b*c-dabc*+d--d+*cba
(a+b)*cab+c**c+ab
(a+b)*(c-d)/eab+cd-*e//e*-cd+ab
(a*(b+c)-d)/eabc+*d-e//e-d*+abc

遇到符号就入栈
右括号一定入栈
遇到左括号取到右括号
低进高拿

方法一、代码编译中缀转后缀

expression = "a+b*(g+f*d+c)/e"

def middle2behind(expresssion):  
    result = []             # 结果列表
    stack = []              # 栈
    for item in expression: 
        if item.isalnum():      # 如果当前字符为数字那么直接放入结果列表
            result.append(item) 
        else:                     # 如果当前字符为一切其他操作符
            if len(stack) == 0:   # 如果栈空,直接入栈
                stack.append(item)
            elif item in '*/(':   # 如果当前字符为*/(,直接入栈
                stack.append(item)
            elif item == ')':     # 如果右括号则全部弹出(碰到左括号停止)
                t = stack.pop()
                while t != '(':   
                    result.append(t)
                    t = stack.pop()
            # 如果当前字符为加减且栈顶为乘除,则开始弹出
            elif item in '+-' and stack[len(stack)-1] in '*/':
                if stack.count('(') == 0:           # 如果有左括号,弹到左括号为止     
                    while stack:
                        result.append(stack.pop())
                else:                               # 如果没有左括号,弹出所有
                    t = stack.pop()
                    while t != '(':
                        result.append(t)
                        t = stack.pop()
                    stack.append('(')
                stack.append(item)  # 弹出操作完成后将‘+-’入栈
            else:
                stack.append(item)# 其余情况直接入栈(如当前字符为+,栈顶为+-)

    # 表达式遍历完了,但是栈中还有操作符不满足弹出条件,把栈中的东西全部弹出
    while stack:
        result.append(stack.pop())
    # 返回字符串
    return "".join(result)


print(middle2behind(expression))

方法二、代码编译中缀转后缀

1,中缀表达式2转后缀表达式
1)遇到操作数直接拼到字符串
2)遇到运算符
a,遇到左括号
b,遇到右括号
c,遇到-+号
d,遇到
/
2,计算后缀表达式
*

def middle2after(s=""):
    rule = {"-":1,"+":1,"*":2,"/":2}
    str = " "
    list = []
    for item in s:              #遍历
        if item.isalnum():
            str += item
        else:
            if item == "(":
                list.append(item)
            elif item == ")":
                while list[-1]!="(":            #如果他的最上面那个不是左括号 就拿出来一个
                    str += list.pop()
                list.pop()
            elif item in "+-":
                if len(list) == 0:
                    list.append(item)
                else:
                    while len(list)!=0 and list[-1]!="(":
                        str += list.pop()
                    list.append(item)

            elif item in "*/":
                list.append(item)

            else:
                print("表达式有问题")
    while len(list) != 0:
        str += list.pop()
    return str

def cal(s=""):                             #计算
    stack = []
    for item in s:
        if item.isalnum():
            stack.append(item)
        else:
            a = float(stack.pop())
            b = float(stack.pop())
            if item == "-":
                c = b - a
            elif item == "+":
                c = b + a
            elif item == "*":
                c = b * a
            elif item == "/":
                c = b / a
            else:
                print('后缀表达式有问题')
            stack.append(c)
    return stack.pop()
str = middle2after("1+3*(4+2)/5+(6+7*8-9)")
print(cal(str))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值