1、 设计一个二维矩阵类 2、 成员变量为double类型,用来存储矩阵中的元素 3、 写出赋值运算符重载(包括+=,-=,=),拷贝构造函数和构造函数 4, 矩阵的加减乘操作,并写出main函数进行调试
时间: 2024-03-25 20:37:06 浏览: 60
好的,让我来回答您的问题。
1. 二维矩阵类的设计
```c++
class Matrix {
private:
int rows_;
int cols_;
double** data_;
public:
// 构造函数
Matrix(int rows, int cols) : rows_(rows), cols_(cols) {
data_ = new double*[rows_];
for (int i = 0; i < rows_; ++i) {
data_[i] = new double[cols_];
memset(data_[i], 0, sizeof(double) * cols_);
}
}
// 拷贝构造函数
Matrix(const Matrix& other) {
rows_ = other.rows_;
cols_ = other.cols_;
data_ = new double*[rows_];
for (int i = 0; i < rows_; ++i) {
data_[i] = new double[cols_];
memcpy(data_[i], other.data_[i], sizeof(double) * cols_);
}
}
// 赋值运算符重载
Matrix& operator=(const Matrix& other) {
if (this != &other) {
for (int i = 0; i < rows_; ++i) {
delete[] data_[i];
}
delete[] data_;
rows_ = other.rows_;
cols_ = other.cols_;
data_ = new double*[rows_];
for (int i = 0; i < rows_; ++i) {
data_[i] = new double[cols_];
memcpy(data_[i], other.data_[i], sizeof(double) * cols_);
}
}
return *this;
}
// += 运算符重载
Matrix& operator+=(const Matrix& other) {
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
data_[i][j] += other.data_[i][j];
}
}
return *this;
}
// -= 运算符重载
Matrix& operator-=(const Matrix& other) {
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
data_[i][j] -= other.data_[i][j];
}
}
return *this;
}
// = 运算符重载
Matrix& operator=(double value) {
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
data_[i][j] = value;
}
}
return *this;
}
// 析构函数
~Matrix() {
for (int i = 0; i < rows_; ++i) {
delete[] data_[i];
}
delete[] data_;
}
// 获取矩阵行数
int rows() const {
return rows_;
}
// 获取矩阵列数
int cols() const {
return cols_;
}
// 获取矩阵元素
double& operator()(int row, int col) {
return data_[row][col];
}
const double& operator()(int row, int col) const {
return data_[row][col];
}
};
```
2. 矩阵加减乘操作
```c++
// 矩阵加法
Matrix operator+(const Matrix& a, const Matrix& b) {
Matrix result(a.rows(), a.cols());
for (int i = 0; i < a.rows(); ++i) {
for (int j = 0; j < a.cols(); ++j) {
result(i, j) = a(i, j) + b(i, j);
}
}
return result;
}
// 矩阵减法
Matrix operator-(const Matrix& a, const Matrix& b) {
Matrix result(a.rows(), a.cols());
for (int i = 0; i < a.rows(); ++i) {
for (int j = 0; j < a.cols(); ++j) {
result(i, j) = a(i, j) - b(i, j);
}
}
return result;
}
// 矩阵乘法
Matrix operator*(const Matrix& a, const Matrix& b) {
Matrix result(a.rows(), b.cols());
for (int i = 0; i < a.rows(); ++i) {
for (int j = 0; j < b.cols(); ++j) {
for (int k = 0; k < a.cols(); ++k) {
result(i, j) += a(i, k) * b(k, j);
}
}
}
return result;
}
```
3. main函数进行调试
```c++
int main() {
Matrix a(2, 2);
a(0, 0) = 1;
a(0, 1) = 2;
a(1, 0) = 3;
a(1, 1) = 4;
Matrix b(2, 2);
b(0, 0) = 5;
b(0, 1) = 6;
b(1, 0) = 7;
b(1, 1) = 8;
Matrix c = a + b;
Matrix d = a - b;
Matrix e = a * b;
cout << "a + b:" << endl;
for (int i = 0; i < c.rows(); ++i) {
for (int j = 0; j < c.cols(); ++j) {
cout << c(i, j) << " ";
}
cout << endl;
}
cout << "a - b:" << endl;
for (int i = 0; i < d.rows(); ++i) {
for (int j = 0; j < d.cols(); ++j) {
cout << d(i, j) << " ";
}
cout << endl;
}
cout << "a * b:" << endl;
for (int i = 0; i < e.rows(); ++i) {
for (int j = 0; j < e.cols(); ++j) {
cout << e(i, j) << " ";
}
cout << endl;
}
return 0;
}
```
希望能帮到您,如有疑问请随时提出。
阅读全文