简单的矩阵类

第一次学习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 inverse(matrix &d); 计算矩阵的逆阵(矩阵变换) double det(matrix 求行列式的值 int rank(matrix 矩阵的秩 }; #endif#ifndef _other_h_ #define menu(); void dotaskjia(); 加 dotaskjian(); 减 dotaskshucheng(); 数乘 dotaskshuchu(); 数除 dotaskcheng(); 乘 bool dotaskqiuzhi(); 求值 dotaskrank(); 求秩 dotaskzhuanzhi(); 转置 dotaskqiuni(); 求逆 dotaskxilie(); 加、减、乘、求值、求秩 #endif * ******************************************************* 定义常量 ******************************************************** matrix::eps="1.0e-8;" 构造函数 matrix::matrix(unsigned x,unsigned y) { pmatrix="new" *[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<<"第 "<
     
     
      
      <<" 行"<<" Matrix::GetElem()eror: element row beyond range...  "<
      
      
       
       imaxy-1 || l<0 )
	{
		cout<<"第 "<
       
       
         <<" 列"<<" Matrix::GetElem()eror: element rank beyond range... "< 
        
          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<<" *************************请选择您想进行的计算************************* "< 
                            
                               >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<<"请输入一个"< 
                                                    
                                                      <<"行"< 
                                                     
                                                       <<"列的矩阵"< 
                                                       
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值