简单的矩阵类
来源:互联网 发布:visio画数据库关系图 编辑:程序博客网 时间:2024/05/21 06:25
第一次学习C++,之前学习过一点C语言,但是都忘记的差不多了,写的第一个C加加程序,一个简单的矩阵类,实现了矩阵相加、矩阵相减、矩阵相乘、矩阵数乘、矩阵求秩、矩阵求转置以及输出运算符的重载。这些简单的功能,并添加了一个可以循环的菜单,分享给大家。平台是vs2010还没有在2015版本上调试过。第一次写,想见证一下我学习C++的心路历程~
#ifndef _MATRIX_H_ //防止头文件被重复使用#define _MATRIX_H_#includeusing 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;i SetElem(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;i GetElem(x1,i);for(int i=0;i SetElem(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;i GetElem(i,y1);for(int i=0;i SetElem(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;tpRow m.imaxy) n=m.imaxy; for (int k=0;k d) { 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<<"请输入一个"<
点击打开链接 点击打开链接
阅读全文
1 0
- 简单的矩阵类
- 编写矩阵类的简单总结
- 一个简单的矩阵类编写实例
- 一个矩阵的简单运算工具类
- 矩阵的简单算法
- C++简单矩阵类
- 矩阵,简单的运行技巧
- 简单的稀疏矩阵实现
- 简单的矩阵元素平移
- 简单的实现螺旋矩阵
- 矩阵的简单测试模板
- HDU1757矩阵的简单运用
- Python 实现简单的矩阵
- 矩阵的一些简单操作
- Matlab矩阵的简单操作
- 蓝桥杯最简单的矩阵键盘
- C#矩阵的简单操作
- 稀疏矩阵的简单描述
- vs2013+Qt 5.8+qt-vs-addin-1.2.5
- 【NOIP 2017】赛季正式打响!
- 【第1086期】使用VH和VW实现真正的流体排版
- Vue 2.5 发布了
- 从上下文,到作用域(彩蛋:理解闭包)
- 简单的矩阵类
- 2017天猫淘宝火炬超级红包入口
- Gradle编译Spring源码,并导入Eclipse
- Error inflating class android.support.design.widget.FloatingActionButton
- Java面试题集锦
- 数据挖掘之Apriori算法c++实现
- 【Leetcode】560. Subarray Sum Equals K
- UVa10534
- [有何不同] 2017年互联网公司应届生薪资盘点