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

📄 matrix4.cpp.html

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

<head>
	<title>matrix4.cpp</title>
</head>

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

⌨️ 快捷键说明

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