作业帮 > 综合 > 作业

怎样编写一个矩阵转置的函数,矩阵的行、列数在程序中由用户输入!【紧急需要,】

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/29 01:06:56
怎样编写一个矩阵转置的函数,矩阵的行、列数在程序中由用户输入!【紧急需要,】
用C++语言编写:
#include <iostream.h>
class Matrix
{
private:
\x05int row,colomn;
public:
\x05double **matrix;
                static Matrix transpos(const Matrix&);
\x05Matrix(int Mrow,int Mcol)
\x05{
\x05\x05row=Mrow;
\x05\x05colomn=Mcol;
\x05\x05matrix=new double* [row];
\x05\x05for(int i=0;i<row;i++)
\x05\x05{
\x05\x05\x05matrix[i]=new double[colomn];
\x05\x05}
\x05\x05/*.
            动态分配数组,并初始化为0;
         .*/                                            
\x05\x05for(i=0;i<row;i++)
\x05\x05{
\x05\x05\x05for(int j=0;j<colomn;j++)
\x05\x05\x05{
\x05\x05\x05\x05matrix[i][j]=0;
\x05\x05\x05}
\x05\x05}
\x05}
\x05Matrix(const Matrix& a)
\x05{
\x05\x05row=a.row;
\x05\x05colomn=a.colomn;
\x05\x05matrix=new double* [row];
\x05\x05for(int i=0;i<row;i++)
\x05\x05{
\x05\x05\x05matrix[i]=new double[colomn];
\x05\x05}
\x05\x05//
\x05\x05for(i=0;i<row;i++)
\x05\x05{
\x05\x05\x05for(int j=0;j<colomn;j++)
\x05\x05\x05{
\x05\x05\x05\x05matrix[i][j]=a.matrix[i][j];
\x05\x05\x05}
\x05\x05}
\x05}
\x05~Matrix()
\x05{
\x05\x05for(int i=0;i<row;i++)
\x05\x05{
\x05\x05\x05delete[] matrix[i];
\x05\x05}
\x05\x05delete[] matrix;
\x05}
Matrix Matrix::transpos(const Matrix& m1)                 //在类的外部定义static的函数就不需要指定static
{
\x05Matrix temp(m1);
\x05for(int i=0;i<m1.colomn;i++)
\x05{
\x05\x05for(int j=0;j<m1.row;j++)
\x05\x05{
\x05\x05\x05temp.matrix[i][j]=m1.matrix[j][i];
\x05\x05}
\x05}
\x05return temp;
}
void main()
{\x05cout<<"请输入矩阵的行列数:"<<endl;
\x05int inputrow,inputcol;
\x05cin>>inputrow>>inputcol;
\x05Matrix a(inputrow,inputcol);
\x05int i,j;
\x05cout<<"请输入a矩阵的值"<<endl;
\x05for(i=0;i<inputrow;i++)
\x05\x05for(j=0;j<inputcol;j++)
\x05\x05\x05cin>>a.matrix[i][j];
\x05Matrix c(inputrow,inputcol);
\x05c=Matrix::transpos(a);
\x05cout<<"c矩阵为:"<<endl;
\x05for(i=0;i<inputrow;i++)
\x05\x05for(j=0;j<inputcol;j++)
\x05\x05\x05cout<<c.matrix[i][j]<<endl;
}

再问: 你这个是在哪里运行的???我的是vc6的!
再答: 哦,是在VC 6.0中啊,你的是什么问题? 我知道了,掉了一个 };你把它加在析构函数的外面,表示内的声明结束。
再问: 就是有个是unexpected end of file found 另一个是:member function already defined or declared
再答: 我知道了,加个 }; 程序如下: #include class Matrix { private: int row,colomn; public: double **matrix; static Matrix transpos(const Matrix&); Matrix(int Mrow,int Mcol) { row=Mrow; colomn=Mcol; matrix=new double* [row]; for(int i=0;i