zoukankan      html  css  js  c++  java
  • C++ 实现二维矩阵的加减乘等运算

    Matrix.h
    #include "iostream" using namespace std; class Matrix { private: int row, list; double **HL; public: Matrix(int r_ = 0, int l_ = 0); Matrix(int r_, int l_, double **newone); Matrix(const Matrix & rhs); ~Matrix(); Matrix operator + (const Matrix & rhs); Matrix operator - (const Matrix & rhs); Matrix operator = (const Matrix & rhs); Matrix operator * (const Matrix & rhs); friend ostream & operator << (ostream & os, const Matrix & rhs); };
    Matrix.cpp
    #include "Matrix.h" int i, j; Matrix::Matrix(int r_, int l_):row(r_),list(l_) { HL = new double *[row]; for(i = 0; i < row; i++) { HL[i] = new double [list]; } cout<<"please enter Matrix :"<<endl; for(i = 0; i < row; i++) { for(j=0; j<list; j++) { cin>>HL[i][j]; } } } Matrix::Matrix(int r_, int l_, double **newone):row(r_),list(l_) //构造函数重载, //主要用于加法减法的return使用 { HL = new double *[row]; for(i = 0; i< row; i++) { HL[i] = new double [list]; } for(i = 0; i <row; i++) { for(j = 0; j<list; j++) { HL[i][j] = newone[i][j]; } } } Matrix::Matrix(const Matrix & rhs) { if(this != & rhs) { this->row = rhs.row; this->list = rhs.list; HL = new double *[row]; for(i = 0; i<row; i++) { HL[i] = new double [list]; } for(i = 0; i<row; i++) { for(j = 0; j<list; j++) { this->HL[i][j] = rhs.HL[i][j]; } } } } Matrix::~Matrix() //析构函数,删除开辟的空间 { cout<<"~ Matrix : row = "<<row<<", list = "<<list<<endl<<endl; for(i = 0; i<row; i++) { delete [] HL[i]; } delete [] HL; } Matrix Matrix::operator + (const Matrix & rhs) { if( (this->row == rhs.row) && (this->list == rhs.list)) { double **newone; int r_, l_; r_ = row; l_ = list; newone = new double *[row]; for(i = 0; i < row; i++) { newone[i] = new double [list]; } for(i = 0; i < row; i++) { for(j = 0; j<list; j++) { newone[i][j] = HL[i][j]+rhs.HL[i][j]; } } return Matrix(r_, l_, newone); } } Matrix Matrix::operator - (const Matrix & rhs) { if((this->row == rhs.row) && (this->list == rhs.list)) { double **newone; int r_, l_; r_ = row; l_ = list; newone = new double *[row]; for(i = 0; i<row;i++) { newone[i] = new double [list]; } for(i=0;i<row;i++) { for(j = 0; j<list; j++) { newone[i][j] = HL[i][j]+rhs.HL[i][j]; } } return Matrix(r_, l_, newone); } } Matrix Matrix::operator * (const Matrix& rhs) { if((this->row = rhs.row) && (this->list = rhs.list)) { double **newone; int r_, l_; r_ = row; l_ = list; newone = new double *[row]; for(i = 0; i<row; i++) { newone[i]=new double [list]; } for(i = 0;i<row;i++) { for(j = 0; j<list; j++) { newone[i][j]=0; if(i == j) { for(int k = 0; k<list; k++) { newone[i][j] = newone[i][j] + HL[i][k]*rhs.HL[k][j]; } } else { for(int k = 0; k<list; k++) { newone[i][j] = newone[i][j] + HL[i][k]*rhs.HL[k][j]; } } } } return Matrix(r_,l_,newone); } } Matrix Matrix::operator = (const Matrix & rhs) { if((this->row = rhs.row) && (this->list == rhs.list)) { for(i = 0; i<row; i++) { for(j = 0; j<list; j++) { this->HL[i][j] = rhs.HL[i][j]; } } return (*this); } } ostream & operator << (ostream & os, const Matrix & rhs) { os<<"Matrix: row="<<rhs.row<<" , list = "<<rhs.list<<endl; for(i = 0; i<rhs.row; i++) { for(j = 0; j<rhs.list; j++) { os<<rhs.HL[i][j]<<" "; } os<<endl; } return os; } int main() { int m,n,x,y; cin>>n>>m>>x>>y; Matrix aa(n,m), bb(n,m), cc(n,m), dd(x,y); cout<<endl<<aa<<endl<<bb<<endl<<cc<<endl<<dd<<endl; cout<<(aa+bb+cc)<<endl<<(cc*bb)<<endl; return 0; }
  • 相关阅读:
    分布式架构高可用架构篇_activemq高可用集群(zookeeper+leveldb)安装、配置、高可用测试
    @interface [SpringMVC+redis]自定义aop注解实现控制器访问次数限制
    ActiveMQ安装与持久化消息
    activemq 5.13.2 jdbc 数据库持久化 异常 找不到驱动程序
    java通过Comparable接口实现字符串比较大小排序的简单实例
    微信小程序--火车票查询
    【调试】如何使用javascript的debugger命令进行调试(重要)
    【调试】js调试console.log使用总结图解(重要)
    ajax提交表单
    一个项目的404错误处理页面
  • 原文地址:https://www.cnblogs.com/zhibei/p/12203351.html
Copyright © 2011-2022 走看看