page144.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 95 行
HTML
95 行
<HTML><HEAD><TITLE>Applications</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2866" HREF="page145.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2864" HREF="page131.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2860" HREF="page143.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2868" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION006130000000000000000">Applications</A></H2><A NAME="secstacksapps"> </A><P>Consider the following expression:<P><A NAME="eqnstacksinfix"> </A> <IMG WIDTH=500 HEIGHT=16 ALIGN=BOTTOM ALT="equation5543" SRC="img674.gif" ><P>In order to determine the value of this expression,we first compute the sum 5+9 and then multiply that by 2.Then we compute the product <IMG WIDTH=35 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline60749" SRC="img675.gif" >and add it to the previous result to get the final answer.Notice that the order in which the operations are to be done is crucial.Clearly if the operations are not done in the correct order,the wrong result is computed.<P>The expression above is written using the usual mathematical notation.This notation is called <em>infix</em><A NAME=5547> </A> notation.What distinguishes this notation is the way that expressionsinvolving binary operators are written.A <em>binary operator</em><A NAME=5549> </A>is an operator which has exactly two operands,such as + and <IMG WIDTH=8 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline60753" SRC="img676.gif" >.In infix notation, binary operators appear<em>in between</em> their operands.<P>Another characteristic of <em>infix</em> notation is thatthe order of operations is determined by<em>operator precedence</em><A NAME=5553> </A>.For example, the <IMG WIDTH=8 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline60753" SRC="img676.gif" > (multiplication) operatorhas higher precedence than does the + (addition) operator.When an evaluation order is desiredthat is different from that provided by the precedence,<em>parentheses</em><A NAME=5555> </A>, ``('' and ``)'',are used to override precedence rules.An expression in parentheses is evaluated first.<P>As an alternative to infix,the Polish logician<em>Jan <IMG WIDTH=9 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap60767" SRC="img677.gif" > ukasiewicz</em><A NAME=5557> </A>introduced notations which require neither parenthesesnor operator precedence rules.The first of these,the so-called <em>Polish notation</em><A NAME=5559> </A>,places the binary operators before their operands.For Equation <A HREF="page144.html#eqnstacksinfix"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we would write:<P> <IMG WIDTH=314 HEIGHT=12 ALIGN=BOTTOM ALT="displaymath60741" SRC="img678.gif" ><P>This is also called <em>prefix</em> notation,because the operators are written in front of their operands.<P>While prefix notation is completely unambiguous in the absence of parentheses,it is not very easy to read.A minor syntactic variation on prefix is to write theoperands as a comma-separated list enclosed in parentheses as follows:<P> <IMG WIDTH=332 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath60742" SRC="img679.gif" ><P>While this notation seems somewhat foreign,in fact it is precisely the notation that is used forfunction calls in Python:<PRE>__add__(__mul__(__add__(5,9) ,2), __mul__(6,5))</PRE><P>The second form of <IMG WIDTH=9 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap60767" SRC="img677.gif" > ukasiewicz notation is the so-called<em>Reverse-Polish notation</em><A NAME=5571> </A>(RPN<A NAME=5672> </A>).Equation <A HREF="page144.html#eqnstacksinfix"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is written as follows in RPN:<P><A NAME="eqnstacksrpn"> </A> <IMG WIDTH=500 HEIGHT=16 ALIGN=BOTTOM ALT="equation5574" SRC="img680.gif" ><P>This notation is also called <em>postfix</em> notation for the obvious reason--the operators are written <em>after</em> their operands.<P>Postfix notation, like prefix notation,does not make use of operator precedencenor does it require the use of parentheses.A postfix expression can always be written without parenthesesthat expresses the desired evaluation order.For example, the expression <IMG WIDTH=62 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline60759" SRC="img681.gif" >, in which the multiplication is done first,is written <IMG WIDTH=67 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline60761" SRC="img682.gif" >;whereas the expression <IMG WIDTH=75 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60763" SRC="img683.gif" > is written <IMG WIDTH=66 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline60765" SRC="img684.gif" >.<P><BR> <HR><UL> <LI> <A NAME="tex2html2869" HREF="page145.html#SECTION006131000000000000000">Evaluating Postfix Expressions</A><LI> <A NAME="tex2html2870" HREF="page146.html#SECTION006132000000000000000">Implementation</A></UL><HR><A NAME="tex2html2866" HREF="page145.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2864" HREF="page131.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2860" HREF="page143.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2868" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?