⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix5.h.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
字号:
<html>

<head>
	<title>matrix5.h</title>
</head>

<body>
<pre><font color="#990000">  1  #ifndef MATRIX5_H
  2  #define MATRIX5_H
  3  
  4  #include &lt;iostream&gt;
  5  #include &lt;stdexcept&gt;
  6  #include &lt;iomanip&gt;
  7  
  8  using namespace std;
  9</font>
 10  /**
 11     Matrix exception class for indexing error
 12  */
 13  class MatrixIndexException : public out_of_range 
 14  {
 15  public:
 16     MatrixIndexException(int i);
 17  private:
 18     string format_message(int i);
 19  };
 20  
 21  /**
 22     Matrix exception class for mismatched argument error
 23  */
 24  class MatrixMismatchException : public invalid_argument 
 25  {
 26  public:   
 27     MatrixMismatchException();
 28  };
 <font color="#990000">29  
 30  /**
 31     This class describes a row in a matrix.
 32  */
 <font color="#009999">33  template&lt;typename T&gt; class Matrix;
 34  
 35  template&lt;typename T&gt; 
 36  class MatrixRow</font>
 37  {
 38  public:
 39     /**
 40        Remember a row for a given matrix
 41        @param r a pointer to the first element in the row
 42        @param s the size of the row
 43     */
 44     MatrixRow(<font color="#009999">Matrix&lt;T&gt;*</font> m, int s);
 45  
 46     /**
 47        Accesses a row element.
 48        @param j the column index
 49        @return a reference to the element with the given index
 50     */
 51     <font color="#009999">T&amp;</font> operator[](int j) throw (MatrixIndexException);
 52  
 53  private:
 54     <font color="#009999">Matrix&lt;T&gt;*</font> mat;
 55     int i;
 56  };
 57  
 58  /**
 59     This class describes a row in a constant matrix.
 60  */
 61  <font color="#009999">template&lt;typename T&gt;</font>
 62  class ConstMatrixRow
 63  {
 64  public:
 65     /**
 66        Constructs a row with a given start and size.
 67        @param r a pointer to the first element in the row
 68        @param s the size of the row
 69     */
 70     ConstMatrixRow(const <font color="#009999">Matrix&lt;T&gt;*</font> m, int s);
 71  
 72     /**
 73        Accesses a row element.
 74        @param j the column index
 75        @return a reference to the element with the given index
 76     */
 77     const T&amp; operator[](int j) const throw (MatrixIndexException);
 78  
 79  private:
 80     const <font color="#009999">Matrix&lt;T&gt;*</font> mat;
 81     int i;
 82  };
 83  
 84  /**
 85     This class describes a matrix with arbitrary rows and columns
 86  */
 <font color="#009999">87  template&lt;typename T&gt; 
 88  class Matrix</font>
 89  {
 90  public:
 91     /**
 92        Constructs a matrix filled with zero elements.
 93     */
 94     Matrix(int r, int c);
 95  </font>
 96     /**
 97        The big three
 98     */
 99     Matrix(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> other);
100     <font color="#009999">Matrix&lt;T&gt;&amp;</font> operator=(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> other);
101     ~Matrix();
102  
103     /**
104        Gets the number of rows of this matrix.
105        @return the number of rows
106     */
107     int get_rows() const;
108     
109     /**
110        Gets the number of columns of this matrix.
111        @return the number of columns
112     */
113     int get_columns() const;
114     
115     /**
116        Accesses a matrix element.
117        @param i the row index
118        @param j the column index
119        @return a reference to the element with the given indexes
120     */
121     <font color="#009999">T&amp;</font> operator()(int i, int j) throw (MatrixIndexException);
122  
123     /**
124        Accesses a matrix element.
125        @param i the row index
126        @param j the column index
127        @return the element with the given indexes
128     */
129     <font color="#009999">T</font> operator()(int i, int j) const throw (MatrixIndexException);
<font color="#990000">130  
131     /**
132        Accesses a matrix row.
133        @param i the row index
134        @return the row with the given index
135     */
136     <font color="#009999">MatrixRow&lt;T&gt;</font> operator[](int i) throw (MatrixIndexException);
137  
138     /**
139        Accesses a matrix row.
140        @param i the row index
141        @return the row with the given index
142     */
143     <font color="#009999">ConstMatrixRow&lt;T&gt;</font> operator[](int i) const
144        throw (MatrixIndexException);
145  
146     /**
147        Computes the matrix sum
148        @param right another matrix
149        @return the update matrix
150     */
151     <font color="#009999">Matrix&lt;T&gt;&amp;</font> operator+=(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right) 
152        throw (MatrixMismatchException);
153  
154  private:</font>
155     /**
156        Copies another matrix into this matrix.
157        @param other the other matrix
158     */
159     void copy(const Matrix&amp; other);
160  
161     /**
162        Frees the memory for this matrix.
163     */
164     void free();
<font color="#990000">165  
166     int rows;
167     int columns;
<font color="#009999">168     T* elements;</font>
169  };
170  
171  /**
172     Computes the matrix sum.
173     @param right another matrix
174     @return the product of this matrix and the other
175  */
<font color="#009999">176  template&lt;typename T&gt;
177  Matrix&lt;T&gt;</font> operator+(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> left, const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right) 
178     throw (MatrixMismatchException);
179  </font>
180  /**
181     Computes the matrix product.
182     @param right another matrix
183     @return the product of this matrix and the other
184  */
<font color="#009999">185  template&lt;typename T&gt;
186  Matrix&lt;T&gt;</font> operator*(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> left, const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right) 
187     throw (MatrixMismatchException);
188  
189  /**
190     Computes the scalar product of a scalar value and a matrix.
191     @param left a scalar value
192     @param right a matrix
193     @return the sum of the given value and the given matrix
194  */
<font color="#009999">195  template&lt;typename T&gt;
196  Matrix&lt;T&gt;</font> operator*(double left, const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right);
197  
198  /**
199     Computes the scalar product of a matrix and a scalar value.
200     @param right a scalar value
201     @return the sum of this matrix and the given value
202  */
203  <font color="#009999">template&lt;typename T&gt;
204  Matrix&lt;T&gt;</font> operator*(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> left, double right);
205  
206  /**
207     Prints a matrix to an output stream.
208     @param left an output stream
209     @param right a matrix
210     @return the given output stream
211  */
212  <font color="#009999">template&lt;typename T&gt;</font>
213  ostream&amp; operator&lt;&lt;(ostream&amp; left, const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right);
214  
215  inline MatrixIndexException::MatrixIndexException(int idx)
216     : out_of_range(format_message(idx)) {}
217  
218  inline MatrixMismatchException::MatrixMismatchException() 
219     : invalid_argument("Matrix arguments have incompatible sizes")  {}
220  
221  <font color="#009999">template&lt;typename T&gt;</font>
222  inline <font color="#009999">Matrix&lt;T&gt;</font>::Matrix(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> other) 
223  { 
224     copy(other); 
225  }
226  
227  <font color="#009999">template&lt;typename T&gt;</font>
228  inline <font color="#009999">Matrix&lt;T&gt;</font>::~Matrix() 
229  { 
230     free(); 
231  }
232  
233  <font color="#009999">template&lt;typename T&gt;</font>
234  inline int Matrix&lt;T&gt;::get_rows() const 
235  { 
236     return rows; 
237  }
238  
239  <font color="#009999">template&lt;typename T&gt;</font>
240  inline int <font color="#009999">Matrix&lt;T&gt;</font>::get_columns() const 
241  { 
242     return columns; 
243  }
244  
245  <font color="#009999">template&lt;typename T&gt;</font>
246  inline void <font color="#009999">Matrix&lt;T&gt;</font>::free()
247  {
248     delete[] elements;
249  }
250  
251  <font color="#009999">template&lt;typename T&gt;</font>
252  inline <font color="#009999">MatrixRow&lt;T&gt; Matrix&lt;T&gt;</font>::operator[](int i)
253     throw (MatrixIndexException)
254  {
255     return <font color="#009999">MatrixRow&lt;T&gt;(this, i)</font>;
256  }   
257  
258  <font color="#009999">template&lt;typename T&gt;</font>
259  inline <font color="#009999">ConstMatrixRow&lt;T&gt; Matrix&lt;T&gt;</font>::operator[](int i) const 
260     throw (MatrixIndexException)
261  {
262     return <font color="#009999">ConstMatrixRow&lt;T&gt;(this, i)</font>;
263  }   
264     
265  <font color="#009999">template&lt;typename T&gt;</font>
266  inline <font color="#009999">MatrixRow&lt;T&gt;</font>::MatrixRow(<font color="#009999">Matrix&lt;T&gt;*</font> m, int s) : mat(m), i(s) { }
267     
268  <font color="#009999">template&lt;typename T&gt;</font>
269  inline <font color="#009999">T&amp; MatrixRow&lt;T&gt;</font>::operator[](int j) throw (MatrixIndexException)
270  {
271     return (*mat)(i,j);
272  }   
273  
274  <font color="#009999">template&lt;typename T&gt;</font>
275  inline <font color="#009999">ConstMatrixRow&lt;T&gt;</font>::ConstMatrixRow(const <font color="#009999">Matrix&lt;T&gt;*</font> m, int s) 
276     : mat(m), i(s) { }
277  
278  <font color="#009999">template&lt;typename T&gt;</font>
279  inline const <font color="#009999">T&amp; ConstMatrixRow&lt;T&gt;</font>::operator[](int j) const 
280     throw (MatrixIndexException)
281  {
282     return (*mat)(i, j);
283  }   
284  
285  <font color="#009999">template&lt;typename T&gt;</font>
286  inline <font color="#009999">Matrix&lt;T&gt;</font> operator*(double left, const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right)
287  {
288     return right * left;
289  }
<font color="#990000">290  
<font color="#009999">291  template&lt;typename T&gt;
292  Matrix&lt;T&gt;::</font>Matrix(int r, int c)
293  {
294     rows = r;
295     columns = c;
296     elements = <font color="#009999">new T[rows * columns]</font>;
297  }
298</font>
299  <font color="#009999">template&lt;typename T&gt;
300  Matrix&lt;T&gt;&amp; Matrix&lt;T&gt;</font>::operator=(const Matrix&lt;T&gt;&amp; other) 
301  {
302     if (this != &amp;other)
303     {
304        free();
305        copy(other);
306     }
307     return *this;
308  }
309  
310  <font color="#009999">template&lt;typename T&gt;</font>
311  void <font color="#009999">Matrix&lt;T&gt;</font>::copy(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> other)
312  {
313     rows = other.rows;
314     columns = other.columns;
315     elements = <font color="#009999">new T[rows * columns]</font>;
316  }
317  
318  <font color="#009999">template&lt;typename T&gt;
319  T&amp; Matrix&lt;T&gt;</font>::operator()(int i, int j) throw (MatrixIndexException)
320  {
321     if (i &lt; 0 || i &gt;= rows)
322        throw MatrixIndexException(i);
323     if (j &lt; 0 || j &gt;= columns)
324        throw MatrixIndexException(j);
325     return elements[i * get_columns() + j];
326  }
327  
328  <font color="#009999">template&lt;typename T&gt;
329  T Matrix&lt;T&gt;:</font>:operator()(int i, int j) const
330     throw (MatrixIndexException)
331  {
332     if (i &lt; 0 || i &gt;= rows)
333        throw MatrixIndexException(i);
334     if (j &lt; 0 || j &gt;= columns)
335        throw MatrixIndexException(j);
336     return elements[i * get_columns() + j];
337  }
338     
339  <font color="#009999">template&lt;typename T&gt;
340  Matrix&lt;T&gt;&amp; Matrix&lt;T&gt;</font>::operator+=(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right) 
341     throw (MatrixMismatchException)
342  {
343     if (rows != right.rows || columns != right.columns)
344        throw MatrixMismatchException();
345     for (int i = 0; i &lt; rows; i++)
346        for (int j = 0; j &lt; columns; j++)
347           (*this)(i, j) += right(i, j);
348     return *this;
349  }
350  
351  <font color="#009999">template&lt;typename T&gt;
352  Matrix&lt;T&gt;</font> operator+(const Matrix&lt;T&gt;&amp; left, const Matrix&lt;T&gt;&amp; right) 
353     throw (MatrixMismatchException)
354  {
355     Matrix&lt;T&gt; result = left;
356     result += right;
357     return result;
358  }
359     
360  <font color="#009999">template&lt;typename T&gt;
361  Matrix&lt;T&gt;</font> operator*(const Matrix&lt;T&gt;&amp; left, const Matrix&lt;T&gt;&amp; right) 
362     throw (MatrixMismatchException)
363  {
364     if (left.get_columns() != right.get_rows())
365        throw MatrixMismatchException();
366     <font color="#009999">Matrix&lt;T&gt;</font> result(left.get_rows(), right.get_columns());
367     for (int i = 0; i &lt; left.get_rows(); i++)
368        for (int j = 0; j &lt; right.get_columns(); j++)
369           for (int k = 0; k &lt; left.get_columns(); k++)         
370              result(i, j) += left(i, k) * right(k, j); 
371     return result;
372  }
373     
374  <font color="#009999">template&lt;typename T&gt;
375  Matrix&lt;T&gt;</font> operator*(const <font color="#009999">Matrix&lt;T&gt;&amp;</font> left, double right)
376  {
377     <font color="#009999">Matrix&lt;T&gt;</font> result(left);
378     for (int i = 0; i &lt; result.get_rows(); i++)
379        for (int j = 0; j &lt; result.get_columns(); j++)
380           result(i, j) *= right; 
381     return result;
382  }
383     
384  <font color="#009999">template&lt;typename T&gt;</font>
385  ostream&amp; operator&lt;&lt;(ostream&amp; left, const <font color="#009999">Matrix&lt;T&gt;&amp;</font> right)
386  {
387     const int WIDTH = 10;
388     for (int i = 0; i &lt; right.get_rows(); i++)
389     {
390        for (int j = 0; j &lt; right.get_columns(); j++)
391           left &lt;&lt; setw(WIDTH) &lt;&lt; right(i, j);
392        left &lt;&lt; "\n";
393     }
394     return left;
395  }
<font color="#990000">396  
397  #endif</font></pre>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -