Python 中缀表达式转换后缀表达式

简介:

实现一个可以处理加减乘数运算的中缀表达式转换后缀表达式的程序:

一个输入中缀表达式inOrder

一个输出池pool

一个缓存栈stack

从前至后逐字读取inOrder

首先看一下不包含括号的:

(1)操作数:直接输出到pool

(2)操作符:判断当前操作符与stack[top]操作符的优先级

  <1>当前操作符优先级高于stack[top]:将当前操作符添加到stack中;

  <2>当前操作符优先级低于或等于stack[top]:从stack[top]开始出栈,直到stack[top]优先级高于当前操作符,然后将当前操作符入栈;

(3)当inOrder遍历结束,如果stack非空,反转stack,添加到pool尾

复制代码
 1 __author__ = 'ZhangHe'
 2 def in2post(inOrder):
 3     pool = ''
 4     stack = ''
 5     for i in inOrder:
 6         ret = proc(i,switch(i),stack,pool)
 7         stack = ret['stack']
 8         pool = ret['pool']
 9     if stack != '':
10         stack = stack[::-1]
11     return pool+stack
12 
13 def switch(c):
14     operator1 = '+-'
15     operator2 = '*/'
16     num = 'abcdefg'
17     if c in num:
18         return 0
19     if c in operator1:
20         return 1
21     if c in operator2:
22         return 2
23 def proc(c,op,stack,pool):
24     top = len(stack)-1
25 
26     if op == 0:#abcdefg
27         pool += c
28     if op == 2 or op==1:#*
29         if top == -1:
30             stack += c
31         elif switch(stack[top]) < op:
32             stack += c
33         else:
34             while top != -1 and switch(stack[top]) >= op:
35                 pool += stack[top]
36                 top -= 1
37             if top != -1:
38                 stack = stack[0:top+1]
39             else:
40                 stack = ''
41             stack += c
42     ret = {
43         'stack':stack,
44         'pool':pool
45     }
46     return ret
47 inOrder = 'a+b*c-d*e+f/g'
48 print in2post(inOrder)
复制代码

当包含括号的时候应该考虑哪些因素呢?

在上个程序的基础上,进一步考虑下边的几点:

(4)加入括号之后,“(”拥有最高优先级,也就是说,遇到“(”就放到stack中就好了;

(5)遇到“)”的操作也比较简单,直接把stack中的元素逐一弹出到pool,直到弹出“(”;

此时需要新的操作符规则:

(5)操作符:判断当前操作符与stack[top]操作符的优先级

  <1>当前操作符优先级高于stack[top]或者stack[top]==‘(’:将当前操作符添加到stack中;

  <2>当前操作符优先级低于或等于stack[top]:从stack[top]开始出栈,直到stack[top]优先级高于当前操作符或者stack[top]为“(”,然后将当前操作符入栈;

复制代码
 1 __author__ = 'ZhangHe'
 2 def in2post(inOrder):
 3     pool = ''
 4     stack = ''
 5     for i in inOrder:
 6         ret = proc(i,switch(i),stack,pool)
 7         stack = ret['stack']
 8         pool = ret['pool']
 9     if stack != '':
10         stack = stack[::-1]
11     return pool+stack
12 
13 def switch(c):
14     operator1 = '+-'
15     operator2 = '*/'
16     operator3 = ')'
17     operator4 = '('
18     num = 'abcdefg'
19     if c in num:
20         return 0
21     if c in operator1:
22         return 1
23     if c in operator2:
24         return 2
25     if c in operator3:
26         return 3
27     if c in operator4:
28         return 4
29 def proc(c,op,stack,pool):
30     top = len(stack)-1
31 
32     if op == 0:#abcdefg
33         pool += c
34     if op == 2 or op==1:#*
35         if top == -1:
36             stack += c
37         elif switch(stack[top]) < op or stack[top] == '(':
38             stack += c
39         else:
40             while top != -1 and switch(stack[top]) >= op and switch(stack[top])<switch(')'):
41                 pool += stack[top]
42                 top -= 1
43             if top != -1:
44                 stack = stack[0:top+1]
45             else:
46                 stack = ''
47             stack += c
48     if op == 3:#)
49         while top!= -1 and stack[top] != '(':
50             pool += stack[top]
51             top -= 1
52         stack = stack[0:top]
53     if op == 4:#(
54         stack += c
55     ret = {
56         'stack':stack,
57         'pool':pool
58     }
59     return ret
60 # inOrder = 'a+b*c-d*e+f/g'
61 inOrder = 'a+b*c+(d*e+f)*g'
62 print in2post(inOrder)
复制代码

输出:

 abc*+de*f+g*+ 

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4581771.html,如需转载请自行联系原作者

相关文章
|
10月前
|
Python
Python编程中正则表达式的使用
【10月更文挑战第22天】正则表达式,一种强大的文本处理工具,在Python编程中有着广泛的应用。本文将介绍如何使用Python中的re库来使用正则表达式,包括如何创建、匹配、查找和替换字符串等。通过学习本文,你将能够掌握Python中正则表达式的基本使用方法。
|
12月前
|
Python
Python中正则表达式(re模块)用法详解
Python中正则表达式(re模块)用法详解
244 2
|
11月前
|
程序员 Python
Python中Lambda表达式的优缺点及使用场景
Python中Lambda表达式的优缺点及使用场景
329 0
|
11月前
|
存储 算法 API
Python学习五:函数、参数(必选、可选、可变)、变量、lambda表达式、内置函数总结、案例
这篇文章是关于Python函数、参数、变量、lambda表达式、内置函数的详细总结,包含了基础知识点和相关作业练习。
141 0
深入浅出python的lambda表达式
今天我们来聊聊Python中一个常用的特性 - lambda表达式。别被这个听起来很高大上的名字吓到,其实它就是个匿名函数的实现机制。
|
Python
Python中的Lambda表达式
Python中的Lambda表达式
182 3
|
运维 Java API
探索Java中的Lambda表达式自动化运维的魔法:如何利用Python脚本提升效率
【8月更文挑战第29天】Lambda表达式是Java 8中引入的一个新特性,它允许我们将功能作为方法参数,或者代码作为数据来处理。在这篇文章中,我们将深入探讨Java中的Lambda表达式,包括它的语法、使用场景以及如何在实际编程中应用它。我们将通过一些简单的示例来演示Lambda表达式的强大功能和灵活性,让你更好地理解和掌握这一新特性。
|
测试技术 数据处理 Python
掌握Python lambda表达式:高效编程的新趋势
【8月更文挑战第22天】在Python中,函数是执行特定任务的代码块。匿名函数,即lambda函数,无需名称即可定义,适用于简短的一次性使用场景。其语法简洁,形如`lambda arguments: expression`,能有效应用于数据处理。例如,计算两数之和:`sum = lambda a, b: a + b`;对列表元素求平方:`squared = map(lambda x: x**2, numbers)`;或将字符串转为大写:`uppercased = map(lambda s: s.upper(), strings)`。这些用例展示了lambda函数如何简化代码,使其更清晰高效。
104 0
|
存储 Python
在Python中,匿名函数(lambda表达式)是一种简洁的创建小型、一次性使用的函数的方式。
【6月更文挑战第24天】Python的匿名函数,即lambda表达式,用于创建一次性的小型函数,常作为高阶函数如`map()`, `filter()`, `reduce()`的参数。lambda表达式以`lambda`开头,后跟参数列表,冒号分隔参数和单行表达式体。例如,`lambda x, y: x + y`定义了一个求和函数。在调用时,它们与普通函数相同。例如,`map(lambda x: x ** 2, [1, 2, 3, 4, 5])`会返回一个列表,其中包含原列表元素的平方。
135 4
|
Python
在Python中,解包参数列表和Lambda表达式是两个不同的概念
【6月更文挑战第19天】在Python中,解包参数允许将序列元素作为单独参数传递给函数,如`greet(*names_and_ages)`。而Lambda表达式用于创建匿名函数,如`lambda x, y: x + y`。两者可结合使用,如`max(*numbers)`找列表最大值,但过度使用lambda可能降低代码可读性。
79 3

推荐镜像

更多