实现泛型矩阵的基本运算。 首先,定义一个矩阵类作为基类,类中需要包含维度值 m (行)和 n (列)。并在构造函数中动态申请内存空间,实例化 m * n 的 int 型二维数组,重载+和﹣作为矩阵加减法运算符,包含按照 m 行 n 列打印出矩阵的 show 方法,析构函数包含动态释放内存空间。 接下来,定义一个4*4方阵类作为矩阵类的派生类,包含成员方法求行列式的值,包含成员方法左乘矩阵打印出结果,注意规格检查。 最后,定义一个主函数,随机生成5*6矩阵 A ,5*6矩阵 B ,5*6矩阵 C ,计算 a + b , a - c ;随机生成4*4方阵 D ,4*4方阵 E ,计算 D * E ,计算 D 和 E 的行列式,判断 D * A 的可计算性。
时间: 2024-02-09 21:08:18 浏览: 63
以下是实现泛型矩阵的基本运算的代码,其中包括矩阵类和方阵类的实现,以及主函数。
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
template <typename T>
class Matrix {
protected:
int m, n;
T **data;
public:
Matrix(int m, int n) {
this->m = m;
this->n = n;
data = new T *[m];
for (int i = 0; i < m; i++) {
data[i] = new T[n];
for (int j = 0; j < n; j++) {
data[i][j] = 0;
}
}
}
Matrix(const Matrix &matrix) {
m = matrix.m;
n = matrix.n;
data = new T *[m];
for (int i = 0; i < m; i++) {
data[i] = new T[n];
for (int j = 0; j < n; j++) {
data[i][j] = matrix.data[i][j];
}
}
}
virtual ~Matrix() {
for (int i = 0; i < m; i++) {
delete[] data[i];
}
delete[] data;
}
Matrix &operator=(const Matrix &matrix) {
if (&matrix != this) {
for (int i = 0; i < m; i++) {
delete[] data[i];
}
delete[] data;
m = matrix.m;
n = matrix.n;
data = new T *[m];
for (int i = 0; i < m; i++) {
data[i] = new T[n];
for (int j = 0; j < n; j++) {
data[i][j] = matrix.data[i][j];
}
}
}
return *this;
}
Matrix operator+(const Matrix &matrix) const {
Matrix result(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result.data[i][j] = data[i][j] + matrix.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix &matrix) const {
Matrix result(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result.data[i][j] = data[i][j] - matrix.data[i][j];
}
}
return result;
}
void show() const {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << data[i][j] << "\t";
}
cout << endl;
}
}
};
template <typename T>
class SquareMatrix : public Matrix<T> {
public:
SquareMatrix(int n) : Matrix<T>(n, n) {
}
SquareMatrix(const SquareMatrix &matrix) : Matrix<T>(matrix) {
}
T determinant() const {
if (this->n == 1) {
return this->data[0][0];
} else if (this->n == 2) {
return this->data[0][0] * this->data[1][1] - this->data[0][1] * this->data[1][0];
} else {
T det = 0;
for (int i = 0; i < this->n; i++) {
SquareMatrix submatrix(this->n - 1);
for (int j = 1; j < this->n; j++) {
for (int k = 0; k < this->n; k++) {
if (k < i) {
submatrix.data[j - 1][k] = this->data[j][k];
} else if (k > i) {
submatrix.data[j - 1][k - 1] = this->data[j][k];
}
}
}
T subdet = submatrix.determinant();
if (i % 2 == 0) {
det += this->data[0][i] * subdet;
} else {
det -= this->data[0][i] * subdet;
}
}
return det;
}
}
SquareMatrix operator*(const SquareMatrix &matrix) const {
if (this->n != matrix.m) {
throw "Invalid matrix dimensions";
}
SquareMatrix result(this->n);
for (int i = 0; i < this->n; i++) {
for (int j = 0; j < this->n; j++) {
T sum = 0;
for (int k = 0; k < this->n; k++) {
sum += this->data[i][k] * matrix.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
void printLeftProduct(const Matrix<T> &matrix) const {
if (this->n != matrix.m) {
throw "Invalid matrix dimensions";
}
SquareMatrix result(this->n);
for (int i = 0; i < this->n; i++) {
for (int j = 0; j < matrix.n; j++) {
T sum = 0;
for (int k = 0; k < this->n; k++) {
sum += this->data[i][k] * matrix.data[k][j];
}
cout << sum << "\t";
}
cout << endl;
}
}
};
int main() {
srand(time(NULL));
Matrix<int> A(5, 6);
Matrix<int> B(5, 6);
Matrix<int> C(5, 6);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
A.data[i][j] = rand() % 10;
B.data[i][j] = rand() % 10;
C.data[i][j] = rand() % 10;
}
}
cout << "A:" << endl;
A.show();
cout << "B:" << endl;
B.show();
cout << "C:" << endl;
C.show();
cout << "A + B:" << endl;
(A + B).show();
cout << "A - C:" << endl;
(A - C).show();
SquareMatrix<int> D(4);
SquareMatrix<int> E(4);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
D.data[i][j] = rand() % 10;
E.data[i][j] = rand() % 10;
}
}
cout << "D:" << endl;
D.show();
cout << "E:" << endl;
E.show();
cout << "D * E:" << endl;
(D * E).show();
cout << "det(D) = " << D.determinant() << endl;
cout << "det(E) = " << E.determinant() << endl;
try {
cout << "D * A:" << endl;
D.printLeftProduct(A);
} catch (const char *msg) {
cerr << msg << endl;
}
return 0;
}
```
这段代码实现了一个泛型矩阵类 `Matrix` 和一个方阵类 `SquareMatrix`,其中 `SquareMatrix` 是 `Matrix` 的派生类。`Matrix` 实现了矩阵加减法和打印矩阵的功能,`SquareMatrix` 实现了计算行列式和矩阵乘法的功能,并增加了判断矩阵规格是否匹配的检查,以及打印左乘矩阵的功能。主函数中随机生成了两个矩阵和两个方阵,并进行了加减法、乘法和求行列式等操作。
阅读全文