📄 page146.html
字号:
<HTML><HEAD><TITLE>Implementation</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="tex2html2888" HREF="page147.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2886" HREF="page144.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2882" HREF="page145.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2890" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006132000000000000000">Implementation</A></H3><P>Program <A HREF="page146.html#progalgorithmsj"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the implementation of a simple RPN calculator.The purpose of this exampleis to illustrate the use of the <tt>Stack</tt> class.The program shown accepts very simplified RPN expressions:The expression may contain only integers,the addition operator, <tt>+</tt>,and the multiplication operator, <tt>*</tt>.In addition, the operator <tt>=</tt> pops the top value off the stackand prints it on the console.Furthermore, the calculator does its computation entirely with integers.<P><P><A NAME="5946"> </A><A NAME="progalgorithmsj"> </A> <IMG WIDTH=575 HEIGHT=391 ALIGN=BOTTOM ALT="program5943" SRC="img686.gif" ><BR><STRONG>Program:</STRONG> Stack application--a single-digit, RPN calculator.<BR><P><P>Notice that the <tt>stack</tt> variable ofthe <tt>calculator</tt> methodmay be any object that extends the abstract <tt>Stack</tt> class.Consequently,the calculator does not depend on the stack implementation used!For example, if we wish to use a stack implemented using an array,we can simply replace line 4 with the following:<PRE>stack = StackAsArray(10)</PRE><P>The running time of the <tt>calculator</tt> method dependsupon the number of symbols, operators, and operands,in the expression being evaluated.If there are <I>n</I> symbols,the body of the inner <tt>for</tt> loop is executed <I>n</I> times.It should be fairly obvious that the amount of work doneper symbol is constant, regardless of the type of symbol encountered.This is the case for both the <tt>StackAsArray</tt>and the <tt>StackAsLinkedList</tt> stack implementations.Therefore, the total running time needed to evaluate anexpression comprised of <I>n</I> symbols is <I>O</I>(<I>n</I>).<P><HR><A NAME="tex2html2888" HREF="page147.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2886" HREF="page144.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2882" HREF="page145.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2890" 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -