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 最全方法详解与进阶(完整篇) ...
随机推荐
- 第17节-BLE安全管理概述
安全管理是BLE中最复杂的内容,涉及LL层.SM层.GAP层 一.妈妈的担心 1. 白名单: 妈妈说,你只能跟A.B.C这3个好孩子玩:他们打电话给你,你才可以出去玩. A.B.C三人,就在妈妈的“白 ...
- Spring(005)-多环境Profile
多个环境下的配置应该怎么进行,比如数据库连接字符,多个环境不同,spring的方案,大概总结如下. 例子,数据库配置. 定义一个获取数据库链接的接口 public interface DataConn ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第七周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nwn ...
- vs解决方案文件出错
问题描述: 电脑死机,重启电脑后打开解决方案,提示“选择的文件不是有效的解决方案文件” 解决方案: 1. 先用记事本打开这个解决方案查看下,发现其中内容变成空白了? 2. 打开项目中的xxxx.vcx ...
- 一种css效果:标题带色块,React+Less
.title { display: inline-block; font-size: 15px; font-weight: bold; position: relative; padding: 5px ...
- PATA1075 PAT Judge (25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- 数据库的查——select的基本使用
--创建学生表 create table students ( id int unsigned not null auto_increment primary key, name varchar(20 ...
- [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 ...
- DVWA XSS (DOM) 通关教程
DOM,全称Document Object Model,是一个平台和语言都中立的接口,可以使程序和脚本能够动态访问和更新文档的内容.结构以及样式. DOM型XSS其实是一种特殊类型的反射型XSS,它是 ...
- .NET Core:Token认证
现在是WebAPI的时代,你所需要面对的不止是浏览器了,通常会使用Web, WebApp, NativeApp等多种呈现方式.其中诸如Ember,Angular,Backbone之类的前端框架类库正随 ...