数组与矩阵(1)_矩阵相乘

矩阵相乘是矩阵运算中十分基本的概念。

在这里我用类的形式封装了矩阵的存储和表示。内部存储的方式是采用一维数组的形式。

另外引入了异常处理的机制。也算是对C++的基本概念进行了实际应用了。 

// error class
class  BadOccur
{
public:    
    BadOccur(
int nErrorNum)
    
{
        ErrorNum 
= nErrorNum;
    }

    
void BadOccurPrint()
    
{
        cout 
<< ErrorNum << " error occurs!" << endl;
    }

private:
    
int ErrorNum;
}
;

// matrix class
class  CMatrix
{
public:
    CMatrix(
int Row = 0int Col = 0)
    
{
        
if ( Row < 0 || Col < 0)
        
{
            
throw BadOccur(2);
        }

        
else
        
{
            nRows 
= Row;
            nCols 
= Col;
            pValueArray 
= new int [Row * Col];
        }

    }

    
int operator()(int Row, int Col) const //get the element from  the position you have given
    
{
        
if ( Row < 1 || Row > nRows || Col < 1 || Col > nCols)
        
{
            
throw BadOccur(1);//cause error 1
        }

        
return pValueArray[(Row - 1* nCols + Col - 1];
    }

    
int& SetData(int i, int j)//abstract the position for you to set data
    
{
        
return pValueArray[(i - 1)* nCols + j - 1]; 
    }

    
int nRows;
    
int nCols;
    
int* pValueArray;
}
;

/*
*    Function Name        :MultiplyMatrix
*    function            :multiply two matrices
*    detail                :none
*    author                :weixiong
*    time                :2007-2-1
*    return type            :bool
*   return description  : true                                      two matrices has multiplied
*                         false                                     two matrices can't be multiplied
*    function parameters    :CMatrix& MatrixA                          multiply matrix A and B
*                         CMatrix& MatrixB                 
*                          
*/

bool  MultiplyMatrix(CMatrix &  MatrixA, CMatrix &  MatrixB, CMatrix &  ResultMatrix)
{
    
if (MatrixA.nCols != MatrixB.nRows)
    
{
        
return false;
    }

    
else
    
{
        
int Sum = 0;
        
for (int Row = 1; Row <= MatrixA.nRows; Row++)
        
{
            
for (int Col = 1; Col <= MatrixB.nCols; Col++)
            
{
                
for (int i = 1; i <= MatrixA.nCols; i++)
                
{
                    Sum 
= Sum + MatrixA(Row, i) * MatrixB(i, Col);
                }

                ResultMatrix.pValueArray[(Row 
- 1* MatrixB.nCols + Col - 1= Sum;
            }

        }

        
return true;
    }

}

int  main()
{
    
try
    
{    
        
//CMatrix ErrorMatrix(-1, 0);//example of cause error 2
        int Row1 = 0;
        
int Col1 = 0;
        cout 
<< "Please enter the row and col for the first matrix." << endl;
        cin 
>> Row1 >> Col1;
        
int Num = 1;
        CMatrix MatrixA(Row1, Col1);
        
for (int i = 1; i <= Row1; i++)
        
{
            
for (int j = 1; j <= Col1; j++)
            
{
                MatrixA.SetData(i, j) 
= Num++;
            }

        }

        
//int a = MatrixA(0, 2);//example of cause error 1
        int Row2 = 0;
        
int Col2 = 0;
        cout 
<< "Please enter the row and col for the second matrix." << endl;
        cin 
>> Row2 >> Col2;
        Num 
= 1;
        CMatrix MatrixB(Row2, Col2);
        
for (int i = 1; i <= Row2; i++)
        
{
            
for (int j = 1; j <= Col2; j++)
            
{
                MatrixB.SetData(i, j) 
= Num++;
            }

        }

        CMatrix ResultMatrix(Row1, Col2);
        MultiplyMatrix(MatrixA, MatrixB, ResultMatrix);
        getchar();
    }

    
catch(BadOccur m)
    
{
        m.BadOccurPrint();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值