page201.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 70 行
HTML
70 行
<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="tex2html3518" HREF="page202.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3516" HREF="page200.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3510" HREF="page200.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3520" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION007241000000000000000">Implementation</A></H3><P>To begin with, we need to represent the terms of the polynomial.Program <A HREF="page201.html#progpolynomialc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> extends the definition ofthe <tt>Term</tt> class introduced in Program <A HREF="page189.html#progpolynomialb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>--some additions are needed to support thethe implementation of polynomial addition.<P><P><A NAME="10468"> </A><A NAME="progpolynomialc"> </A> <IMG WIDTH=575 HEIGHT=562 ALIGN=BOTTOM ALT="program10311" SRC="img817.gif" ><BR><STRONG>Program:</STRONG> <tt>Term</tt> methods.<BR><P><P>A number of additional operations are declared in Program <A HREF="page201.html#progpolynomialc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The first method, <tt>__copy__</tt>, creates a copy of a given term.<P>Two properties, <tt>coefficient</tt> and <tt>exponent</tt>,use the methods <tt>getCoefficient</tt> and <tt>getExponent</tt>(respectively)to return the corresponding instance attributes of a <tt>Term</tt> instance.Clearly, the running time of each of these operations is <I>O</I>(1).<P>The final method, <tt>__add__</tt>, provides the meansto add two <tt>Term</tt>s together.The result of the addition is another <tt>Term</tt>.The working assumption is that the terms to be added have identical exponents.If the exponents are allowed to differ,the result of of the addition is a polynomialwhich cannot be represented using a single term!To add terms with like exponents,we simply need to add their respective coefficients.Therefore, the running time of the <tt>Term</tt> addition operator is <I>O</I>(1).<P>We now turn to the polynomial itself.Program <A HREF="page201.html#progpolynomialAsSortedLista"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> introducesthe <tt>PolynomialAsSortedList</tt> class.This class extends the abstract <tt>Polynomial</tt> classdefined in Program <A HREF="page189.html#progpolynomiala"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.It has a single instance attribute of type <tt>SortedListAsLinkedList</tt>.We have chosen in this implementation to usethe linked-list sorted list implementationto represent the sequence of terms.<P><P><A NAME="10475"> </A><A NAME="progpolynomialAsSortedLista"> </A> <IMG WIDTH=575 HEIGHT=771 ALIGN=BOTTOM ALT="program10351" SRC="img818.gif" ><BR><STRONG>Program:</STRONG> <tt>PolynomialAsSortedList</tt> class <tt>__init__</tt> and <tt>__add__</tt> methods.<BR><P><P>Program <A HREF="page201.html#progpolynomialAsSortedLista"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__add__</tt> method.This method adds two <tt>Polynomial</tt>s to obtain a third.It is intended to be used like this:<PRE>p1 = PolynomialAsSortedList()p2 = PolynomialAsSortedList()# ...p3 = p1 + p2</PRE><P><HR><A NAME="tex2html3518" HREF="page202.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3516" HREF="page200.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3510" HREF="page200.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3520" 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 + -
显示快捷键?