## 十、Python语言中简单数据结构的应用(之二)
----From a high schoolstudent's view to learn Python
关键字:
python 列表 堆栈 数据结构 递归调用 函数 组合问题 后缀表达式 前缀表达式逆波兰表达式 运算符 优先级
四、利用堆栈编程实现复杂的四则运算
本部分使用堆栈将表达式转换为后缀表达式,并计算表达式的值,表达式为四则运算表达式。
***Postfix notation***is an unambiguous way of writing an arithmetic expres- sion withoutparentheses. It is defined so that if“(*exp*1)**op**(*exp*2)”is a normal, fully parenthesized expression whose operation is**op**, the postfix version of this is“*pexp*1*pexp*2**op**”, where*pexp*1is the postfix version of*exp*1and*pexp*2is the postfix version of*exp*2.The postfix version of a sin- gle number or variable is just thatnumber or variable. For example, the postfix version of“((5+2)∗(8−3))/4”is “5 2 + 8 3 −∗4 /”. Describe a nonrecursiveway of evaluating an expression in postfix notation.
这是《Data Structures andAlgorithms in Python》书中堆栈的一道练习P252.
以下关于后缀表达式的详细说明(参考baidu)
1、概述
不包含括号,[运算符](http://baike.baidu.com/view/425996.htm)放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则,如:(2+ 1) * 3 , 即2 1 + 3 *
2、计算
运用后缀表达式进行计算的具体做法:
建立一个栈S 。从左到右读后缀表达式,如果读到[操作数](http://baike.baidu.com/view/420846.htm)就将它压入栈S中,如果读到n元运算符(即需要参数个数为n的运算符)则取出由栈顶向下的n项按操作符运算,再将运算的结果代替原栈顶的n项,压入栈S中。如果后缀表达式未读完,则重复上面过程,最后输出栈顶的数值则为结束。
3、转换
计算机实现转换:
将中缀表达式转换为后缀表达式的算法思想:
·开始扫描;
·数字时,加入后缀表达式;
·运算符:
1. 若为 '(',入栈;
1. 若为')',则依次把栈中的的运算符加入后缀表达式中,直到出现'(',从栈中删除'(' ;
1. 若为 除括号外的其他[运算符](http://baike.baidu.com/view/425996.htm),当其优先级高于栈顶运算符时,直接入栈。否则从栈顶开始,依次弹出比当前处理的[运算符优先级](http://baike.baidu.com/view/262524.htm)高和优先级相等的运算符,直到一个比它优先级低的或者遇到了一个左括号为止。
·当扫描的中缀表达式结束时,栈中的的所有运算符[出栈](http://baike.baidu.com/view/346791.htm);
4、人工实现转换
这里给出一个中缀表达式:a+b*c-(d+e)
第一步:按照[运算符](http://baike.baidu.com/view/425996.htm)的优先级对所有的运算单位加括号:式子变成了:((a+(b*c))-(d+e))
第二步:转换前缀与后缀表达式
- 前缀:把运算符号移动到对应的括号前面
则变成了:-( +(a *(bc))+(de))
把括号去掉:-+a*bc+de前缀式子出现
- 后缀:把运算符号移动到对应的括号后面
则变成了:((a(bc)* )+ (de)+)-
把括号去掉:abc*+de+-后缀式子出现
发现没有,前缀式,[后缀式](http://baike.baidu.com/view/2973716.htm)是不需要用括号来进行优先级的确定的。如表达式:3+(2-5)*6/3
5、计算机转换过程详细说明
后缀表达式 栈
3_________________+
3 ________________+(
3 2_______________+(-
3 2 5 -_____________+
3 2 5 -_____________+*
3 2 5 - 6 *___________+/
3 2 5 - 6 *3__________+/
3 2 5 - 6 *3/+________
("_____"用于隔开后缀表达式与栈)
遍历中缀表达式的每个节点,如果:
1)、 该节点为[操作数](http://baike.baidu.com/view/420846.htm):
直接拷贝进入后缀表达式
2)、 该节点是运算符,分以下几种情况:
A、 为“(”[运算符](http://baike.baidu.com/view/425996.htm):
压入临时堆栈中
B、 为“)”[运算符](http://baike.baidu.com/view/425996.htm):
不断地弹出临时堆栈顶部[运算符](http://baike.baidu.com/view/425996.htm)直到顶部的运算符是“(”为止。并把弹出的[运算符](http://baike.baidu.com/view/425996.htm)都添加到后缀表达式中
C、 为其他运算符,有以下步骤进行:
比较该运算符与临时栈栈顶指针的运算符的优先级,如果临时栈栈顶指针的优先级高于该运算符的优先级,弹出并添加到后缀表达式中,反复执行前面的比较工作,直到遇到一个栈顶指针的优先级低于该运算符的优先级,停止弹出添加并把该运算符压入栈中。
此时的比较过程如果出现栈顶的[指针](http://baike.baidu.com/view/159417.htm)为‘(’,则停止循环并把该运算符压入栈中,注意:‘(’不要弹出来。
遍历完中缀表达式之后,检查临时栈,如果还有[运算符](http://baike.baidu.com/view/425996.htm),则全部弹出,并添加到后缀表达式中。
以上描述的部分来自于网上的资料,写的非常详细,人工转换的例子也有。
五、使用Python的程序实现。
要把下面的仔细看完,还是需要一些耐心的。
首先是一些预处理的函数:目的是检查()使用是否匹配,有无不合法的字符出现在表达式中:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 32.1px; height: 363.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0px;"><font style="font-size: 14px;">1</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">2</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">3</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">4</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">5</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">6</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">7</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">8</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">9</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">10</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">11</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">12</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">13</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">14</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">15</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">16</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">17</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">18</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">19</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">20</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">21</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">22</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">23</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">24</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">25</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">26</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">27</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">28</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">29</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">30</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">31</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">32</font></span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">33</font></span></p></td><td valign="top" style="width: 375.3px; height: 363.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"># -*- coding=utf-8 -*-</font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144); min-height: 11px;"><font style="font-size: 14px;"><br/></font></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">def is_matched(expr):</font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> """ReturnTrue if all delimiters are properlymatch; <wbr/></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> Falseotherwise."""</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> lefty ='({['</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> righty =')}]'</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> S =[] <wbr/></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> for c inexpr:</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> if c inlefty: <wbr/></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> S.append(c)</wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> elif c inrighty:</wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if len(S) ==0: <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> returnFalse</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if righty.index(c) !=lefty.index(S.pop()):</wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> returnFalse</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> returnlen(S) == 0</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144); min-height: 11px;"><font style="font-size: 14px;"><br/></font></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;">def validate (instr):</font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> """检查输入的表达式,将不合法的字符去掉 """</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> """对于-()以及(-())这样形式的字符串处理很麻烦,统一转换为(0-1)* """</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> op ='+-*/()' <wbr> <wbr> <wbr> <wbr> <wbr> #合法的运算符</wbr></wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> digital ='1234567890.'</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> x1 =""</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> for i inrange(len(instr)):</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> if instr[i] in op or instr[i]in digital:</wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> x1 = x1 + instr[i]</wbr></wbr></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> x1 =x1.replace('(-', '((0-1)*')</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> ifx1[0]=='-':</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"><font style="font-size: 14px;"> <wbr> <wbr> <wbr> <wbr> x1 = x1.replace('-','(0-1)*', 1) <wbr/></wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0px;"><font style="font-size: 14px;"> <wbr> <wbr> returnx1</wbr></wbr></font></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
这个程序本身没有太多需要解释的,唯一需要注意的是:
line1:# -*- coding=utf-8-*-
这可不是注释,虽然以#开头,如果想让.py文件中使用中文的注释,而解释器不报错的话,必须在文件开头加入这么一句。
同时,如果想print语句显示中文字符串,必须注意些:
print u”中文显示举例”
在检查了表达式字符串的合法行之后,我们就将表达式中的数与运算符分离,按照各自的类型,逐个按照表达式的顺序存储在一个List中,如’123+234’,字符串123、234会被作为两个整型数保存。
程序如下:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 384.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">14</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">15</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">16</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">17</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">18</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">19</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">20</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">21</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">22</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">23</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">24</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">25</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">26</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">27</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">28</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">29</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">30</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">31</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">32</span></p></td><td valign="top" style="width: 376.9px; height: 384.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">def separation (x):</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> """</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> 将表达式进行拆分,拆分的结果存放到一个list中</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> 由于list中的元素可以为各种类型,这给我们提供了极大的便利</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> 将数字串转换为int或float型,运算符按字符串存储</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> """</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> op = '+-*/()' <wbr> <wbr> <wbr> <wbr> <wbr> #合法的运算符</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> digital = '1234567890.'</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> alist = []</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> ifrom = 999</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> for i in range(len(x)):</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> if x[i] inop:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if ifrom ==999: <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append(x[i])</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> else:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if x[i - 1] in digital:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if '.' inx[ifrom:i]:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append( float(x[ifrom:i] ) )</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append( int( x[ifrom:i]) )</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append(x[i])</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ifrom = 999 <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if ifrom==999:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ifrom=i <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> if ifrom != 999:</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> if '.' inx[ifrom:len(x)]:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append( float(x[ifrom:len(x)] ) )</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append( int(x[ifrom:len(x)] ) )</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> return alist</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
最后就是后缀表达式转换的函数了:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 742.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">14</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">15</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">16</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">17</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">18</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">19</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">20</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">21</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">22</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">23</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">24</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">25</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">26</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">27</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">28</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">29</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">30</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">31</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">32</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">33</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">34</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">35</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">36</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">37</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">38</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">39</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">40</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">41</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">42</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">43</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">44</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">45</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">46</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">47</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">48</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">49</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">50</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">51</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">52</span></p></td><td valign="top" style="width: 376.9px; height: 742.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">defsuffix_expression_r(exprinlist):</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> alist=[]</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> blist=[]</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> for i in exprinlist:</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> if type(i)== float or type(i) == int:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>alist.append(i) <wbr> <wbr> <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> if type(i)== str:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='(':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='+' ori=='-':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if len(blist)==0:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> else:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> whileTrue:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> x=blist.pop()</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if x=='+' or x=='-' or x=='*'or x=='/':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append(x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if x=='(':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> break</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if len(blist)==0:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> break</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='*' ori=='/':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if len(blist)==0:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> else:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> whilelen(blist)>0:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> x=blist.pop()</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if x=='*' orx=='/':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append(x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> break</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if x=='+' orx=='-':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> break</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if x=='(':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> blist.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> break</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i==')':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> while len(blist)>0:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>x=blist.pop()</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ifx!='(':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> alist.append(x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> ifx=='(':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> break <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> while len(blist)>0:</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr>alist.append(blist.pop()) <wbr> <wbr/></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> return alist</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
函数的输入参数是一个使用separation()函数将字符串表达式转换后的list
blist用来模拟存放运算符的堆栈,alist用来存放结果
最后要做的是进行计算,前面所有这些都是为了让计算机知道,哪些是数,哪些是运算操作符,并且将运算符的优先级顺序区分出来,等所有这些都做好,其实计算反而是最简单的事情了,看函数的实现:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 336.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">14</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">15</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">16</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">17</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">18</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">19</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">20</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td><td valign="top" style="width: 377.4px; height: 336.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">defcalculation(expression):</span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> list_a=[] <wbr/></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> for i in expression:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> iftype(i)==float or type(i)==int:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> list_a.append(i)</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> eliftype(i)==str:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> x=list_a.pop()</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> y=list_a.pop()</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='+':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> result=x+y</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='-':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> result=y-x</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='*':</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> result=x*y</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if i=='/' andabs(x)>1E-9:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> result=y/x</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>list_a.append(result)</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> return 'Error'</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> return result</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
函数调用方法:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 156.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p></td><td valign="top" style="width: 377.4px; height: 156.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">expr ="3+(2-5)*6.0/3"</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">if notis_matched(expr):</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> print u"错误的表达式!"</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">else:</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> temp_x1=validate(expr)</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> print(temp_x1)</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> expr_list = separation(temp_x1)</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> print expr_list</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> print suffix_expression_r(expr_list)</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> s_expr =suffix_expression_r(expr_list)</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> print s_expr</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Heiti sC Light'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> print expr, ' = ',calculation(s_expr)</wbr></wbr></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
显示的运行结果如下:
3+(2-5)*6.0/3
[3, '+', '(', 2, '-', 5, ')','*', 6.0, '/', 3]
[3, 2, 5, '-', 6.0, '*', 3,'/', '+']
3+(2-5)*6.0/3 = -3.0
六、结束语
看着、想着都很简单的事,为什么让计算机做起来这么复杂呢?甚至都会觉得计算机一点也没有我之前很智能的感觉了。但是在把这个程序做好之后,你输入任一的四则运算表达式,不管多长,它都能瞬间的检查你的表达式是否正确,如果正确的话它也能够瞬间的给你答案,似乎又智能了。
所以,我们应该这么认为:计算机的计算能力是很强大的,关键在于我们能够让它做什么,这个让计算机做什么的过程就是编程的过程。
我的更多文章:
- [Python程序调试的一些体会](http://blog.sina.com.cn/s/blog_d6cca93e0101ewc9.html)(2013-10-06 22:57:35)
- [十四、Python编程计算24点(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euxx.html)(2013-10-03 22:18:28)
- [十三、Python编程计算24点(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101eukc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片")
(2013-10-02 22:15:46)
- [十一、Python简单数据结构应用(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ep9z.html)(2013-09-23 23:31:49)
- [十、Python编程解决组合问题(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101entc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片")
(2013-09-21 23:37:27)
- [九、Python编程解决组合问题(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ent7.html)(2013-09-21 23:32:54)
- [八、Python的函数编程(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwj.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频")
(2013-09-20 23:09:39)
- [七、Python的函数编程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频")
(2013-09-20 23:09:10)
- [六、Python的程序流程](http://blog.sina.com.cn/s/blog_d6cca93e0101ejeg.html)(2013-09-19 16:53:58)
- [高中生如何学编程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)