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`,并演示了如何使用重载运算符来执行矩阵加减乘除运算。

相关推荐

最新推荐

recommend-type

C++ 类的赋值运算符''=''重载的方法实现

主要介绍了C++ 类的赋值运算符'='重载的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

c++运算符重载矩阵运算

c++运算符重载矩阵运算 题目:对具有同行列数的整形矩阵进行加、减、乘、转置、赋值运算
recommend-type

C++实现图的邻接矩阵表示

主要为大家详细介绍了C++实现图的邻接矩阵表示,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++ Eigen库计算矩阵特征值及特征向量

主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++简单集合类的实现方法

如何使用C++实现一个简单的集合类,这篇文章主要介绍了C++简单集合类的实现方法,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。