C 简单矩阵问题求解!!!!

[复制链接]
查看11 | 回复1 | 2011-6-27 02:10:36 | 显示全部楼层 |阅读模式
题目如图,谢谢!!
回复

使用道具 举报

千问 | 2011-6-27 02:10:36 | 显示全部楼层
#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;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行