利用运算符重载技术,实现矩阵与一个常数相乘,两个矩阵相加,两个矩阵相乘,取矩阵的指定下标的元素(m[i][j])形式并举例验证;当两个矩阵不满足乘法运算的要求时,抛出RequirementMismatch异常;
时间: 2025-01-21 19:38:03 浏览: 43
在C++中,运算符重载是一种强大的特性,可以让我们定义自定义类型的运算符行为。下面是一个示例,展示如何通过运算符重载实现矩阵与常数的乘法、矩阵加法、矩阵乘法以及矩阵元素的访问。我们还会定义一个`DimensionMismatch`异常,用于处理矩阵乘法时的维度不匹配问题。
```cpp
#include <iostream>
#include <vector>
#include <stdexcept>
class DimensionMismatch : public std::exception {
public:
const char* what() const noexcept override {
return "DimensionMismatch: Matrix dimensions do not match for the requested operation.";
}
};
class Matrix {
private:
std::vector<std::vector<double>> data;
size_t rows;
size_t cols;
public:
Matrix(size_t rows, size_t cols) : rows(rows), cols(cols), data(rows, std::vector<double>(cols, 0.0)) {}
size_t getRows() const { return rows; }
size_t getCols() const { return cols; }
double& operator()(size_t i, size_t j) {
if (i >= rows || j >= cols) {
throw std::out_of_range("Index out of range");
}
return data[i][j];
}
const double& operator()(size_t i, size_t j) const {
if (i >= rows || j >= cols) {
throw std::out_of_range("Index out of range");
}
return data[i][j];
}
Matrix operator+(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw DimensionMismatch();
}
Matrix result(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
Matrix operator*(const Matrix& other) const {
if (cols != other.rows) {
throw DimensionMismatch();
}
Matrix result(rows, other.cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < other.cols; ++j) {
for (size_t k = 0; k < cols; ++k) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
Matrix operator*(double scalar) const {
Matrix result(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result.data[i][j] = data[i][j] * scalar;
}
}
return result;
}
friend Matrix operator*(double scalar, const Matrix& matrix) {
return matrix * scalar;
}
void print() const {
for (const auto& row : data) {
for (const auto& elem : row) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
}
};
int main() {
Matrix A(2, 3);
Matrix B(3, 2);
Matrix C(2, 3);
// Initialize matrices
for (size_t i = 0; i < A.getRows(); ++i) {
for (size_t j = 0; j < A.getCols(); ++j) {
A(i, j) = i + j;
}
}
for (size_t i = 0; i < B.getRows(); ++i) {
for (size_t j = 0; j < B.getCols(); ++j) {
B(i, j) = i * j;
}
}
for (size_t i = 0; i < C.getRows(); ++i) {
for (size_t j = 0; j < C.getCols(); ++j) {
C(i, j) = i * j;
}
}
// Matrix addition
try {
Matrix D = A + C;
std::cout << "Matrix A + C:" << std::endl;
D.print();
} catch (const DimensionMismatch& e) {
std::cout << e.what() << std::endl;
}
// Matrix multiplication
try {
Matrix E = A * B;
std::cout << "Matrix A * B:" << std::endl;
E.print();
} catch (const DimensionMismatch& e) {
std::cout << e.what() << std::endl;
}
// Matrix-scalar multiplication
Matrix F = A * 2.0;
std::cout << "Matrix A * 2.0:" << std::endl;
F.print();
return 0;
}
```
### 解释
1. **异常类`DimensionMismatch`**:
- 继承自`std::exception`,并重写了`what()`方法,返回错误信息。
2. **矩阵类`Matrix`**:
- 使用`std::vector`存储矩阵数据。
- 重载了`()`运算符,用于访问矩阵元素。
- 重载了`+`运算符,用于矩阵加法。
- 重载了`*`运算符,用于矩阵乘法和矩阵与常数的乘法。
- 提供了`print()`方法,用于打印矩阵。
3. **主函数`main`**:
- 初始化三个矩阵`A`, `B`, `C`。
- 进行矩阵加法和乘法,并处理可能的`DimensionMismatch`异常。
- 进行矩阵与常数的乘法。
阅读全文
相关推荐















