C++矩阵模板类

/*
 ============================================================================
 Name        : 矩阵实现
 Author      : Swair Fang
 Version     : 1.1
 Copyright   : by Swair Fang
 Description : 堆内矩阵实现, Ansi-style
 功能:
     1.取反
	 2.赋值
	 3.加法
	 4。减法
	 5.点乘
	 6.点除
	 7.矩阵相乘
	 8.打印
	 9.随机数
 ============================================================================
 */
#ifndef _Matrix_H
#define _Matrix_H


#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;


template
       
       
         class CMatrix { typedef struct Mnode { T Elt; struct Mnode *Right; struct Mnode *Down; } *Position; private: Position **Px; int m_M; int m_N; public: CMatrix(); CMatrix(int m,int n); ~CMatrix(); void Empty(); CMatrix(const CMatrix 
        
          &SrcMatrix); void operator~(); CMatrix 
         
           & operator=(const CMatrix 
          
            &SrcMatrix); CMatrix 
           
             & operator+(const CMatrix 
            
              &SrcMatrix); CMatrix 
             
               & operator-(const CMatrix 
              
                &SrcMatrix); CMatrix 
               
                 & operator*(const CMatrix 
                
                  &SrcMatrix); CMatrix 
                 
                   & operator/(const CMatrix 
                  
                    &SrcMatrix); void MxMul(CMatrix 
                   
                     &SrcMatrix); void Print(); double MRand(); }; template 
                    
                      double CMatrix 
                     
                       ::MRand() { return (double)rand()/1000; } template 
                      
                        CMatrix 
                       
                         ::CMatrix(int m,int n) { m_M=m; m_N=n; Px=new Position*[m]; //开了一个"指针矩阵"空间 int t=0; for(t=0;t 
                        
                          Right=NULL; P->Down=NULL; Position Pbf=P; Position Tmp=NULL; int i=0; //矩阵第一行 int j; for(j=0;j 
                         
                           Elt=MRand(); Tmp->Right=NULL; Tmp->Down=NULL; Px[i][j]=Tmp; P->Right=Tmp; P=P->Right; } for(i=1;i 
                          
                            Elt=MRand(); Tmp->Right=NULL; Tmp->Down=NULL; Px[i][j]=Tmp; Px[i-1][j]->Down=Tmp; P->Right=Tmp; P=P->Right; } } delete Pbf; } template 
                           
                             CMatrix 
                            
                              ::~CMatrix() { Position P=Px[0][0]; Position PbfDown; Position Tmp; while(P!=NULL) { PbfDown=P->Down; while(P!=NULL) { Tmp=P; P=P->Right; delete Tmp; } P=PbfDown; } for(int t=0;t 
                             
                               void CMatrix 
                              
                                ::Empty() { Position P=Px[0][0]; Position PbfDown; Position Tmp; while(P!=NULL) { PbfDown=P->Down; while(P!=NULL) { Tmp=P; P=P->Right; delete Tmp; } P=PbfDown; } for(int t=0;t 
                               
                                 CMatrix 
                                
                                  ::CMatrix(const CMatrix 
                                 
                                   &SrcMatrix) { Position pMnode=Px[0][0]; Position pScrMnode=SrcMatrix.Px[0][0]; Position pMnodeTmp,pScrMnodeTmp; while(pMnode!=NULL) { pMnodeTmp=pMnode; pScrMnodeTmp=pScrMnode; while(pMnode!=NULL) { pMnode->Elt=pScrMnode->Elt; pMnode=pMnode->Right; pScrMnode=pScrMnode->Right; } pMnode=pMnodeTmp->Down; pScrMnode=pScrMnodeTmp->Down; } } template 
                                  
                                    CMatrix 
                                   
                                     & CMatrix 
                                    
                                      ::operator=(const CMatrix 
                                     
                                       &SrcMatrix) { if(this==&SrcMatrix) return *this; if(&SrcMatrix!=NULL) Empty(); m_M=SrcMatrix.m_M; m_N=SrcMatrix.m_N; CMatrix 
                                      
                                        *DstMatrix=new CMatrix(m_M,m_N); Position pMnode=DstMatrix->Px[0][0]; Position pScrMnode=SrcMatrix.Px[0][0]; Position pMnodeTmp,pScrMnodeTmp; while(pMnode!=NULL) { pMnodeTmp=pMnode; pScrMnodeTmp=pScrMnode; while(pMnode!=NULL) { pMnode->Elt=pScrMnode->Elt; pMnode=pMnode->Right; pScrMnode=pScrMnode->Right; } pMnode=pMnodeTmp->Down; pScrMnode=pScrMnodeTmp->Down; } Px=DstMatrix->Px; return *this; } template 
                                       
                                         CMatrix 
                                        
                                          & CMatrix 
                                         
                                           ::operator+(const CMatrix 
                                          
                                            &SrcMatrix) { if(m_M!=SrcMatrix.m_M||m_N!=SrcMatrix.m_N) { printf("Matrix is not accordencd\n"); exit(1); } else { CMatrix 
                                           
                                             *DstMatrix=new CMatrix(m_M,m_N); Position pDstMnode=DstMatrix->Px[0][0]; Position pMnode=Px[0][0]; Position pScrMnode=SrcMatrix.Px[0][0]; Position pDstMnodeTmp,pMnodeTmp,pScrMnodeTmp; while(pMnode!=NULL) { pDstMnodeTmp=pDstMnode; pMnodeTmp=pMnode; pScrMnodeTmp=pScrMnode; while(pMnode!=NULL) { pDstMnode->Elt=pMnode->Elt+pScrMnode->Elt; pDstMnode=pDstMnode->Right; pMnode=pMnode->Right; pScrMnode=pScrMnode->Right; } pDstMnode=pDstMnodeTmp->Down; pMnode=pMnodeTmp->Down; pScrMnode=pScrMnodeTmp->Down; } return *DstMatrix; } } template 
                                            
                                              void CMatrix 
                                             
                                               ::operator~() { Position pMnode=Px[0][0]; Position pMnodeTmp; while(pMnode!=NULL) { pMnodeTmp=pMnode; while(pMnode!=NULL) { pMnode->Elt=~pMnode->Elt; pMnode=pMnode->Right; } pMnode=pMnodeTmp->Down; } } template 
                                              
                                                CMatrix 
                                               
                                                 & CMatrix 
                                                
                                                  ::operator-(const CMatrix 
                                                 
                                                   &SrcMatrix) { if(m_M!=SrcMatrix.m_M||m_N!=SrcMatrix.m_N) { printf("Matrix is not accordencd\n"); exit(1); } else { CMatrix 
                                                  
                                                    *DstMatrix=new CMatrix(m_M,m_N); Position pDstMnode=DstMatrix->Px[0][0]; Position pMnode=Px[0][0]; Position pScrMnode=SrcMatrix.Px[0][0]; Position pDstMnodeTmp,pMnodeTmp,pScrMnodeTmp; while(pMnode!=NULL) { pDstMnodeTmp=pDstMnode; pMnodeTmp=pMnode; pScrMnodeTmp=pScrMnode; while(pMnode!=NULL) { pDstMnode->Elt=pMnode->Elt-pScrMnode->Elt; pDstMnode=pDstMnode->Right; pMnode=pMnode->Right; pScrMnode=pScrMnode->Right; } pDstMnode=pDstMnodeTmp->Down; pMnode=pMnodeTmp->Down; pScrMnode=pScrMnodeTmp->Down; } return *DstMatrix; } } template 
                                                   
                                                     CMatrix 
                                                    
                                                      & CMatrix 
                                                     
                                                       ::operator*(const CMatrix 
                                                      
                                                        &SrcMatrix) { if(m_M!=SrcMatrix.m_M||m_N!=SrcMatrix.m_N) { printf("Matrix is not accordencd\n"); exit(1); } else { CMatrix 
                                                       
                                                         *DstMatrix=new CMatrix(m_M,m_N); Position pDstMnode=DstMatrix->Px[0][0]; Position pMnode=Px[0][0]; Position pScrMnode=SrcMatrix.Px[0][0]; Position pDstMnodeTmp,pMnodeTmp,pScrMnodeTmp; while(pMnode!=NULL) { pDstMnodeTmp=pDstMnode; pMnodeTmp=pMnode; pScrMnodeTmp=pScrMnode; while(pMnode!=NULL) { pDstMnode->Elt=pMnode->Elt*pScrMnode->Elt; pDstMnode=pDstMnode->Right; pMnode=pMnode->Right; pScrMnode=pScrMnode->Right; } pDstMnode=pDstMnodeTmp->Down; pMnode=pMnodeTmp->Down; pScrMnode=pScrMnodeTmp->Down; } return *DstMatrix; } } template 
                                                        
                                                          CMatrix 
                                                         
                                                           & CMatrix 
                                                          
                                                            ::operator/(const CMatrix 
                                                           
                                                             &SrcMatrix) { if(m_M!=SrcMatrix.m_M||m_N!=SrcMatrix.m_N) { printf("Matrix is not accordencd\n"); exit(1); } else { CMatrix 
                                                            
                                                              *DstMatrix=new CMatrix(m_M,m_N); Position pDstMnode=DstMatrix->Px[0][0]; Position pMnode=Px[0][0]; Position pScrMnode=SrcMatrix.Px[0][0]; Position pDstMnodeTmp,pMnodeTmp,pScrMnodeTmp; while(pMnode!=NULL) { pDstMnodeTmp=pDstMnode; pMnodeTmp=pMnode; pScrMnodeTmp=pScrMnode; while(pMnode!=NULL) { pDstMnode->Elt=pMnode->Elt/pScrMnode->Elt; pDstMnode=pDstMnode->Right; pMnode=pMnode->Right; pScrMnode=pScrMnode->Right; } pDstMnode=pDstMnodeTmp->Down; pMnode=pMnodeTmp->Down; pScrMnode=pScrMnodeTmp->Down; } return *DstMatrix; } } template 
                                                             
                                                               void CMatrix 
                                                              
                                                                ::MxMul(CMatrix 
                                                               
                                                                 &SrcMatrix) { if(m_N!=SrcMatrix.m) { printf("Matrix can not be Multiplied\n"); exit(1); } else { int i,j,k; for(i=0;i 
                                                                
                                                                  Elt=0; for(k=0;k 
                                                                 
                                                                   Elt+=(Px[i][k]->Elt)*(SrcMatrix.Px[k][j]->Elt); } } } } template 
                                                                  
                                                                    void CMatrix 
                                                                   
                                                                     ::Print() { Position pMnode=Px[0][0]; Position pMnodeTmp; while(pMnode!=NULL) { pMnodeTmp=pMnode; while(pMnode!=NULL) { char *cp=(char*)pMnode; // printf("0x%x: %10f ",cp,pMnode->Elt); printf("%10f ",pMnode->Elt); pMnode=pMnode->Right; } printf("\n"); pMnode=pMnodeTmp->Down; } printf("\n"); } #endif/*_Matrix_H*/ int main(void) { printf("===============初始化矩阵===============\n"); printf("M1,M2,M3\n"); CMatrix 
                                                                    
                                                                      M1(3,2); CMatrix 
                                                                     
                                                                       M2(3,2); CMatrix 
                                                                      
                                                                        M3(3,2); M1.Print(); M2.Print(); M3.Print(); cout<<"------M3=M1---------"< 
                                                                        
                                                                       
                                                                      
                                                                     
                                                                    
                                                                   
                                                                  
                                                                 
                                                                
                                                               
                                                              
                                                             
                                                            
                                                           
                                                          
                                                         
                                                        
                                                       
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值