简单的矩阵类

来源:互联网 发布:visio画数据库关系图 编辑:程序博客网 时间:2024/05/21 06:25

第一次学习C++,之前学习过一点C语言,但是都忘记的差不多了,写的第一个C加加程序,一个简单的矩阵类,实现了矩阵相加、矩阵相减、矩阵相乘、矩阵数乘、矩阵求秩、矩阵求转置以及输出运算符的重载。这些简单的功能,并添加了一个可以循环的菜单,分享给大家。平台是vs2010还没有在2015版本上调试过。第一次写,想见证一下我学习C++的心路历程~


#ifndef _MATRIX_H_                                        //防止头文件被重复使用#define _MATRIX_H_#include using namespace std;//**********************矩阵类***************************************************class Matrix  {//************ 矩阵私有成员说明与定义 ***************************************  private:double **pmatrix;                                     //存储矩阵元素  int imaxx,imaxy;                                      //矩阵大小  static double Eps;//************ 矩阵公有成员说明与定义 ***************************************public:  Matrix(unsigned int x,unsigned int y);                  //构造已知行列数的矩阵Matrix(Matrix &m);                                      //用已知矩阵构造新矩阵~Matrix();                                              //析构函数//基本函数便于后面的计算int getrow();                                           //返回矩阵行数int getcol();                                           //返回矩阵列数int exrow(int,int);                                     //交换矩阵两行int excol(int,int);                                //交换矩阵两列double GetElem(int h, int l);                           //返回矩阵元素void SetElem(int h,int l,double m);                     //设置矩阵元素bool SetValue(int x,int y,double value);                //赋值函数void SetI();                                            //设置矩阵为单位阵 void input();                                           //输入函数Matrix copy(Matrix &m);                                 //复制矩阵    //重载    friend Matrix operator + (Matrix &m,Matrix &m2);        //矩阵相加    friend Matrix operator - (Matrix &m,Matrix &m2);        //矩阵相减 friend Matrix operator * (Matrix &m,Matrix &m2);        //矩阵相乘friend Matrix operator * (double x,Matrix &m);          //矩阵数乘friend Matrix operator / (Matrix &m,double x);          //矩阵数乘Matrix operator = (Matrix &m);                          //等号的重载friend ostream & operator <<(ostream &output,Matrix &m);//单目运算符<<的重载//运算 Matrix Rotate(Matrix &m);                               //矩阵的转置friend Matrix inverse(Matrix &D);                       //计算矩阵的逆阵(矩阵变换)double det(Matrix &m);                                  //求行列式的值int Rank(Matrix &m);                                    //矩阵的秩};#endif#ifndef _OTHER_H_#define _OTHER_H_int menu();void dotaskjia();     //加void dotaskjian();    //减void dotaskshucheng();//数乘void dotaskshuchu();  //数除void dotaskcheng();   //乘bool dotaskqiuzhi();  //求值void dotaskrank();    //求秩void dotaskzhuanzhi();//转置bool dotaskqiuni();   //求逆int dotaskxilie();    //加、减、乘、求值、求秩#endif/* ******************************************************* 定义常量 ********************************************************/double Matrix::Eps=1.0e-8;/* ******************************************************* 构造函数 ********************************************************/Matrix::Matrix(unsigned int x,unsigned int y){  pmatrix=new double *[x];                               //为2级指针申请空间,每个元素是一个一级指针  if(!pmatrix)  {  cerr<<"Matrix constructing error"<imaxx;}/* ******************************************************* 取矩阵列函数 *******************************************************/int Matrix::getcol(){return this->imaxy;}/* ******************************************************* 取矩阵元素函数******************************************************/double Matrix::GetElem(int h, int l)                          {if(h>imaxx-1 || h<0 ){cout<<"第 "<imaxy-1 || l<0 ){cout<<"第 "<pmatrix[h][l];  }/* *************************************************设置矩阵元素函数 ********************************************************/void Matrix::SetElem(int h,int l,double m){if(h>imaxx-1 || h<0 ){cout<<"Matrix::SetElem() eror: element row setting beyond range...  "<imaxy-1 || l<0 ){cout<<"Matrix::SetElem() eror: element rank setting beyond range...  "<pmatrix[h][l]=m;}/* **************************************************设置矩阵为单位阵********************************************************/void Matrix::SetI()         {for(int i=0;iSetElem(i,j,1.0);else this->SetElem(i,j,0.0);}/* *************************************************交换矩阵两行元素********************************************************/int Matrix::exrow(int x1,int x2){ if(x1<0 || x1>this->getrow()){cout<<"Matrix::exrow() error::row1 inputed beyond range"<this->getrow()){cout<<"Matrix::exrow() error::row2 inputed beyond range"<imaxy;double* ex;ex=new double [ran];for (int i=0;iGetElem(x1,i);for(int i=0;iSetElem(x1,i,this->GetElem(x2,i)); this->SetElem(x2,i,*(ex+i));}delete [] ex;return 1;}/* *************************************************交换矩阵两列元素 ********************************************************/int Matrix::excol(int y1,int y2)       {if(y1<0 || y1>this->getcol()){cout<<"Matrix::excol() error::col1 inputed beyond range"<this->getcol()){cout<<"Matrix::excol() error::col2 inputed beyond range"<getrow();double* ex;ex=new double [ran]; for (int i=0;iGetElem(i,y1);for(int i=0;iSetElem(i,y1,this->GetElem(i,y2));      this->SetElem(i,y2,*(ex+i));}delete [] ex;return 1;}/* ****************************************************矩阵赋值***********************************************************/bool Matrix::SetValue(int x,int y,double value)  {  if((x>=imaxx)||(y>=imaxy))  {  cerr<<"Invalid(x,y): "<>value;  SetValue(ix,iy,value);  }  }  /* ******************************************************* 拷贝函数 ********************************************************/Matrix Matrix::copy(Matrix &m){Matrix m2(m.imaxx,m.imaxy);for(int i=0;i=0;tpCon--,tpRow++){total*=m.pmatrix[tpRow][tpCon];num++;}tpRow=num;for(int tpCon=m.imaxy-1;tpRowm.imaxy) n=m.imaxy; for (int k=0;kd) { d=vv; L=i; H=j; } } if(d#include "other.h"#include "Windows.h" int main()  { bool exit=false;while (1){int choice=menu();switch(choice){case(1):{dotaskjia();break;}case (2):{dotaskjian();break;}case (3):{dotaskshucheng();break;}case (4):{dotaskshuchu();break;}case (5):{dotaskcheng();break;}case (6):{dotaskqiuzhi();break;}case (7):{dotaskrank();break;}case (8):{dotaskzhuanzhi();break;}case (9):{dotaskqiuni();break;}case (10):{dotaskxilie();break;}default:cout<<"请再次选择"<#include "windows.h"/* ******************************************************* 显示菜单 ********************************************************/int menu(){ system("cls");system("Color F0");int choice;cout<<"     *************************请选择您想进行的计算*************************                        "<>choice;return choice;}/* ******************************************************* 矩阵相加 ********************************************************/void dotaskjia(){system("Color F1");int x,y;cout<<"请输入想要相加的矩阵的行号与列号:"<>x;cin>>y;Matrix a(x,y);cout<<"请输入第一个"<>x;cin>>y;Matrix a(x,y);cout<<"请输入第一个"<>x; cin>>y; Matrix a(x,y); cout<<"请输入一个"<>z; Matrix b(x,y); b=z*a; cout<<"矩阵数乘的结果为:"<>x; cin>>y; Matrix a(x,y); cout<<"请输入一个"<>z; Matrix b(x,y); b=a/z; cout<<"矩阵数除的结果为:"<>x; cin>>y; cout<<"请输入想要相乘的第二个矩阵的行号与列号:"<>xx; cin>>yy; Matrix a(x,y); cout<<"请输入第一个"<>x; cin>>y; if(x!=y) { cerr<<"无法求得该矩阵的值"<>x; cin>>y; Matrix a(x,y); int z;cout<<"请输入一个"<>x; cin>>y; Matrix a(x,y); cout<<"请输入一个"<

点击打开链接 点击打开链接


原创粉丝点击