下面这段程序给出了矩阵的加法,减法,以及乘法的运算,并且使用了运算符重载等方式输出求解的答案,欲了解详细的过程请认真阅读代码。
#include<iostream>
using namespace std;
template<typename T>
class Matrix
{
private:
int rows, cols;
T* element;
public:
Matrix(int r, int c);
Matrix(const Matrix<T>& m);
~Matrix()
{
delete[]element;
}
int Rows() const
{
return rows;
}
int Columns()const
{
return cols;
}
T& operator()(int i, int j)const;
Matrix<T>& operator=(const Matrix<T>& m);
Matrix<T> operator+()const;
Matrix<T> operator+(const Matrix<T>& m)const;
Matrix<T> operator-()const;
Matrix<T> operator-(const Matrix<T>& m)const;
Matrix<T> operator*(const Matrix<T>& m)const;
void print()const
{
for (int i = 0; i < rows * cols; i++)
{
cout << element[i] << " ";
if ((i + 1) % this->cols == 0)
{
cout << endl;
}
}
cout << endl;
}
};
template<typename T>
Matrix<T>::Matrix(int r, int c)
{
if (c < 0 || r < 0)
{
cout << "bad initializer" << endl;
exit(1);
}
if (r == 1 && c == 1)
{
cout << "bad initializer" << endl;
exit(1);
}
rows = r;
cols = c;
element = new T[r * c];
};
template<typename T>
Matrix<T>::Matrix(const Matrix<T>& m)
{
rows = m.rows;
cols = m.cols;
element = new T[rows * cols];
for (int i = 0; i < rows * cols; i++)
{
element[i] = m.element[i];
}
}
template<typename T>
T& Matrix<T>::operator()(int i, int j)const
{
if (i<1 || i>rows || j<1 || j>cols)
{
cout << "Out of bounds." << endl;
exit(1);
}
return element[(i - 1) * cols + j - 1];
}
template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& m)
{
rows = m.rows;
cols = m.cols;
delete[]element;
element = new T[rows * cols];
for (int i = 0; i < rows * cols; i++)
{
element[i] = m.element[i];
}
return *this;
};
template<typename T>
Matrix<T> Matrix<T>::operator-(const Matrix<T>& m)const
{
if (rows != m.rows || cols != m.cols)
{
cout << "Size not matched!" << endl;
exit(1);
}
Matrix<T>w(rows, cols);
for (int i = 0; i < rows * cols; i++)
{
w.element[i] = element[i] - m.element[i];
}
return w;
};
template<typename T>
Matrix<T> Matrix<T>::operator*(const Matrix<T>& m)const
{
if (cols != m.rows)
{
cout << "Size not matched" << endl;
exit(1);
}
Matrix<T> w(rows, m.cols);
int ct = 0, cm = 0, cw = 0;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= m.cols; j++)
{
T sum = element[ct] * m.element[cm];
for (int k = 2; k <= cols; k++)
{
ct++;
cm += m.cols;
sum += element[ct] * m.element[cm];
}
w.element[cw] = sum;
cw++;
ct -= cols - 1;
cm = j;
}
ct += cols;
cm = 0;
}
return w;
};
template<typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>& m)const
{
if (rows != m.rows || cols != m.cols)
{
cout << "Size not matched!" << endl;
exit(1);
}
Matrix<T>w(rows, cols);
for (int i = 0; i < rows * cols; i++)
{
w.element[i] = element[i] + m.element[i];
}
return w;
};
template<typename T>
Matrix<T> Matrix<T>::operator+()const
{
for (int i = 0; i < rows * cols; i++)
{
element[i] += 1;
}
}
template<typename T>
Matrix<T> Matrix<T>::operator-()const
{
for (int i = 0; i < rows * cols; i++)
{
element[i] -= 1;
}
}
int main()
{
Matrix<int>obj1(2, 2), obj2(2, 2), obj3(2, 2);
cout << "A矩阵:" << endl;
obj1(1, 1) = 4;
obj1(2, 1) = 3;
obj1(1, 2) = 2;
obj1(2, 2) = 1;
obj1.print();
cout << "B矩阵:" << endl;
obj2(1, 1) = 1;
obj2(2, 1) = 3;
obj2(1, 2) = 2;
obj2(2, 2) = 4;
obj2.print();
cout << "A+B矩阵:" << endl;
obj3 = obj1 + obj2;
obj3.print();
cout << "A-B矩阵:" << endl;
obj3 = obj1 - obj2;
obj3.print();
cout << "A*B矩阵:" << endl;
obj3 = obj1 * obj2;
obj3.print();
system("pause");
return 0;
}