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

原题为设计一个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(矩阵)的基本运算( +、-、=、<<)的更多相关文章

  1. 【CSS3】 理解CSS3 transform中的Matrix(矩阵)

    理解CSS3 transform中的Matrix(矩阵) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu ...

  2. 理解CSS3 transform中的Matrix(矩阵)

    一.哥,我被你吓住了 打架的时候会被块头大的吓住,学习的时候会被奇怪名字吓住(如“拉普拉斯不等式”).这与情感化设计本质一致:界面设计好会让人觉得这个软件好用! 所以,当看到上面“Matrix(矩阵) ...

  3. 理解CSS3 transform中的Matrix(矩阵)——张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2427 一.哥,我被你 ...

  4. css3 transform中的matrix矩阵

    CSS3中的矩阵CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform),后者则是3D变换.2D变换矩阵为3*3, 如上面矩阵示 ...

  5. CSS3中的矩阵

    CSS3中的矩阵 CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform),后者则是3D变换.2D变换矩阵为3*3,如下面矩阵示 ...

  6. 前端matrix矩阵的变化

    css3 transform中的matrix矩阵   CSS3中的矩阵CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform), ...

  7. [转]numpy中的matrix矩阵处理

    今天看文档发现numpy并不推荐使用matrix类型.主要是因为array才是numpy的标准类型,并且基本上各种函数都有队array类型的处理,而matrix只是一部分支持而已. 这个转载还是先放着 ...

  8. numpy中的matrix矩阵处理

    numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...

  9. Android中的Matrix(矩阵)

    写在前面 看这篇笔记之前先看一下参考文章,这篇笔记没有系统的讲述矩阵和代码的东西,参考文章写的也有错误的地方,要辨证的看. 如何计算矩阵乘法 android matrix 最全方法详解与进阶(完整篇) ...

随机推荐

  1. linux 中断底半部机制对比(任务队列,工作队列,软中断)--由linux RS485引出的血案【转】

    转自:http://blog.chinaunix.net/uid-20768928-id-5077401.html 在LINUX RS485的使用过程中,由于各种原因,最后不得不使用中断底半部机制的方 ...

  2. 每天一套题打卡|河南省第九届ACM/ICPC

    A 表达式求值 表达式求值:可以用递归求解,也可以用栈模拟,考过多次. 类似题目:NYOJ305,NYOJ35 用栈模拟做法: #include <stdio.h> #include &l ...

  3. 莫烦TensorFlow_04 placeholder

    import tensorflow as tf input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) outp ...

  4. Spring data redis的使用

    Spring data redis的使用 一.Redis的安装和使用 Redis是用C语言开发的一个高性能键值对数据库,可用于数据缓存,主要用于处理大量数据的高访问负载. 下载地址:https://g ...

  5. 【使用篇二】SpringBoot整合Servlet(1)

    两种方式: 通过注解扫描完成 Servlet组件的注册 通过方法完成 Servlet组件的注册 一.通过注解扫描完成 Servlet 组件的注册 1. 编写Servlet类 /** * SpringB ...

  6. django之三剑客、静态文件配置、请求响应对象、数据库操作

    三剑客 from django.shortcuts import render,HttpResponse,redirect HttpResponse # 返回字符串 render(response, ...

  7. [ZJOI2019]线段树(线段树,DP)

    又是神仙题. 要写博客太长了,先咕着.放个代码先. 为什么 fmul 在 linux 底下还被定义过了--能想象到我一发 CE 的绝望吗 qaq #include<bits/stdc++.h&g ...

  8. [LeetCode] 382. Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  9. 关于交叉编译Nodejs的坑

    前言 交叉编译Nodejs到其他平台上的时候,遇到了2个坑,网上极少有人提及,花了整个晚上才解决,在此记录下. 我的编译目标环境为: 龙芯3A 编译脚本 cd 代码目录 export PREFIX=/ ...

  10. mnist数据集探究

    一.mnist的属性和方法 为了方便我只检查了后20个属性和方法 from tensorflow.examples.tutorials.mnist import input_data mnist = ...