#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;
}
初始化时矩阵
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