用C++,如何导入一个A(2550,4)的矩阵啊? 求助。。。
答案:3 悬赏:50 手机版
解决时间 2021-05-08 19:37
- 提问者网友:我们很暧昧
- 2021-05-08 02:59
rt
最佳答案
- 五星知识达人网友:轮獄道
- 2021-05-08 03:53
你是从文件读取吗?
全部回答
- 1楼网友:山有枢
- 2021-05-08 05:39
#include "iostream.h"
class CMatrix
{
int m_Rows;
int m_Cols;
float *m_P;
public:
CMatrix(int i=0,int j=0);
~CMatrix();
void Init();
void Show();
CMatrix operator+(CMatrix &rc);
CMatrix operator*(CMatrix &rc);
CMatrix operator-(CMatrix &rc);
CMatrix & operator=(CMatrix &rc);
CMatrix (CMatrix &rc);
};
CMatrix::CMatrix(int i,int j)
{
int m,n;
m_P=0;
if(i<0||j<0)
return;
m_Rows=i;
m_Cols=j;
m_P=new float[m_Rows*m_Cols];
for(m=0;m<i;m++)
for(n=0;n<j;n++)
m_P[m*m_Cols+n]=0;
}
void CMatrix::Init()
{
int i,j;
for(i=0;i<m_Rows;i++)
for(j=0;j<m_Cols;j++)
{
cout<<"请输入矩阵元素["<<i<<"]["<<j<<"]:";
cin>>m_P[i*m_Cols+j];
}
}
void CMatrix::Show()
{
int i,j;
for(i=0;i<m_Rows;i++)
{
for(j=0;j<m_Cols;j++)
cout<<" "<<m_P[i*m_Cols+j];
cout<<endl;
}
}
CMatrix CMatrix::operator*(CMatrix &rc)
{
int i,j,k;
if(m_Cols!=rc.m_Rows)
return CMatrix();
CMatrix r(m_Rows,rc.m_Cols);
for(i=0;i<m_Rows;i++)
for(j=0;j<rc.m_Cols;j++)
for(k=0;k<m_Cols;k++)
r.m_P[i*rc.m_Cols+j]+=m_P[i*m_Cols+k]*rc.m_P[k*rc.m_Cols+j];
return r;
}
CMatrix CMatrix::operator+(CMatrix &rc)
{
if(m_Rows!=rc.m_Rows||m_Cols!=rc.m_Cols)
return CMatrix();
CMatrix r(m_Rows,m_Cols);
for(int i=0;i<m_Rows;i++)
for(int j=0;j<rc.m_Cols;j++)
r.m_P[i*m_Cols+j]=m_P[i*m_Cols+j]+rc.m_P[i*m_Cols+j];
return r;
}
CMatrix CMatrix::operator-(CMatrix &rc)
{
if(m_Rows!=rc.m_Rows||m_Cols!=rc.m_Cols)
return CMatrix();
CMatrix r(m_Rows,m_Cols);
for(int i=0;i<m_Rows;i++)
for(int j=0;j<rc.m_Cols;j++)
r.m_P[i*m_Cols+j]=m_P[i*m_Cols+j]-rc.m_P[i*m_Cols+j];
return r;
}
CMatrix & CMatrix::operator=(CMatrix &rc)
{
if(m_P!=0)
delete m_P;
m_Rows=rc.m_Rows;
m_Cols=rc.m_Cols;
m_P=new float[m_Rows*m_Cols];
for(int i=0;i<m_Rows*m_Cols;i++)
m_P[i]=rc.m_P[i];
return *this;
}
CMatrix::CMatrix (CMatrix &rc)
{
int i,j;
m_Rows=rc.m_Rows;
m_Cols=rc.m_Cols;
m_P=new float[m_Rows*m_Cols];
for(i=0;i<m_Rows;i++)
for(j=0;j<m_Cols;j++)
m_P[i*m_Cols+j]=rc.m_P[i*m_Cols+j];
}
CMatrix::~CMatrix ()
{
if(m_P!=0)
delete []m_P;
}
int main(int argc, char* argv[])
{
cout<<"第一个矩阵"<<endl;
CMatrix c(2,2);
c.Init();
cout<<"第二个矩阵"<<endl;
CMatrix b(2,3);
b.Init();
cout<<"第三个矩阵"<<endl;
CMatrix d(2,3);
d.Init();
cout<<"第一个矩阵是:"<<endl;
c.Show();
cout<<"第二个矩阵是:"<<endl;
b.Show();
cout<<"第三个矩阵是:"<<endl;
d.Show();
CMatrix r;
r=c*b;
cout<<"第一和第二个矩阵的相乘后的矩阵是:"<<endl;
r.Show();
CMatrix k;
k=b+d;
cout<<"第二和第三个矩阵的相加后的矩阵是:"<<endl;
k.Show();
CMatrix p;
p=b-d;
cout<<"第二和第三个矩阵的相减后的矩阵是:"<<endl;
p.Show();
return 0;
}
class CMatrix
{
int m_Rows;
int m_Cols;
float *m_P;
public:
CMatrix(int i=0,int j=0);
~CMatrix();
void Init();
void Show();
CMatrix operator+(CMatrix &rc);
CMatrix operator*(CMatrix &rc);
CMatrix operator-(CMatrix &rc);
CMatrix & operator=(CMatrix &rc);
CMatrix (CMatrix &rc);
};
CMatrix::CMatrix(int i,int j)
{
int m,n;
m_P=0;
if(i<0||j<0)
return;
m_Rows=i;
m_Cols=j;
m_P=new float[m_Rows*m_Cols];
for(m=0;m<i;m++)
for(n=0;n<j;n++)
m_P[m*m_Cols+n]=0;
}
void CMatrix::Init()
{
int i,j;
for(i=0;i<m_Rows;i++)
for(j=0;j<m_Cols;j++)
{
cout<<"请输入矩阵元素["<<i<<"]["<<j<<"]:";
cin>>m_P[i*m_Cols+j];
}
}
void CMatrix::Show()
{
int i,j;
for(i=0;i<m_Rows;i++)
{
for(j=0;j<m_Cols;j++)
cout<<" "<<m_P[i*m_Cols+j];
cout<<endl;
}
}
CMatrix CMatrix::operator*(CMatrix &rc)
{
int i,j,k;
if(m_Cols!=rc.m_Rows)
return CMatrix();
CMatrix r(m_Rows,rc.m_Cols);
for(i=0;i<m_Rows;i++)
for(j=0;j<rc.m_Cols;j++)
for(k=0;k<m_Cols;k++)
r.m_P[i*rc.m_Cols+j]+=m_P[i*m_Cols+k]*rc.m_P[k*rc.m_Cols+j];
return r;
}
CMatrix CMatrix::operator+(CMatrix &rc)
{
if(m_Rows!=rc.m_Rows||m_Cols!=rc.m_Cols)
return CMatrix();
CMatrix r(m_Rows,m_Cols);
for(int i=0;i<m_Rows;i++)
for(int j=0;j<rc.m_Cols;j++)
r.m_P[i*m_Cols+j]=m_P[i*m_Cols+j]+rc.m_P[i*m_Cols+j];
return r;
}
CMatrix CMatrix::operator-(CMatrix &rc)
{
if(m_Rows!=rc.m_Rows||m_Cols!=rc.m_Cols)
return CMatrix();
CMatrix r(m_Rows,m_Cols);
for(int i=0;i<m_Rows;i++)
for(int j=0;j<rc.m_Cols;j++)
r.m_P[i*m_Cols+j]=m_P[i*m_Cols+j]-rc.m_P[i*m_Cols+j];
return r;
}
CMatrix & CMatrix::operator=(CMatrix &rc)
{
if(m_P!=0)
delete m_P;
m_Rows=rc.m_Rows;
m_Cols=rc.m_Cols;
m_P=new float[m_Rows*m_Cols];
for(int i=0;i<m_Rows*m_Cols;i++)
m_P[i]=rc.m_P[i];
return *this;
}
CMatrix::CMatrix (CMatrix &rc)
{
int i,j;
m_Rows=rc.m_Rows;
m_Cols=rc.m_Cols;
m_P=new float[m_Rows*m_Cols];
for(i=0;i<m_Rows;i++)
for(j=0;j<m_Cols;j++)
m_P[i*m_Cols+j]=rc.m_P[i*m_Cols+j];
}
CMatrix::~CMatrix ()
{
if(m_P!=0)
delete []m_P;
}
int main(int argc, char* argv[])
{
cout<<"第一个矩阵"<<endl;
CMatrix c(2,2);
c.Init();
cout<<"第二个矩阵"<<endl;
CMatrix b(2,3);
b.Init();
cout<<"第三个矩阵"<<endl;
CMatrix d(2,3);
d.Init();
cout<<"第一个矩阵是:"<<endl;
c.Show();
cout<<"第二个矩阵是:"<<endl;
b.Show();
cout<<"第三个矩阵是:"<<endl;
d.Show();
CMatrix r;
r=c*b;
cout<<"第一和第二个矩阵的相乘后的矩阵是:"<<endl;
r.Show();
CMatrix k;
k=b+d;
cout<<"第二和第三个矩阵的相加后的矩阵是:"<<endl;
k.Show();
CMatrix p;
p=b-d;
cout<<"第二和第三个矩阵的相减后的矩阵是:"<<endl;
p.Show();
return 0;
}
- 2楼网友:枭雄戏美人
- 2021-05-08 04:04
int A[2550][4]
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯