使用c++写一个矩阵类CMatrix,基本要求如下: ·支持任意大小的矩阵:行数和列数可以动态设置(不用修改代码) 重载操作符*,实现矩阵相乘 重载操作符++和一,分别实现矩阵元素同时+1和-1 在main函数中对以上功能进行调用
时间: 2024-03-06 10:52:03 浏览: 24
好的,我会根据您的要求给出 C++ 代码实现。以下是 CMatrix 类的实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
class CMatrix {
public:
CMatrix(int rows, int cols) : m_rows(rows), m_cols(cols) {
m_data.resize(rows, vector<int>(cols, 0));
}
int getRows() const {
return m_rows;
}
int getCols() const {
return m_cols;
}
int& operator()(int i, int j) {
return m_data[i][j];
}
const int& operator()(int i, int j) const {
return m_data[i][j];
}
CMatrix operator*(const CMatrix& other) const {
if (m_cols != other.m_rows) {
throw std::invalid_argument("Columns of left matrix must match rows of right matrix.");
}
CMatrix result(m_rows, other.m_cols);
for (int i = 0; i < m_rows; i++) {
for (int j = 0; j < other.m_cols; j++) {
for (int k = 0; k < m_cols; k++) {
result(i, j) += m_data[i][k] * other(k, j);
}
}
}
return result;
}
CMatrix& operator++() {
for (int i = 0; i < m_rows; i++) {
for (int j = 0; j < m_cols; j++) {
m_data[i][j]++;
}
}
return *this;
}
CMatrix& operator--() {
for (int i = 0; i < m_rows; i++) {
for (int j = 0; j < m_cols; j++) {
m_data[i][j]--;
}
}
return *this;
}
private:
int m_rows;
int m_cols;
vector<vector<int>> m_data;
};
int main() {
CMatrix m1(2, 3);
m1(0, 0) = 1;
m1(0, 1) = 2;
m1(0, 2) = 3;
m1(1, 0) = 4;
m1(1, 1) = 5;
m1(1, 2) = 6;
CMatrix m2(3, 2);
m2(0, 0) = 7;
m2(0, 1) = 8;
m2(1, 0) = 9;
m2(1, 1) = 10;
m2(2, 0) = 11;
m2(2, 1) = 12;
CMatrix m3 = m1 * m2;
cout << "m1:" << endl;
for (int i = 0; i < m1.getRows(); i++) {
for (int j = 0; j < m1.getCols(); j++) {
cout << m1(i, j) << " ";
}
cout << endl;
}
cout << "m2:" << endl;
for (int i = 0; i < m2.getRows(); i++) {
for (int j = 0; j < m2.getCols(); j++) {
cout << m2(i, j) << " ";
}
cout << endl;
}
cout << "m3:" << endl;
for (int i = 0; i < m3.getRows(); i++) {
for (int j = 0; j < m3.getCols(); j++) {
cout << m3(i, j) << " ";
}
cout << endl;
}
++m3;
cout << "m3 after increment:" << endl;
for (int i = 0; i < m3.getRows(); i++) {
for (int j = 0; j < m3.getCols(); j++) {
cout << m3(i, j) << " ";
}
cout << endl;
}
--m3;
cout << "m3 after decrement:" << endl;
for (int i = 0; i < m3.getRows(); i++) {
for (int j = 0; j < m3.getCols(); j++) {
cout << m3(i, j) << " ";
}
cout << endl;
}
return 0;
}
```
在上面的代码中,我们定义了一个 CMatrix 类,它支持任意大小的矩阵,并重载了操作符 *、++ 和 --。在 main 函数中,我们创建了两个矩阵 m1 和 m2,分别进行了赋值。然后我们计算了 m1 和 m2 的乘积,存储在 m3 中,并输出了结果。接着我们对 m3 进行了 ++ 和 -- 操作,并输出了结果。
希望这个代码对您有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)