矩阵类 with 二维数组下标运算 [][]

本类主要在于实现重载[] 模仿二维矩阵,即支持如下操作

CMatrix m(3,3);

m[1][1]=3;

CMatrix.h:

/********************************************

  CopyRight 2007 北京交通大学

  工程名称:  NewMatrix

  文件名:    CMatrix.h

  修改日期:  2007-4-21 11:21:28

  描述:      定义了矩阵类的+ - = []等接口

********************************************/

#include <iostream>

class CMatrix

{

public:
            // 根据"三原则" 必须显示定义构造\拷贝\析构函数

       CMatrix(int r, int c);

           CMatrix(const CMatrix &zMatrix);

           CMatrix &operator=(const CMatrix &zMatrix);

           ~CMatrix();

            // 定义加减接口

       CMatrix  operator+(const CMatrix &zMatrix);

           CMatrix  operator-(const CMatrix &zMatrix);

            // 定义[]下标访问,无赖但是有效的声明,此后必须全部用二元[]赋值




       int *operator[](int pos);

           

       

        // 定义<<的友元接口

       friend std::ostream &  operator<<(std::ostream &out, const CMatrix &zMatrix);

private:
            // 存数据区域

       int *m_elem;

            // 存矩阵行列

       int m_row, m_col;
};

CMatrix.cpp:

/********************************************
   CopyRight 2007 北京交通大学
   工程名称:   NewMatrix
   文件名:     CMatrix.cpp
   修改日期:   2007-4-21 11:20:39
   描述:       实现了矩阵类的加减和下标运算
********************************************/

#include "CMatrix.h"
#include <iostream>
#include <iomanip>

CMatrix::CMatrix(int r,int c)
:m_col(c),m_row(r)
{
m_elem = new int[m_col * m_row];

// 安全检查,防分配失败
if (!m_elem)
{
   throw ;
}

memset(m_elem,0,sizeof(int) * m_col * m_row);

}

CMatrix::CMatrix(const CMatrix &zMatrix)
{
m_col = zMatrix.m_col;
m_row = zMatrix.m_row;

m_elem= new int[m_col * m_row];

memcpy(m_elem,zMatrix.m_elem,sizeof(int)*m_col*m_row);
}

CMatrix& CMatrix::operator =(const CMatrix &zMatrix)
{
// 防内存泄露
if(m_elem)
   delete [] m_elem;

m_col = zMatrix.m_col;
m_row = zMatrix.m_row;

m_elem = new int[m_col * m_row];

memcpy(m_elem,zMatrix.m_elem,sizeof(int)*m_col*m_row);

return *this;
}

CMatrix::~CMatrix()
{
delete [] m_elem;
}

std::ostream& operator   << (std::ostream& out,const CMatrix &zMatrix)
{
for(int i=0;i!=zMatrix.m_row * zMatrix.m_col;++i)
{

   out<<std::setw(2)<<zMatrix.m_elem[i]<<" ";

   // 换行
   if(!((i+1)%zMatrix.m_col))
    out<<std::endl;
}

return out;
}

int* CMatrix::operator [] (int pos)
{
return &m_elem[m_col * pos];
}

CMatrix CMatrix::operator +(const CMatrix &zMatrix)
{
if(m_col != zMatrix.m_col || m_row != zMatrix.m_row)
{
   std::cout<<"不同型矩阵无法相加";
   throw ;
}

CMatrix rMatrix(*this);
for(int i=0;i != m_col * m_row;++i)
   rMatrix.m_elem[i] += zMatrix.m_elem[i];

return rMatrix;
}

CMatrix CMatrix::operator -(const CMatrix &zMatrix)
{
if (m_col != zMatrix.m_col || m_row != zMatrix.m_row)
{
   std::cout<<"不同型矩阵无法相加";
   throw ;
}

CMatrix rMatrix(*this);

for(int i=0;i!=m_col * m_row;++i)
   rMatrix.m_elem[i] -= zMatrix.m_elem[i];

return rMatrix;
}

main.cpp:

/********************************************

  CopyRight 2007 北京交通大学

  工程名称:  NewMatrix

  文件名:    main.cpp

  修改日期:  2007-4-21 11:21:08

  描述:      对矩阵类的各项操作进行测试

********************************************/

#include "CMatrix.h"
#include <iostream>

int main()
{
    CMatrix m1(3, 4);

    for (int i = 0; i != 3; ++i)
           for (int j = 0; j != 4; ++j)
   m1[i][j] = 7;

    m1[0][0] = 5;

    std::cout << "m1:\n"
              << m1;

    CMatrix m2(3, 4);

    for (i = 0; i != 3; ++i)
           for (int j = 0; j != 4; ++j)
    m2[i][j] = i + j;

    std::cout << "m2:\n"
              << m2;

    std::cout << "m1+m2:\n"
              << m1 + m2;

    m2 = m1;

    std::cout << "m2:\n"
              << m2;

    std::cout << "m2-m1:\n"
              << m2 - m1;
    return 0;
}

 

Leave a Reply

Your email address will not be published.