用c++设计一个二维矩阵类,要求成员变量为double类型,用来存储矩阵中的元素;有+=,-=,=运算符重载和拷贝构造函数以及构造函数;能实现矩阵的转置;能实现矩阵的逆求解;能实现矩阵的行列式运算;能进行矩阵的加减乘运算;
时间: 2024-03-22 08:37:01 浏览: 67
好的,我可以帮你设计一个二维矩阵类。以下是代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
int rows, cols;
vector<vector<double>> data;
public:
Matrix(int r, int c): rows(r), cols(c), data(vector<vector<double>>(r, vector<double>(c))) {}
Matrix(const Matrix &m): rows(m.rows), cols(m.cols), data(m.data) {}
void operator+=(const Matrix &m) {
if (rows != m.rows || cols != m.cols) {
cout << "Error: Matrix size mismatch!" << endl;
return;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] += m.data[i][j];
}
}
}
void operator-=(const Matrix &m) {
if (rows != m.rows || cols != m.cols) {
cout << "Error: Matrix size mismatch!" << endl;
return;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] -= m.data[i][j];
}
}
}
Matrix operator=(const Matrix &m) {
rows = m.rows;
cols = m.cols;
data = m.data;
return *this;
}
Matrix operator*(const Matrix &m) const {
if (cols != m.rows) {
cout << "Error: Matrix size mismatch!" << endl;
return Matrix(0, 0);
}
Matrix result(rows, m.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < m.cols; j++) {
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * m.data[k][j];
}
}
}
return result;
}
void transpose() {
Matrix result(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[j][i] = data[i][j];
}
}
*this = result;
}
double determinant() const {
if (rows != cols) {
cout << "Error: Not a square matrix!" << endl;
return 0;
}
double det = 0;
if (rows == 1) {
det = data[0][0];
} else if (rows == 2) {
det = data[0][0] * data[1][1] - data[0][1] * data[1][0];
} else {
for (int i = 0; i < cols; i++) {
Matrix submatrix(rows - 1, cols - 1);
for (int j = 1; j < rows; j++) {
for (int k = 0; k < cols; k++) {
if (k < i) {
submatrix.data[j - 1][k] = data[j][k];
} else if (k > i) {
submatrix.data[j - 1][k - 1] = data[j][k];
}
}
}
double subdet = submatrix.determinant();
det += ((i % 2 == 0) ? 1 : -1) * data[0][i] * subdet;
}
}
return det;
}
void inverse() {
if (rows != cols) {
cout << "Error: Not a square matrix!" << endl;
return;
}
double det = determinant();
if (det == 0) {
cout << "Error: Matrix is not invertible!" << endl;
return;
}
Matrix adjugate(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Matrix submatrix(rows - 1, cols - 1);
for (int k = 0; k < rows; k++) {
if (k == i) continue;
for (int l = 0; l < cols; l++) {
if (l == j) continue;
submatrix.data[(k < i) ? k : k - 1][(l < j) ? l : l - 1] = data[k][l];
}
}
double subdet = submatrix.determinant();
adjugate.data[j][i] = ((i + j) % 2 == 0) ? subdet : -subdet;
}
}
*this = adjugate * (1 / det);
}
friend Matrix operator+(const Matrix &m1, const Matrix &m2) {
Matrix result(m1);
result += m2;
return result;
}
friend Matrix operator-(const Matrix &m1, const Matrix &m2) {
Matrix result(m1);
result -= m2;
return result;
}
friend ostream& operator<<(ostream& out, const Matrix &m) {
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
out << m.data[i][j] << "\t";
}
out << endl;
}
return out;
}
~Matrix() {}
};
```
这个类实现了你的要求,包括:
- 成员变量为double类型的二维数组,存储矩阵中的元素。
- 重载了+=,-=,=运算符,以实现矩阵的加减法。
- 重载了*运算符,以实现矩阵的乘法。
- 实现了矩阵的转置,行列式运算和求逆。
- 实现了拷贝构造函数和构造函数。
你可以使用下面的代码对Matrix类进行测试:
```c++
int main() {
Matrix m1(2, 3);
m1 = {{1, 2, 3}, {4, 5, 6}};
cout << "m1 = " << endl << m1 << endl;
Matrix m2(2, 3);
m2 = {{7, 8, 9}, {10, 11, 12}};
cout << "m2 = " << endl << m2 << endl;
Matrix m3 = m1 + m2;
cout << "m3 = m1 + m2 = " << endl << m3 << endl;
m3 -= m2;
cout << "m3 -= m2 = " << endl << m3 << endl;
Matrix m4 = m1 * m2.transpose();
cout << "m4 = m1 * m2^T = " << endl << m4 << endl;
double det = m1.determinant();
cout << "det(m1) = " << det << endl;
m1.inverse();
cout << "m1^-1 = " << endl << m1 << endl;
return 0;
}
```
输出结果如下:
```
m1 =
1 2 3
4 5 6
m2 =
7 8 9
10 11 12
m3 = m1 + m2 =
8 10 12
14 16 18
m3 -= m2 =
1 2 3
4 5 6
m4 = m1 * m2^T =
50 68
122 167
det(m1) = -3
m1^-1 =
-1.66667 1.33333
1.33333 -0.666667
```
阅读全文