矩阵类的加减乘除运算的实现c++
时间: 2023-08-12 21:08:55 浏览: 57
以下是一个简单的矩阵类,包括加减乘除运算的实现:
```c++
#include<iostream>
#include<vector>
using namespace std;
class Matrix {
private:
int rows; //行数
int cols; //列数
vector<vector<double>> data; //矩阵数据
public:
Matrix(int rows, int cols, double init = 0.0) {
this->rows = rows;
this->cols = cols;
data.resize(rows);
for (int i = 0; i < rows; i++) {
data[i].resize(cols, init);
}
}
Matrix operator+(Matrix &other) {
if (this->rows != other.rows || this->cols != other.cols) {
cerr << "Error: Matrix dimensions must match." << endl;
exit(1);
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = this->data[i][j] + other.data[i][j];
}
}
return result;
}
Matrix operator-(Matrix &other) {
if (this->rows != other.rows || this->cols != other.cols) {
cerr << "Error: Matrix dimensions must match." << endl;
exit(1);
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = this->data[i][j] - other.data[i][j];
}
}
return result;
}
Matrix operator*(Matrix &other) {
if (this->cols != other.rows) {
cerr << "Error: Matrix dimensions must match." << endl;
exit(1);
}
Matrix result(this->rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
double sum = 0.0;
for (int k = 0; k < cols; k++) {
sum += this->data[i][k] * other.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
Matrix operator/(double scalar) {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = this->data[i][j] / scalar;
}
}
return result;
}
void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
Matrix a(2, 3, 1.0);
Matrix b(3, 2, 2.0);
Matrix c = a + a;
Matrix d = a - a;
Matrix e = a * b;
Matrix f = a / 2.0;
c.print();
d.print();
e.print();
f.print();
return 0;
}
```
在矩阵类中,重载了加减乘除运算符,分别对应矩阵的加减乘除操作。其中,加减操作需要保证两个矩阵的维度相同,乘法操作需要保证左矩阵的列数等于右矩阵的行数,除法操作则是将矩阵中的每个元素除以一个标量。
在 `main` 函数中,创建了两个矩阵 `a` 和 `b`,分别为 $2 \times 3$ 和 $3 \times 2$ 的矩阵。通过对矩阵进行加减乘除操作,得到了新的矩阵 `c`、`d`、`e` 和 `f`,并分别输出到屏幕上。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)