#includestdio.h
#includeconio.h
#includestdlib.h
classCMatrix
{
private:
introw,col;
double*data;
public:
staticintnum;
public:
intGetRow() {returnrow;}
intGetCol() {returncol;}
CMatrix(inta,intb)
{
inti,j,k=0;
row=a;
col=b;
data=(double*)malloc(row*col*sizeof(double));
if(data==NULL)
{
printf(\"ERROR:MallocforCMatrix\\nPressanykeytoexit:\\n\");
getch();
return;
}
for(i=0;irow;i)
for(j=0;jcol;j)
data[k]=0;
CMatrix::num;
}
CMatrix(inta,intb,double*pdata)
{
inti,j,k=0;
row=a;
col=b;
data=(double*)malloc(row*col*sizeof(double));
if(data==NULL)
{
printf(\"ERROR:MallocforCMatrix\\nPressanykeytoexit:\\n\");
getch();
return;
}
for(i=0;irow;i)
for(j=0;jcol;j)
{
data[k]=pdata[k];
k;
}
CMatrix::num;
}
CMatrix(CMatrix
row=CM.row;
col=CM.col;
data=(double*)malloc(row*col*sizeof(double));
if(data==NULL)
{
printf(\"ERROR:MallocforCMatrix\\nPressanykeytoexit:\\n\");
getch();
return;
}
for(i=0;irow;i)
for(j=0;jcol;j)
{
data[k]=CM.data[k];
k;
}
CMatrix::num;
}
~CMatrix()
{
row=0;
col=0;
free(data);
CMatrix::num--;
}
voidSetEyeMatirx()
{
inti,j,k=0;
for(i=0;irow;i)
for(j=0;jcol;j)
if(i==j)
data[k]=1;
else
data[k]=0;
}
CMatrixtranspose()
{
inti,j,k=0;
double*p=(double*)malloc(row*col*sizeof(double));
if(p==NULL)
{
printf(\"ERROR:MallocforCMatrix\\nPressanykeytoexit:\\n\");
getch();
return*this;
}
k=0;
for(i=0;irow;i)
for(j=0;jcol;j)
p[j*rowi]=data[k];
k=0;
for(i=0;irow;i)
for(j=0;jcol;j)
{
data[k]=p[k];
k;
}
k=row;
row=col;
col=k;
free(p);
return*this;
}
voidMySetMatrix() /*设置数组的值*/
{
inti,j,k=0;
for(i=0;irow;i)
for(j=0;jcol;j,k)
data[k]=k;
}
voiddisplay() /*输出*/
{
inti,j,k=0;
for(i=0;irow;i)
for(j=0;jcol;j)
if(j==col-1)
printf(\"%2.0f\\n\",data[k]);
else
printf(\"%2.0f,\",data[k]);
}
};
intCMatrix::num=0;
intmain()
{
CMatrixa(3,4);
printf(\"num=%d\\n\",CMatrix::num);
printf(\"row=%d,col=%d\\n\",a.GetRow(),a.GetCol());
a.display();
printf(\"\\n\");
a.MySetMatrix();
a.display();
printf(\"\\n\");
a.transpose();
a.display();
printf(\"\\n\");
CMatrixb(a);
printf(\"num=%d\\n\",CMatrix::num);
printf(\"b=\\n\");
b.display();
printf(\"\\n\");
b.SetEyeMatirx();
b.display();
printf(\"\\n\");
getch();
return0;
} |