c++利用类重载矩阵加减乘除
时间: 2023-08-13 21:02:23 浏览: 43
好的,我可以通过类重载运算符来实现矩阵的加减乘除。下面是一个简单的示例代码:
```c++
#include <iostream>
using namespace std;
class Matrix {
private:
int rows;
int cols;
double* data;
public:
Matrix(int m, int n) : rows(m), cols(n), data(new double[m*n]) {}
Matrix(const Matrix& mat) : rows(mat.rows), cols(mat.cols), data(new double[mat.rows * mat.cols]) {
for (int i = 0; i < rows * cols; i++) {
data[i] = mat.data[i];
}
}
~Matrix() { delete[] data; }
double& operator()(int i, int j) {
return data[i*cols + j];
}
const double& operator()(int i, int j) const {
return data[i*cols + j];
}
Matrix& operator=(const Matrix& mat) {
if (this != &mat) {
delete[] data;
rows = mat.rows;
cols = mat.cols;
data = new double[rows * cols];
for (int i = 0; i < rows * cols; i++) {
data[i] = mat.data[i];
}
}
return *this;
}
Matrix operator+(const Matrix& mat) const {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result(i, j) = (*this)(i, j) + mat(i, j);
}
}
return result;
}
Matrix operator-(const Matrix& mat) const {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result(i, j) = (*this)(i, j) - mat(i, j);
}
}
return result;
}
Matrix operator*(const Matrix& mat) const {
if (cols != mat.rows) {
throw runtime_error("Matrix dimensions do not match for multiplication.");
}
Matrix result(rows, mat.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < mat.cols; j++) {
double sum = 0;
for (int k = 0; k < cols; k++) {
sum += (*this)(i, k) * mat(k, j);
}
result(i, j) = sum;
}
}
return result;
}
Matrix operator/(const Matrix& mat) const {
if (mat.rows != mat.cols || rows != cols || mat.rows != rows) {
throw runtime_error("Matrix dimensions do not match for division.");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result(i, j) = (*this)(i, j) / mat(i, j);
}
}
return result;
}
friend ostream& operator<<(ostream& os, const Matrix& mat) {
for (int i = 0; i < mat.rows; i++) {
for (int j = 0; j < mat.cols; j++) {
os << mat(i, j) << " ";
}
os << endl;
}
return os;
}
};
int main() {
Matrix A(2, 3);
A(0, 0) = 1;
A(0, 1) = 2;
A(0, 2) = 3;
A(1, 0) = 4;
A(1, 1) = 5;
A(1, 2) = 6;
cout << "Matrix A:" << endl;
cout << A << endl;
Matrix B(3, 2);
B(0, 0) = 1;
B(0, 1) = 4;
B(1, 0) = 2;
B(1, 1) = 5;
B(2, 0) = 3;
B(2, 1) = 6;
cout << "Matrix B:" << endl;
cout << B << endl;
Matrix C = A + B;
cout << "A + B:" << endl;
cout << C << endl;
Matrix D = A - B;
cout << "A - B:" << endl;
cout << D << endl;
Matrix E = A * B;
cout << "A * B:" << endl;
cout << E << endl;
Matrix F(2, 3);
F(0, 0) = 2;
F(0, 1) = 4;
F(0, 2) = 6;
F(1, 0) = 8;
F(1, 1) = 10;
F(1, 2) = 12;
cout << "Matrix F:" << endl;
cout << F << endl;
Matrix G = A / F;
cout << "A / F:" << endl;
cout << G << endl;
return 0;
}
```
在这个示例中,我们定义了一个 `Matrix` 类,并重载了 `operator+`、`operator-`、`operator*` 和 `operator/` 运算符。这些运算符可以用来执行矩阵加减乘除运算。我们还重载了括号运算符 `()`,以方便访问矩阵元素。
在 `main` 函数中,我们创建了两个矩阵 `A` 和 `B`,并演示了如何使用重载运算符来执行矩阵加减乘除运算。