zoukankan      html  css  js  c++  java
  • C++中Matrix(矩阵)的基本运算( +、-、=、<<)

    利用二维指针开辟空间形成二维数组;

    原题为设计一个Matrix类,实现基本的矩阵运算;

    初次设计为HL[10][10]数组,存放矩阵元素,后改为二维指针;

    主要问题存在于二维指针理解的不透彻,无法理解其开辟空间的方法;

    HL = new double *[row];
    for(i = 0;i < row;i++)
        HL[i] = new double [list];

    其次对于不同类型矩阵相加没有找到合适的处理方式,只能手动控制不使不同类型矩阵相加或相减;
    其中 row 为行,list为列,HL为存放矩阵的二维指针;

    附上一个new运算符的用法;

    https://www.cnblogs.com/2015-16/p/11782595.html
      1 #include<iostream>
      2 using namespace std;
      3 
      4 class Matrix
      5 {
      6     private:
      7         int row,list;
      8         double **HL;
      9     public:
     10         Matrix(int r_ = 0, int l_ = 0);
     11         Matrix(int r_ , int l_ , double **newone);
     12         Matrix(const Matrix & rhs);
     13         ~Matrix();
     14         Matrix operator + (const Matrix & rhs);
     15         Matrix operator - (const Matrix & rhs);
     16         Matrix operator = (const Matrix & rhs);
     17         friend ostream & operator << (ostream & os , const Matrix & rhs);
     18 };
     19 
     20 int i,j;
     21 
     22 Matrix::Matrix(int r_ , int l_):row(r_),list(l_)  //构造函数
     23 {
     24     HL = new double *[row];
     25     for(i = 0;i < row;i++)
     26         HL[i] = new double [list]; 
     27     cout<<"please enter Matrix :"<<endl;
     28     for(i = 0;i < row;i++)
     29         for(j = 0;j < list;j++)
     30             cin>>HL[i][j];
     31 } 
     32 
     33 Matrix::Matrix(int r_ , int l_ , double **newone ):row(r_),list(l_)  //构造函数重载,主要用于加法减法中的return使用
     34 {
     35     HL = new double *[row];
     36     for(i = 0;i < row;i++)
     37         HL[i] = new double [list]; 
     38     for(i = 0;i < row;i++)
     39         for(j = 0;j < list;j++)
     40             HL[i][j] = newone[i][j];
     41 } 
     42 
     43 Matrix::Matrix(const Matrix & rhs)
     44 {
     45     if(this != & rhs)
     46     {
     47         this->row = rhs.row;
     48         this->list = rhs.list;
     49         HL = new double *[row];
     50         for(i = 0;i < row;i++)
     51             HL[i] = new double [list]; 
     52         for(i = 0;i < row;i++)
     53             for(j = 0;j < list;j++)
     54                 this->HL[i][j] = rhs.HL[i][j];
     55     }
     56 }
     57 
     58 Matrix::~Matrix()    // 析构函数,删除开辟的空间
     59 {
     60     cout<<"~ Matrix : row ="<<row<<" , list = "<<list<<endl<<endl;
     61     for(i = 0;i < row;i++)
     62         delete [] HL[i];
     63     delete [] HL;
     64 }
     65 
     66 Matrix Matrix::operator + (const Matrix & rhs)
     67 {
     68     if( (this->row == rhs.row)&&(this->list == rhs.list) )
     69     {
     70         double **newone;
     71         int r_,l_;
     72         r_ = row;l_ = list;
     73         newone = new double *[row];
     74         for(i = 0;i < row;i++)
     75             newone[i] = new double [list]; 
     76         for(i = 0;i < row;i++)
     77             for(j = 0;j < list;j++)
     78                 newone[i][j] = HL[i][j] + rhs.HL[i][j];
     79         return Matrix(r_,l_,newone);
     80     }
     81 //    else
     82 //        cout<<"error ——矩阵类型不符 "<<endl; 
     83 }
     84 
     85 Matrix Matrix::operator - (const Matrix & rhs)
     86 {
     87     if( (this->row == rhs.row)&&(this->list == rhs.list) )
     88     {
     89         double **newone;
     90         int r_,l_;
     91         r_ = row;l_ = list;
     92         newone = new double *[row];
     93         for(i = 0;i < row;i++)
     94             newone[i] = new double [list]; 
     95         for(i = 0;i < row;i++)
     96             for(j = 0;j < list;j++)
     97                 newone[i][j] = HL[i][j] - rhs.HL[i][j];
     98         return Matrix(r_,l_,newone);
     99     }
    100 //    else
    101 //        cout<<"error ——矩阵类型不符 "<<endl; 
    102 }
    103 
    104 Matrix Matrix::operator = (const Matrix & rhs)
    105 {
    106     if((this->row == rhs.row)&&(this->list == rhs.list))
    107     {
    108         for(i = 0;i < row;i++)
    109             for(j = 0;j < list;j++)
    110                 this->HL[i][j] = rhs.HL[i][j];
    111         return (*this); 
    112     } 
    113 //    else
    114 //        cout<<"error ——矩阵类型不符 "<<endl; 
    115 }
    116 
    117 ostream & operator << (ostream & os,const Matrix & rhs)
    118 {
    119     os<<"Matrix : row ="<<rhs.row<<" , list = "<<rhs.list<<endl;
    120     for(i = 0;i < rhs.row;i++)
    121     {
    122         for(j = 0;j < rhs.list;j++)
    123             os<<rhs.HL[i][j]<<" ";
    124         os<<endl;    
    125     }
    126     return os;
    127 }
    128 
    129 int main()
    130 {
    131     int m,n,x,y;
    132     cin>>n>>m>>x>>y;
    133     Matrix aa(n,m),bb(n,m),cc(n,m),dd(x,y);
    134     cout<<endl<<aa<<endl<<bb<<endl<<cc<<endl<<dd<<endl;
    135     cout<<(aa+bb+cc)<<endl<<(cc-bb)<<endl;
    136     return 0;
    137 } 

     2019-11-02    15:34:51

  • 相关阅读:
    简单的生成编号的存储过程
    表实体上面添加特性获取到连接字符串
    Spring 静态注入讲解(MethodInvokingFactoryBean)
    嵌套的SQL另外一种写法
    微信小程序之列表下拉加载更多
    微信小程序之--->轮播图制作
    javascript变量作用域
    struts2升级报ActionContextCleanUp<<is deprecated。Please use the new filters
    java日期加减
    数字转大写
  • 原文地址:https://www.cnblogs.com/2015-16/p/11782566.html
Copyright © 2011-2022 走看看