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
#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
C++中Matrix(矩阵)的基本运算( +、-、=、<<)的更多相关文章
- 【CSS3】 理解CSS3 transform中的Matrix(矩阵)
理解CSS3 transform中的Matrix(矩阵) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu ...
- 理解CSS3 transform中的Matrix(矩阵)
一.哥,我被你吓住了 打架的时候会被块头大的吓住,学习的时候会被奇怪名字吓住(如“拉普拉斯不等式”).这与情感化设计本质一致:界面设计好会让人觉得这个软件好用! 所以,当看到上面“Matrix(矩阵) ...
- 理解CSS3 transform中的Matrix(矩阵)——张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2427 一.哥,我被你 ...
- css3 transform中的matrix矩阵
CSS3中的矩阵CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform),后者则是3D变换.2D变换矩阵为3*3, 如上面矩阵示 ...
- CSS3中的矩阵
CSS3中的矩阵 CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform),后者则是3D变换.2D变换矩阵为3*3,如下面矩阵示 ...
- 前端matrix矩阵的变化
css3 transform中的matrix矩阵 CSS3中的矩阵CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform), ...
- [转]numpy中的matrix矩阵处理
今天看文档发现numpy并不推荐使用matrix类型.主要是因为array才是numpy的标准类型,并且基本上各种函数都有队array类型的处理,而matrix只是一部分支持而已. 这个转载还是先放着 ...
- numpy中的matrix矩阵处理
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...
- Android中的Matrix(矩阵)
写在前面 看这篇笔记之前先看一下参考文章,这篇笔记没有系统的讲述矩阵和代码的东西,参考文章写的也有错误的地方,要辨证的看. 如何计算矩阵乘法 android matrix 最全方法详解与进阶(完整篇) ...
随机推荐
- linux wake on lan功能通过ethtool配置【转】
转自:https://blog.csdn.net/fanlilei/article/details/38042063 ethtool工具中的wol功能一直很迷惑.今天看了代码将其帮助中下面的参数说明下 ...
- python安装thrift-sasl提示缺少sasl.h文件
这其实是在pip安装Cyrus-SASL bindings for Python时报的错误. 由于没有whl文件,都是源码要经过编译之后才能安装. 所以,报这个错误,一般都是缺少安装软件造成的. 不管 ...
- LVS(二):四种工作模型
面试的时候必问这个四种工作模式,因为这几乎是企业里面必用的内容,所以一定要将其理解通透. 一.lvs-nat模式 二.LVS-DR模式(默认) 三.LVS-tun模式 四.LVS-fullnat模式 ...
- Mybatis-Plus Bugs
Mybatis-Plus Bugs 实体类中属性和数据库字段对应异常 Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'user ...
- Eclipse安装svn插件(五)
一.在线安装 1. 点击 Help --> Install New Software... 2. 在弹出的窗口中点击add按钮,输入Name(任意)和Location(插件的URL),点击OK ...
- 新手Java在华为的几点建议?
随着互联网时代的飞速发展,越来越多的人投身于软件开发行业,大家都称他们为程序员,或者码农. 这些程序员的水平也是参差不齐的,有些人从比较好的学校毕业,水平却一般般:也有些人从一般搬的学校毕业,但是水平 ...
- Genome Sequencing of MuseumSpecimens Reveals Rapid Changes in the Genetic Composition of Honey Bees in California
文章地址:https://academic.oup.com/gbe/article/10/2/458/4810442#supplementary-data Abstract 在自然生态系统和管理生态系 ...
- day 25
Nothing is more dangerous than discontinued labor. 没有什么比半途而废更危险.
- [LeetCode] 738. Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- 工作中常用的Linux命令介绍与实践
前言 做后端开发的同学,一般都会接触到服务器,而我们现在的系统用的比较多的服务器系统就是linux了,平时多多少少也会接触到一些linux下的shell命令.我们来介绍下linux一些常用的命令和使用 ...