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

原题为设计一个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
 #include<iostream>
using namespace std; class Matrix
{
private:
int row,list;
double **HL;
public:
Matrix(int r_ = , int l_ = );
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);
friend ostream & operator << (ostream & os , const Matrix & rhs);
}; int i,j; Matrix::Matrix(int r_ , int l_):row(r_),list(l_)  //构造函数
{
HL = new double *[row];
for(i = ;i < row;i++)
HL[i] = new double [list];
cout<<"please enter Matrix :"<<endl;
for(i = ;i < row;i++)
for(j = ;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 = ;i < row;i++)
HL[i] = new double [list];
for(i = ;i < row;i++)
for(j = ;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 = ;i < row;i++)
HL[i] = new double [list];
for(i = ;i < row;i++)
for(j = ;j < list;j++)
this->HL[i][j] = rhs.HL[i][j];
}
} Matrix::~Matrix()    // 析构函数,删除开辟的空间
{
cout<<"~ Matrix : row ="<<row<<" , list = "<<list<<endl<<endl;
for(i = ;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 = ;i < row;i++)
newone[i] = new double [list];
for(i = ;i < row;i++)
for(j = ;j < list;j++)
newone[i][j] = HL[i][j] + rhs.HL[i][j];
return Matrix(r_,l_,newone);
}
// else
// cout<<"error ——矩阵类型不符 "<<endl;
} 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 = ;i < row;i++)
newone[i] = new double [list];
for(i = ;i < row;i++)
for(j = ;j < list;j++)
newone[i][j] = HL[i][j] - rhs.HL[i][j];
return Matrix(r_,l_,newone);
}
// else
// cout<<"error ——矩阵类型不符 "<<endl;
} Matrix Matrix::operator = (const Matrix & rhs)
{
if((this->row == rhs.row)&&(this->list == rhs.list))
{
for(i = ;i < row;i++)
for(j = ;j < list;j++)
this->HL[i][j] = rhs.HL[i][j];
return (*this);
}
// else
// cout<<"error ——矩阵类型不符 "<<endl;
} ostream & operator << (ostream & os,const Matrix & rhs)
{
os<<"Matrix : row ="<<rhs.row<<" , list = "<<rhs.list<<endl;
for(i = ;i < rhs.row;i++)
{
for(j = ;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 ;
}

2019-11-02    15:34:51

最新文章

  1. 【leetcode】Unique Binary Search Trees (#96)
  2. Pair Project:电梯控制程序
  3. SqlServer2012 数据库的同步之发布+订阅
  4. Day1(2016/1/21)——Beginning
  5. 10.29 afternoon
  6. mac 终端下ssh 登录远程服务器不发输入中文
  7. Winform 无边框窗口移动自定义边框粗细颜色
  8. 【VB超简单入门】六、基本数据类型
  9. Life in Changsha 第二次scrum冲刺
  10. SpringMVC与Struts2的主要区别
  11. 破解idea地址
  12. 《HelloGitHub月刊》第 09 期
  13. apk公钥私钥用法
  14. 原生JavaScript运动功能系列(一):运动功能剖析与匀速运动实现
  15. VirtualBox fedora29 安装
  16. JAVA web端JS下载excel文件
  17. 如何唯一的标识一台Android设备?
  18. Web3.js API 中文文档
  19. 移植vsftpd到arm linux
  20. JAVA设计模式详解(六)----------状态模式

热门文章

  1. 3-8 pivot操作
  2. kolla部署openstack allinone,报错APIError: 500 Server Error: Internal Server Error (\&quot;oci runtime error: container_linux.go:235: starting container process caused \&quot;container init exited prematurely
  3. Jenkins连接Git仓库时候报错Permission denied, please try again.
  4. UE4 C++中出现的让人手足无措的问题(持续更新)
  5. 多线程(五)多线程同步_Event事件
  6. 接口测试Post和Get区别(面试题)
  7. loadrunner 集合点设置2
  8. CloudCompare打开pcd文件
  9. Python面向对象 | 鸭子方法
  10. 手机爬虫--appium