📄 matrix4.cpp.html
字号:
<html>
<head>
<title>matrix4.cpp</title>
</head>
<body>
<pre> <font color="#990000">1 using namespace std;
2
3 #include <iomanip>
4 #include <sstream>
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 << "Matrix index " << n << " 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 < rows * columns; i++)
23 elements[i] = 0;
24 }
25
<font color="#009999">26 Matrix& Matrix::operator=(const Matrix& other)</font>
27 {
28 if (this != &other)
29 {
30 free();
31 copy(other);
32 }
33 return *this;
34 }
35</font>
36 void Matrix::copy(const Matrix& other)
37 {
38 rows = other.rows;
39 columns = other.columns;
40 elements = new double[rows * columns];
41 for (int i = 0; i < rows * columns; i++)
42 elements[i] = other.elements[i];
43 }
<font color="#990000">44
45 double& Matrix::operator()(int i, int j)
46 {
47 if (i < 0 || i >= rows)
48 <font color="#009999">throw Matrix::IndexException(i)</font>;
49 if (j < 0 || j >= 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 < 0 || i >= rows)
57 throw Matrix::IndexException(i);
58 if (j < 0 || j >= columns)
59 throw Matrix::IndexException(j);
60 return elements[i * get_columns() + j];
61 }
62
63 Matrix& Matrix::operator+=(const Matrix& right)
64 {
65 if (rows != right.rows || columns != right.columns)
66 throw <font color="#009999">Matrix::MismatchException()</font>;
67 for (int i = 0; i < rows; i++)
68 for (int j = 0; j < columns; j++)
69 (*this)(i, j) += right(i, j);
70 return *this;
71 }</font>
72
73 Matrix operator+(const Matrix& left, const Matrix& right)
74 {
75 Matrix result = left;
76 result += right;
77 return result;
78 }
79
80 Matrix operator*(const Matrix& left, const Matrix& 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 < left.get_rows(); i++)
86 for (int j = 0; j < right.get_columns(); j++)
87 for (int k = 0; k < left.get_columns(); k++)
88 result(i, j) += left(i, k) * right(k, j);
89 return result;
90 }
91
92 Matrix operator*(const Matrix& left, double right)
93 {
94 Matrix result(left);
95 for (int i = 0; i < result.get_rows(); i++)
96 for (int j = 0; j < result.get_columns(); j++)
97 result(i, j) *= right;
98 return result;
99 }
100
101 ostream& operator<<(ostream& left, const Matrix& right)
102 {
103 const int WIDTH = 10;
104 for (int i = 0; i < right.get_rows(); i++)
105 {
106 for (int j = 0; j < right.get_columns(); j++)
107 left << setw(WIDTH) << right(i, j);
108 left << "\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 + -