有两个矩阵a和b,均为n行m列(m和n的至自己给出),求两矩阵之和、差、积、商,重载运算符“+”、“-”、“*”、“\”,使之能用于矩阵向加减乘除,如c=a+b、c=a*b。用c++写出这段代码
时间: 2024-11-09 14:26:05 浏览: 59
在C++中,为了能够对矩阵进行加法、减法、乘法以及除法操作,你需要创建一个自定义的`Matrix`类,并重载相应的运算符。这里是一个简单的例子:
```cpp
#include <iostream>
#include <vector>
class Matrix {
private:
int m, n;
std::vector<std::vector<int>> data;
public:
// 构造函数
Matrix(int _m, int _n) : m(_m), n(_n) {
data.resize(m, std::vector<int>(n, 0));
}
// 获取元素
int& operator()(int i, int j) { return data[i][j]; }
const int& operator()(int i, int j) const { return data[i][j]; }
// 加法
Matrix operator+(const Matrix& other) {
if (m != other.m || n != other.n) {
throw std::invalid_argument("Matrices must have the same dimensions for addition.");
}
Matrix result(m, n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
result(i, j) = data[i][j] + other.data[i][j];
}
}
return result;
}
// 减法
Matrix operator-(const Matrix& other) {
if (m != other.m || n != other.n) {
throw std::invalid_argument("Matrices must have the same dimensions for subtraction.");
}
Matrix result(m, n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
result(i, j) = data[i][j] - other.data[i][j];
}
}
return result;
}
// 数学乘法
Matrix operator*(const Matrix& other) {
if (n != other.m) {
throw std::invalid_argument("Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.");
}
Matrix result(m, other.n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < other.n; ++j) {
int sum = 0;
for (int k = 0; k < n; ++k) {
sum += data[i][k] * other.data[k][j];
}
result(i, j) = sum;
}
}
return result;
}
// 除法,假设矩阵是对称正定的,可以避免除数为零的问题
Matrix divide(const Matrix& other) {
if (other == Matrix(n, n)) {
throw std::runtime_error("Cannot divide by zero.");
}
return *this * other.inverse();
}
// 返回逆矩阵,仅限于方阵(n=m)
Matrix inverse() {
if (n != m) {
throw std::runtime_error("Only square matrices can have an inverse.");
}
// 实现矩阵的逆,这里省略了计算细节
// 可参考高斯消元法或其他算法
return Matrix(n, n); // 这里只是占位,实际需要实现矩阵逆的计算
}
// 打印矩阵
void print() {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
std::cout << data[i][j] << " ";
}
std::cout << "\n";
}
}
};
// 主程序测试
int main() {
Matrix a(2, 2), b(2, 2);
a.print(); // 输入矩阵a
b.print(); // 输入矩阵b
Matrix c = a + b;
c.print(); cout << "Matrix addition: \n";
c = a - b;
c.print(); cout << "Matrix subtraction: \n";
c = a * b;
c.print(); cout << "Matrix multiplication: \n";
try {
c = a.divide(b);
c.print(); cout << "Matrix division: \n";
} catch (const std::exception& e) {
cerr << "Error: " << e.what() << endl;
}
return 0;
}
```
这个示例展示了如何定义矩阵类并实现基本的算术操作。请注意,实际的矩阵运算(例如除法和逆矩阵)可能会更复杂,特别是涉及到数值稳定性的问题。上述代码只是一个基础演示,实际应用中可能需要进一步完善。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)