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 最全方法详解与进阶(完整篇) ...
随机推荐
- 远程唤醒、WOL、Magic_Packet【转】
转自:https://www.cnblogs.com/zhuimengle/p/5898830.html 原文:http://blog.csdn.net/flyoxs/article/details/ ...
- linux的initcall机制
linux的initcall机制(针对编译进内核的驱动) initcall机制的由来 我们都知道,linux对驱动程序提供静态编译进内核和动态加载两种方式,当我们试图将一个驱动程序编译进内核时,开发者 ...
- NCcat学习使用
一.使用手册 1.简介:nc/NetCat是一款端口监听工具,可以用来建立系统之间的连接.传输文件.TCP代理等. 2.命令参数 nc [-options] hostname port[s] ...
- 第13节-BLE协议L2CAP层
学习资料:官方手册 Vol 3: Core System Package [Host volume] Part A: Logical Link Control and Adaptation Proto ...
- 使用Windows api 获得系统时间并生成文件夹
// 使用window api 获得系统时间 // 生成 #include "stdafx.h" #include <Windows.h> #include <d ...
- day45 作业
一.将当前日期按"2017-12-27 11:11 星期三"格式输出 function getdate(){ var d = new Date(); year = d.getFul ...
- VNC远程登陆树莓派3(包括开机启动)
在树莓派上安装VNC需要使用命令行.如果需要远程操作安装VNC,就必须通过SSH登录到命令行界面(Raspbian的默认用户名是:pi,默认密码是:raspberry). 安装 命令行输入: sudo ...
- vmware centos 桥接模式 联网记录
参考这篇文章 https://www.cnblogs.com/jasmine-Jobs/p/5928218.html 记得要修改/etc/sysconfig/network文件的网关配置,因为ip变动 ...
- Apex 中插入更新数据的事件执行顺序
在使用 Apex 代码插入或更新数据的时候,若干事件会被按顺序执行.了解这些顺序可以提高调试程序的效率,也可以避免不必要的错误. 可以参考官方文档. 事件的执行顺序 从数据库中读取要更新的数据记录或初 ...
- 清北学堂(2019 5 2) part 5
今天讲图论,顺便搞一搞之前没弄完的前向星dij 1.图的基本概念(课件原话): G (图)= (V(点); E(边)) 一般来说,图的存储难度主要在记录边的信息 无向图的存储中,只需要将一条无向边拆成 ...