1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include<iostream>

using namespace std;

template<class T>
class matrix {

public:
    matrix(int theRows = 0, int theColumns = 0);

    matrix(const matrix<T> &);

    ~matrix() { delete[] element; }

    int rows() const { return theRows; }

    int columns() const { return theColumns; }

    T &operator()(int i, int j) const;

    matrix<T> &operator=(const matrix<T> &);

    matrix<T> operator+(const T &x) const;// 与常数相加
    matrix<T> operator+(const matrix<T> &) const;

    matrix<T> operator-(const T &x) const; // 与常数相减
    matrix<T> operator-(const matrix<T> &) const;

    matrix<T> operator&(const matrix<T> &) const;

    matrix<T> operator*(const matrix<T> &) const;

    matrix<T> operator+=(const T &);

    matrix<T> &operator+=(const matrix<T> &);

    int illegalParameterValue(string s) const {
        cout << s << endl;
        return -1;
    }

    T *getElement() const;

private:
    int theRows; // 矩阵的行数
    int theColumns; // 矩阵的列数
    T *element; // 数组element
};

// 矩阵类的构造函数和复制函数
template<class T>
matrix<T>::matrix(int theRows, int theColumns) {
    // 矩阵构造函数
    // 检验行数和列数的有效性
    if (theRows < 0 || theColumns < 0)
        throw illegalParameterValue("Rows and columns muse be >= 0");
    if ((theRows == 0 || theColumns == 0) && (theRows != 0 || theColumns != 0))
        throw illegalParameterValue("Either both or neigher rows and columns should be zero");
    // 创造矩阵
    this->theRows = theRows;
    this->theColumns = theColumns;
    element = new T[theRows * theColumns];
}

template<class T>
matrix<T>::matrix(const matrix<T> &m) {
    // 矩阵的复制构造函数
    // 创建矩阵
    theRows = m.theRows;
    theColumns = m.theColumns;
    element = new T[theRows * theColumns];

    // 复制m的每一个元素
    copy(m.element, m.element + theRows * theRows, element);
}

// 矩阵类对赋值操作符=的重载
template<class T>
matrix<T> &matrix<T>::operator=(const matrix<T> &m) {
    // 复制
    if (this != &m) {
        // 不能自己复制自己
        delete[] element;
        theRows = m.theRows;
        theColumns = m.theColumns;
        element = new T[theRows * theColumns];
        // 复制每一个元素
        copy(m.element, m.element + theRows * theColumns, element);
    }
    return *this;
}

// 矩阵类对()操作符的重载
template<class T>
T &matrix<T>::operator()(int i, int j) const {
    // 返回对元素element (i, j)的引用
    if (i < 1 || i > theRows || j < 1 || j > theColumns)
        throw illegalParameterValue("matrixIndexOutOfBounds");
    return element[(i - 1) * theColumns + j - 1];
}

template<class T>
matrix<T> matrix<T>::operator+(const matrix<T> &m) const {
    // 返回矩阵w=(*this)+m
    if (theRows != m.theRows || theColumns != m.theColumns)
        throw illegalParameterValue("matrixSizeMismatch");
    // 生成结果矩阵
    matrix<T> w(theRows, theColumns);
    for (int i = 0; i < theRows * theColumns; ++i) {
        w.element[i] = element[i] + m.element[i];
    }
    return w;
}

template<class T>
matrix<T> matrix<T>::operator+(const T &x) const {
    // 返回结果矩阵w
    matrix<T> w(theColumns, theRows);
    for (int i = 0; i < theRows; ++i) {
        for (int j = 0; j < theColumns; ++j)
            w.element[i * theColumns + j] = element[i * theColumns + j] + x;
    }
    return w;
}

template<class T>
matrix<T> matrix<T>::operator-(const T &x) const {
    // 返回结果矩阵w
    matrix<T> w(theColumns, theRows);
    for (int i = 0; i < theRows; ++i) {
        for (int j = 0; j < theColumns; ++j)
            w.element[i * theColumns + j] = element[i * theColumns + j] - x;
    }
    return w;
}

template<class T>
matrix<T> matrix<T>::operator-(const matrix<T> &m) const {
    // 返回结果矩阵w
    matrix<T> w(theColumns, theRows);
    for (int i = 0; i < theRows; ++i) {
        for (int j = 0; j < theColumns; ++j)
            w.element[i * theColumns + j] = element[i * theColumns + j] - m.element[i * theColumns + j];
    }
    return w;

}

template<class T>
matrix<T> matrix<T>::operator+=(const T &x) {
    for (int i = 0; i < theRows; ++i) {
        for (int j = 0; j < theColumns; ++j)
            this->element[i * theRows + j] = element[i * theRows + j] + x;
    }
    return *this;
}

template<class T>
matrix<T> matrix<T>::operator&(const matrix<T> &) const {
    return *this;
}

// 矩阵乘法
template<class T>
matrix<T> matrix<T>::operator*(const matrix<T> &m) const {
    // 矩阵乘法,返回结果矩阵w=(*this)*m
    if (theColumns != m.theRows)
        throw illegalParameterValue("matrixSizeMisMatch");
    matrix<T> w(theRows, theColumns); // 结果矩阵
    // 定义矩阵*this,m和w的游标且初始化以为(1, 1)元素定位
    int ct = 0, cm = 0, cw = 0;
    // 对所有i和j计算w(i, j)
    for (int i = 1; i <= theRows; ++i) {
        // 计算结果矩阵的的第i行
        for (int j = 1; j <= m.theColumns; ++j) {
            // 计算w(i, j)的第一项
            T sum = element[ct] * element[cm];
            // 累加其余所以项
            for (int k = 2; k <= theColumns; ++k) {
                ct++; // *this中第i行的下一项
                cm += m.theColumns; //m中第j列的下一项
                sum += element[ct] * m.element[cm]; // *this的第i行元素乘m的第j列元素求和
            }
            w.element[cw++] = sum; // 存储在w(i, j)
            // 从行的起点和下一列从新开始
            ct -= theColumns - 1; // 重置ct为第一行第一个
            cm = j; // cm为m的第j列开始往下
        }
        // 从下一行和第一列重新开始
        ct += theColumns; // ct走完一行
        cm = 0; // cm走完了一列
    }
    return w;
}

template<class T>
T *matrix<T>::getElement() const {
    return element;
}

template<class T>
matrix<T> &matrix<T>::operator+=(const matrix<T> &m) {
    for (int i = 0; i < theRows; ++i) {
        for (int j = 0; j < theColumns; ++j)
            this->element[i * theRows + j] = element[i * theRows + j] + m.element[i * theRows + j];
    }
    return *this;
}

template<class T>
ostream &operator<<(ostream &out, const matrix<T> &m) {
    for (int i = 0; i < m.rows(); ++i) {
        for (int j = 0; j < m.columns(); ++j)
            out << m.getElement()[i * m.rows() + j] << '\t';
        out << '\n';
    }

    return out;
}

int main() {
    auto *mat1 = new matrix<int>(2, 2);
    cout << "初始化时矩阵" << endl;
    cout << *mat1 << endl;
    cout << "赋值后矩阵" << endl;
    (*mat1)(1, 1) = 2;
    (*mat1)(2, 2) = 2;
    cout << *mat1 << endl;
    cout << "进行=运算" << endl;
    auto *mat2 = mat1;
    cout << *mat2 << endl;
    cout << "与常数进行加法运算" << endl;
    *mat1 = *mat1 + 1;
    cout << *mat1 << endl;
    cout << "进行矩阵加法" << endl;
    *mat2 = *mat1 + *mat1;
    cout << *mat2 << endl;
    cout << "进行自加运算" << endl;
    *mat1 += *mat1;
    cout << *mat1 << endl;
    cout << "进行矩阵乘法" << endl;
    cout << "Mat1: " << '\n' << *mat1 << endl;
    cout << "Mat2: " << '\n' << *mat2 << endl;
    *mat2 = (*mat1) * (*mat2);
    cout << *mat2 << endl;
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
初始化时矩阵
0	0	
0	0	

赋值后矩阵
2	0	
0	2	

进行=运算
2	0	
0	2	

与常数进行加法运算
3	1	
1	3	

进行矩阵加法
6	2	
2	6	

进行自加运算
12	4	
4	12	

进行矩阵乘法
Mat1: 
12	4	
4	12	

Mat2: 
12	4	
4	12	

160	96	
96	160	

进程已结束,退出代码0