C++矩阵求和

【问题描述】定义矩阵类。有两个矩阵对象 a和b,均为2行3列。求两个矩阵之和。重载运算符“+",使之能用于矩阵相加,如c=a+b.
【输入形式】a=(下划线部分为输出的提示信息)

           1 2 3

           4 5 6

      b=

           6 5 4

           3 2 1
【输出形式】

           a=

           b=

           7 7 7

           7 7 7
【样例输入】

           1 2 3

           4 5 6

      

           6 5 4

           3 2 1


【样例输出】

           a=

           b=

&nb

### C++ 中实现矩阵求和的方法 在 C++ 编程语言中,可以通过多种方式实现矩阵求和的功能。以下是几种常见的方法及其对应的代码示例。 #### 方法一:使用二维数组直接相加 这种方法是最基础的方式之一,适用于简单的矩阵操作场景。通过嵌套循环遍历两个矩阵中的对应位置元素并将其累加至结果矩阵中。 ```cpp #include <iostream> using namespace std; const int ROWS = 3; const int COLS = 3; int main() { int matrix1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int matrix2[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; int resultMatrix[ROWS][COLS]; // 计算矩阵的和 for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j]; } } // 打印结果矩阵 cout << "Result Matrix:" << endl; for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { cout << resultMatrix[i][j] << " "; } cout << endl; } return 0; } ``` 此代码展示了如何定义两个固定大小的三维矩阵,并将它们逐元素相加以获得一个新的矩阵[^1]。 --- #### 方法二:利用类封装矩阵数据结构 为了提高代码可读性和扩展性,可以创建一个 `Matrix` 类来管理矩阵的数据以及提供成员函数完成各种操作,比如加法运算。 ```cpp #include <iostream> using namespace std; class Matrix { private: static const int SIZE = 3; double data[SIZE][SIZE]; public: void setElement(int row, int col, double value); void add(const Matrix& other); void display(); }; void Matrix::setElement(int row, int col, double value) { if (row >= 0 && row < SIZE && col >= 0 && col < SIZE){ data[row][col] = value; } } void Matrix::add(const Matrix& other) { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { this->data[i][j] += other.data[i][j]; } } } void Matrix::display() { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { cout << data[i][j] << "\t"; } cout << endl; } } int main(){ Matrix m1,m2; // 初始化第一个矩阵m1 m1.setElement(0,0,1); m1.setElement(0,1,2); m1.setElement(0,2,3); m1.setElement(1,0,4); m1.setElement(1,1,5); m1.setElement(1,2,6); m1.setElement(2,0,7); m1.setElement(2,1,8); m1.setElement(2,2,9); // 初始化第二个矩阵m2 m2.setElement(0,0,9); m2.setElement(0,1,8); m2.setElement(0,2,7); m2.setElement(1,0,6); m2.setElement(1,1,5); m2.setElement(1,2,4); m2.setElement(2,0,3); m2.setElement(2,1,2); m2.setElement(2,2,1); // 显示原始矩阵 cout<<"Original Matrices:"<<endl; cout<<"\nMatrix M1:\n"; m1.display(); cout<<"\nMatrix M2:\n"; m2.display(); // 加法操作 m1.add(m2); cout<<"\nSum of the two matrices is:\n"; m1.display(); return 0; } ``` 这段代码实现了更复杂的面向对象的设计模式,在实际应用中有助于维护大型项目时保持清晰逻辑关系[^4]。 --- #### 方法三:动态分配内存处理任意尺寸矩阵 如果事先不知道具体行列数目,则需借助指针或者标准库容器(如 vector)来进行动态存储空间申请。 ```cpp #include <iostream> #include <vector> std::vector<std::vector<int>> addMatrices(const std::vector<std::vector<int>>& mat1, const std::vector<std::vector<int>>& mat2) { size_t rows = mat1.size(), cols = mat1[0].size(); std::vector<std::vector<int>> res(rows, std::vector<int>(cols)); for(size_t r=0;r<rows;++r){ for(size_t c=0;c<cols;++c){ res[r][c] = mat1[r][c] + mat2[r][c]; } } return res; } void showMatrix(const std::vector<std::vector<int>>& mat){ for(auto &row : mat){ for(auto elem : row){ std::cout<<elem<<" "; } std::cout<<"\n"; } } int main(){ std::vector<std::vector<int>> A{{1,2},{3,4}}, B{{5,6},{7,8}}; auto Sum=addMatrices(A,B); showMatrix(Sum); return 0; } ``` 这里采用了 STL 的向量代替传统静态数组形式,允许灵活调整规模而无需担心越界访问等问题发生[^3]。 --- ### 总结 以上三种方案分别代表了不同层次的技术需求——从最简单直观的手动索引控制到高级抽象化的 OOP 设计思路再到现代化工具支持下的高效解决方案。开发者应根据实际情况选取最适合当前项目的策略实施开发工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值