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 最全方法详解与进阶(完整篇) ...
随机推荐
- APPIUM 常用API介绍(3)
1.send_keys send_keys(self, *value): Simulates typing into the element[在元素中模拟输入(开启appium自带的输入法并配置了ap ...
- 基于DBUtils实现数据库连接池及flask项目部署
阅读目录 flask中是没有ORM的,如果在flask里面连接数据库有两种方式 数据库连接池原理 模式一: 模式二: 数据库连接池 flask中是没有ORM的,如果在flask里面连接数据库有两种方式 ...
- 201871010117-石欣钰 《面向对象程序设计(Java)》第十周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...
- django学习-视图练习
写一个真正有用的视图 每个视图必须要做的只有两件事: 返回一个包含被请求页面内容的HttpResponse对象,或抛出一个异常,比如Http404. 至于你还想干些什么,随便你. 你的视图可以从数据库 ...
- Location配置与ReWrite语法(五)
原文链接:https://www.cnblogs.com/crazylqy/p/6892010.html location表示uri方式定位,基础语法有三种: location = pattern { ...
- djangoORM 修改表结构/字段/外键操作
Django支持修改表结构 把max_length=64 改为60 再执行一遍 python manage.py makemigrations python manage.py migrate 如果是 ...
- 论文阅读笔记五十七:FCOS: Fully Convolutional One-Stage Object Detection(CVPR2019)
论文原址:https://arxiv.org/abs/1904.01355 github: tinyurl.com/FCOSv1 摘要 本文提出了一个基于全卷积的单阶段检测网络,类似于语义分割,针对每 ...
- 实现一个new操作符
new 操作符做了这些事: 1.它创建了一个全新的对象: 2.它会被执行[[Prototype]](也就是__proto__)链接: 3.它使this指向新创建的对象: 4.通过new创建的每个对象最 ...
- Excel-查找函数
1.VLOOKUP函数 该函数的语法规则如下: VLOOKUP(lookup_value,table_array,col_index_num,range_lookup) 多表关联查询---vlooku ...
- Java的十三个设计模式
OOP三大基本特性 封装 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的属性和方法只让可信的类操作,对不可信的进行信息隐藏. 继承 继承是指这样一种能力,它可以使用现有的类的所有功能,并在无 ...