求高手,编写一个程序实现一个矩阵类,通过重载+,-,*运算符来实现矩阵的加,减,乘操作。

最好用C++实现,谢谢大家啊。。稍微有点问题,编译通不过啊,请帮忙下。... 最好用C++实现,谢谢大家啊。。
稍微有点问题,编译通不过啊,请帮忙下。
展开
 我来答
花式码农
2009-01-07 · TA获得超过1.7万个赞
知道大有可为答主
回答量:4894
采纳率:0%
帮助的人:4737万
展开全部
// ---------------- ---------------------
// ----------------------------------------------------------------
// 假期无聊,实现的矩阵类,模板哦!哈哈。。。
/// ------------- ---------------
/// ---------------------------------------------------

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

template < class T >
class CMatrix
{
public: //------------------ 构造部 -------------------------
CMatrix( void );
CMatrix( unsigned h, unsigned w );
CMatrix( const CMatrix& m );
CMatrix( const vector<T>& vec );
CMatrix( const vector<T>& vec,unsigned h,unsigned w );
~CMatrix(void);
private: //------------------- 数据部 ---------------------------
vector<T> m_vec_data;
unsigned m_u_width;
unsigned m_u_height;

public: // ------------------- 重载运算符 -------------------------

/// --- 以下均要求T具有如下运算能力:+ - += 等用到的。。。。

//取值运算
T& operator() ( unsigned row, unsigned col );
T operator() ( unsigned row, unsigned col ) const;

//赋值运算
CMatrix& operator = ( const CMatrix& );
CMatrix& operator += ( const CMatrix& );
CMatrix& operator -= ( const CMatrix& );
CMatrix& operator *= ( T );
CMatrix& operator /= ( T );

//二元运算符
CMatrix operator + ( const CMatrix& ) const;
CMatrix operator - ( const CMatrix& ) const;
CMatrix operator * ( const CMatrix& ) const;
CMatrix operator * ( T ) const;
CMatrix operator / ( T ) const;

bool operator == ( const CMatrix& ) const;
bool operator != ( const CMatrix& ) const;

public: // -------------------- 操作函数部 -------------------------
void transpose();
inline bool empty();
inline long size();
inline unsigned height();
inline unsigned width();

public: // ------------------ 输入输出 ----------------------
// 注意:矩阵元素必须支持输入输出,否则程序会错误!!
/*friend ostream& operator << ( ostream& os,const CMatrix& ma );*/
template < typename T > friend ostream& operator << ( ostream& os,const CMatrix<T>& ma );
//friend istream& operator >> ( istream& is,CMatrix& ma );
template < class T > friend istream& operator >> ( istream& is,CMatrix<T>& ma );
};
/**-----------------------------------------------------------------------------
* 输出
*------------------------------------------------------------------------------
*/
template < class T >
ostream& operator << ( ostream& os,const CMatrix<T>& ma )
{
if(!ma.m_u_height || !ma.m_u_width)
{
os<<"这是一个空矩阵!\n";
return os;
}
for(unsigned i=0;i<ma.m_u_height;i++)
{
for(unsigned j=0;j<ma.m_u_width;j++)
{
os<<ma.m_vec_data[i*ma.m_u_width + j]<<"\t";
}
os<<endl<<endl;
}
return os;
}
/**-----------------------------------------------------------------------------
* 输入
*------------------------------------------------------------------------------
*/
template < class T >
istream& operator >> ( istream& is,CMatrix<T>& ma )
{
if( !ma.m_u_height && !ma.m_u_width )
{
cerr<<"空矩阵!\n";
return is;
}
cout<<"请依次输入各个共"<<ma.m_u_height*ma.m_u_width<<"个成员:\n";
for( unsigned i=0;i<ma.m_u_height;i++ )
{
for( unsigned j=0;j<ma.m_u_width;j++ )
{
cout<<"["<<i<<"]["<<j<<"]=";
is>>ma.m_vec_data[i*ma.m_u_width + j];
}
}

return is;
}
/**-----------------------------------------------------------------------------
* 普通构造
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>::CMatrix(void):m_u_width(0),m_u_height(0)
{
}
/**-----------------------------------------------------------------------------
* 构造指定大小的全0矩阵
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>::CMatrix( unsigned h,unsigned w ):m_u_width(w),m_u_height(h)
{
//unsigned size = w*h;
//while(size--)
//{
// m_vec_data.push_back(0);
//}
vector<T> temp(w*h,T());
m_vec_data = temp;
}
/**-----------------------------------------------------------------------------
* 拷贝构造函数
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>::CMatrix( const CMatrix& m ):m_vec_data(m.m_vec_data),m_u_width(m.m_u_width),m_u_height(m.m_u_height)
{
//m_vec_data = m.m_vec_data;
//m_u_width = m.m_u_width;
//m_u_height = m.m_u_height;
}

/**-----------------------------------------------------------------------------
* 以向量构造
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>::CMatrix(const vector<T>& vec):m_u_width(static_cast<unsigned>(vec.size())),m_u_height(1),m_vec_data(vec)
{
//m_u_width = static_cast<unsigned>(vec.size());
//m_u_height = 1;
//m_vec_data = vec;
}
/**-----------------------------------------------------------------------------
* 以向量+大小构造
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>::CMatrix(const vector<T>& vec, unsigned int h, unsigned int w):m_u_height(h),m_u_width(w)
{
// --- 1.0 版 -----
//unsigned input_capability = h * w;
//unsigned fact_capability = static_cast<unsigned>(vec.size());
//if( input_capability == fact_capability )
//{
// m_vec_data = vec;
//}
//else if( input_capability < fact_capability )
//{
// unsigned gap = fact_capability - input_capability;
// vector<T> temp = vec;
// while( gap-- )
// {
// temp.pop_back();
// }
// m_vec_data = vec;
//}
//else
//{
// unsigned gap = input_capability - fact_capability;
// vector<T> temp = vec;
// while( gap-- )
// {
// temp.push_back(0);
// }
// m_vec_data = vec;
//}
// --- 2.0 版 ------
int gap = h*w - static_cast<unsigned>(vec.size());
if( !gap )
{
m_vec_data = vec;
}
else if( gap < 0 )
{
m_vec_data = vec;
m_vec_data.erase( m_vec_data.end() + gap,m_vec_data.end() ); //注意:此时gap为负数,故为+
}
else
{
//注意,此时使用了类型T的默认构造函数
m_vec_data = vec;
m_vec_data.insert(m_vec_data.end(),gap,T());
}
}
/**-----------------------------------------------------------------------------
* 析构函数
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>::~CMatrix(void)
{
m_u_width = 0;
m_u_height = 0;
m_vec_data.clear();
}

/**-----------------------------------------------------------------------------
* 取值
*------------------------------------------------------------------------------
*/
template < class T >
T& CMatrix<T>::operator ()(unsigned int row, unsigned int col)
{
assert( row < m_u_height&&col < m_u_width ); //必须在范围内

return m_vec_data[row*m_u_width + col];
}

/**-----------------------------------------------------------------------------
* const取值
*------------------------------------------------------------------------------
*/
template < class T >
T CMatrix<T>::operator ()(unsigned int row, unsigned int col) const
{
assert( row < m_u_height&&col < m_u_width ); //在范围内

return m_vec_data[row*m_u_width + col];
}
/**-----------------------------------------------------------------------------
* 矩阵=
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>& CMatrix<T>::operator = ( const CMatrix& ma )
{
m_u_width = ma.m_u_width;
m_u_height = ma.m_u_height;
m_vec_data = ma.m_vec_data;

return *this;
}
/**-----------------------------------------------------------------------------
* 矩阵+=
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>& CMatrix<T>::operator += ( const CMatrix& ma )
{
assert( (m_u_height == ma.m_u_height) && (m_u_width == ma.m_u_width) ); //行、列相同

for(unsigned i=0;i<m_vec_data.size();i++)
{
m_vec_data[i] += ma.m_vec_data[i];
}

return *this;
}

/**-----------------------------------------------------------------------------
* 矩阵-=
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>& CMatrix<T>::operator -= ( const CMatrix& ma )
{
assert( (m_u_height == ma.m_u_height) && (m_u_width == ma.m_u_width) ); //行、列相同

for(unsigned i=0;i<m_vec_data.size();i++)
{
m_vec_data[i] -= ma.m_vec_data[i];
}

return *this;
}

/**-----------------------------------------------------------------------------
* 矩阵*=T
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>& CMatrix<T>::operator *= ( T n )
{
for(vector<T>::iterator it = m_vec_data.begin();it!=m_vec_data.end();it++)
{
*it *= n;
}

return *this;
}

/**-----------------------------------------------------------------------------
* 矩阵/=T
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T>& CMatrix<T>::operator /= ( T n )
{
assert(n); //非零
for(vector<T>::iterator it = m_vec_data.begin();it!=m_vec_data.end();it++)
{
*it /= n;
}

return *this;
}

/**-----------------------------------------------------------------------------
* 矩阵+
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T> CMatrix<T>::operator + ( const CMatrix& ma ) const
{
assert( (m_u_width == ma.m_u_width)&&(m_u_height == ma.m_u_height) );

vector<T> temp( m_u_width * m_u_height );
for(unsigned i=0;i<m_vec_data.size();i++)
{
temp[i] = m_vec_data[i] + ma.m_vec_data[i];
}

return CMatrix(temp,m_u_height,m_u_width);
}

/**-----------------------------------------------------------------------------
* 矩阵-
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T> CMatrix<T>::operator - ( const CMatrix& ma ) const
{
assert( (m_u_width == ma.m_u_width)&&(m_u_height == ma.m_u_height) );

vector<T> temp( m_u_width * m_u_height );
for(unsigned i=0;i<m_vec_data.size();i++)
{
temp[i] = m_vec_data[i] - ma.m_vec_data[i];
}

return CMatrix(temp,m_u_height,m_u_width);
}

/**-----------------------------------------------------------------------------
* 矩阵* --==>>>算法待优化 时间复杂度 n^3
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T> CMatrix<T>::operator * ( const CMatrix& ma ) const
{
assert( m_u_width == ma.m_u_height );

CMatrix temp( m_u_height, ma.m_u_width );

for(unsigned i=0; i<m_u_height; i++)
{
for(unsigned j=0;j<ma.m_u_width;j++)
{
for( unsigned m=0;m<ma.m_u_width;m++ )
temp(i,j) += m_vec_data[i*m_u_width + m] * ma(m,j);
}
}

return temp;
}
/**-----------------------------------------------------------------------------
* 矩阵*T
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T> CMatrix<T>::operator * ( T n ) const
{
vector<T> temp(m_vec_data);
for(vector<T>::iterator it=temp.begin();it!=temp.end();it++)
{
*it *= n;
}

return CMatrix(temp,m_u_height,m_u_width);
}

/**-----------------------------------------------------------------------------
* 矩阵/T
*------------------------------------------------------------------------------
*/
template < class T >
CMatrix<T> CMatrix<T>::operator / ( T n ) const
{
assert(n);
vector<T> temp(m_vec_data);
for(vector<T>::iterator it=temp.begin();it!=temp.end();it++)
{
*it /= n;
}

return CMatrix(temp,m_u_height,m_u_width);
}
/**-----------------------------------------------------------------------------
* 矩阵 ==
*------------------------------------------------------------------------------
*/
template < class T >
bool CMatrix<T>::operator == ( const CMatrix& ma ) const
{
if( m_u_height != ma.m_u_height || m_u_width != ma.m_u_width )
return false;
for(unsigned i=0;i<m_vec_data.size();i++)
{
if( m_vec_data[i] != ma.m_vec_data[i] )
return false;
}
return true;
}

/**-----------------------------------------------------------------------------
* 矩阵 !=
*------------------------------------------------------------------------------
*/
template < class T >
bool CMatrix<T>::operator !=(const CMatrix& ma) const
{
if(m_u_height != ma.m_u_height || m_u_width != ma.m_u_width)
return false;

for(unsigned i=0;i<m_vec_data.size();i++)
{
if( m_vec_data[i] != ma.m_vec_data[i] )
return true;
}

return false;
}

/**-----------------------------------------------------------------------------
* 矩阵转置
*------------------------------------------------------------------------------
*/
template < class T >
void CMatrix<T>::transpose()
{
vector<T> temp(m_u_width*m_u_height);
for(unsigned i=0;i < m_u_height;i++)
{
for(unsigned j=0;j<m_u_width;j++)
{
temp[ j*m_u_height + i ] = m_vec_data[ i*m_u_width + j ];
}
}
swap(m_u_width,m_u_height);
m_vec_data = temp;
}
/**-----------------------------------------------------------------------------
* 判空
*------------------------------------------------------------------------------
*/
template < class T >
inline bool CMatrix<T>::empty()
{
return m_vec_data.empty();
}
/**-----------------------------------------------------------------------------
* 大小
*------------------------------------------------------------------------------
*/
template < class T >
inline long CMatrix<T>::size()
{
return static_cast<long>(m_vec_data.size());
}
/**-----------------------------------------------------------------------------
* 高度
*------------------------------------------------------------------------------
*/
template < class T >
inline unsigned CMatrix<T>::height()
{
return m_u_height;
}
/**-----------------------------------------------------------------------------
* 宽度
*------------------------------------------------------------------------------
*/
template < class T >
inline unsigned CMatrix<T>::width()
{
return m_u_width;
}

int main()
{
CMatrix<int> mat0(6,8);
cout<<mat0<<endl;
for(int i=0;i<6;i++)
{
for(int j=0;j<8;j++)
mat0(i,j) = i+j;
}
cout<<mat0<<endl;

vector<int> vec( 23,2 );
CMatrix<int> mat1(vec,5,5);
cout<<mat1<<endl;

vector<int> vec1( 40,5);
CMatrix<int> mat2(vec1,6,6);
cout<<mat2<<endl;

CMatrix<int> mat3(6,6);
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
mat3(i,j) = i*j;
}
cout<<mat3<<endl;

cout<<mat3 + mat2<<endl;
mat3 += mat2;
mat3 = mat3 + mat2;
cout<<mat3<<endl;

return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式