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

📄 matrix3.cpp.html

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

<head>
	<title>matrix3.cpp</title>
</head>

<body>
<pre><font color="#990000">  1  #include &lt;iomanip&gt;
  2  #include &lt;sstream&gt;
  3
  4  #include "matrix3.h"
  5
  6  string MatrixIndexException::format_message(int n)
  7  {
  8     ostringstream outstr;
  9     outstr &lt;&lt; "Matrix index " &lt;&lt; n &lt;&lt; " out of range";
 10     return outstr.str();
 11  }
 12</font>
 13  Matrix::Matrix(int r, int c)
 14  {
 15     rows = r;
 16     columns = c;
 17     elements = new double[rows * columns];
 18     for (int i = 0; i &lt; rows * columns; i++)
 19        elements[i] = 0;
 20  }
 21
 22  Matrix&amp; Matrix::operator=(const Matrix&amp; other)
 23  {
 24     if (this != &amp;other)
 25     {
 26        free();
 27        copy(other);
 28     }
 29     return *this;
 30  }
 31
 32  void Matrix::copy(const Matrix&amp; other)
 33  {
 34     rows = other.rows;
 35     columns = other.columns;
 36     elements = new double[rows * columns];
 37     for (int i = 0; i &lt; rows * columns; i++)
 38        elements[i] = other.elements[i];
 39  }
 <font color="#990000">40
 41  double&amp; Matrix::operator()(int i, int j) throw (MatrixIndexException)
 42  {
 43     if (i &lt; 0 || i &gt;= rows)
 44        throw MatrixIndexException(i);
 45     if (j &lt; 0 || j &gt;= columns)
 46        throw MatrixIndexException(j);
 47     return elements[i * get_columns() + j];
 48  }
 49
 50  double Matrix::operator()(int i, int j) const <font color="#009999">throw (MatrixIndexException)</font>
 51  {
<font color="#009999"> 52     if (i &lt; 0 || i &gt;= rows)
 53        throw MatrixIndexException(i);
 54     if (j &lt; 0 || j &gt;= columns)
 55        throw MatrixIndexException(j);</font>
 56     return elements[i * get_columns() + j];
 57  }
 58
 59  Matrix&amp; Matrix::operator+=(const Matrix&amp; right)
 60     <font color="#009999">throw (MatrixMismatchException)</font>
 61  {
 62     <font color="#009999">if (rows != right.rows || columns != right.columns)
 63        throw MatrixMismatchException();</font>
 64     for (int i = 0; i &lt; rows; i++)
 65        for (int j = 0; j &lt; columns; j++)
 66           (*this)(i, j) += right(i, j);
 67     return *this;
 68  }
 69
 70  Matrix operator+(const Matrix&amp; left, const Matrix&amp; right)
 71     <font color="#009999">throw (MatrixMismatchException)</font>
 72  {
 73     Matrix result = left;
 74     result += right;
 75     return result;
 76  }
 77
 78  Matrix operator*(const Matrix&amp; left, const Matrix&amp; right)
 79     <font color="#009999">throw (MatrixMismatchException)</font>
 80  {
 81     <font color="#009999">if (left.get_columns() != right.get_rows())
 82        throw MatrixMismatchException();</font>
 83     Matrix result(left.get_rows(), right.get_columns());
 84     for (int i = 0; i &lt; left.get_rows(); i++)
 85        for (int j = 0; j &lt; right.get_columns(); j++)
 86           for (int k = 0; k &lt; left.get_columns(); k++)
 87              result(i, j) += left(i, k) * right(k, j);
 88     return result;
 89  }
 90
 91  Matrix operator*(const Matrix&amp; left, double right)
 92  {
 93     Matrix result(left);
 94     for (int i = 0; i &lt; result.get_rows(); i++)
 95        for (int j = 0; j &lt; result.get_columns(); j++)
 96           result(i, j) *= right;
 97     return result;
 98  }</font>
 99
100  ostream&amp; operator&lt;&lt;(ostream&amp; left, const Matrix&amp; right)
101  {
102     const int WIDTH = 10;
103     for (int i = 0; i &lt; right.get_rows(); i++)
104     {
105        for (int j = 0; j &lt; right.get_columns(); j++)
106           left &lt;&lt; setw(WIDTH) &lt;&lt; right(i, j);
107        left &lt;&lt; "\n";
108     }
109     return left;
110  }
111  </pre>
</body>
</html>

⌨️ 快捷键说明

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