用面向对象程序设计(OOP)方法实现一个基本矩阵类 20
具备以下功能:矩阵的输入、输出矩阵的转置矩阵的加法、减法、乘法运算求矩阵的秩、方阵求逆矩阵的QR分解、SVD分解图形界面、复数矩阵类、全英文注释...
具备以下功能:
矩阵的输入、输出
矩阵的转置
矩阵的加法、减法、乘法运算
求矩阵的秩、方阵求逆
矩阵的QR分解、SVD分解
图形界面、复数矩阵类、全英文注释 展开
矩阵的输入、输出
矩阵的转置
矩阵的加法、减法、乘法运算
求矩阵的秩、方阵求逆
矩阵的QR分解、SVD分解
图形界面、复数矩阵类、全英文注释 展开
1个回答
展开全部
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
//m*n 阶矩阵
class M
{
public:
M(int m, int n);
M(M& m);
virtual ~M();
int m;
int n;
private:
int *a;
friend istream& operator>>( istream& in, M& m );
friend ostream& operator<<( ostream& out, M& m );
friend M& operator+(M& a, M& b);
};
M::M(int m=1, int n=1) : m(m), n(n)
{
a = new int[n*m];
}
//拷贝构造
M::M(M& m) : m(m.m), n(m.n)
{
a = new int[m.n*m.m];
memcpy(a, m.a, m.n*m.m*sizeof(int));
}
M::~M()
{
delete a;
}
//输入矩阵
istream& operator>>( istream& in, M& m )
{
cout<<"input "<<m.m<<"*"<<m.n<<" matrix"<<endl;
for( int i=0; i<m.m; i++ )
{
cout<<"input line "<<i+1<<":";//输入第i+1 行
for( int j=0; j<m.n; j++ )
{
in >> (*(m.a+i*m.n+j));
}
}
return in;
}
//输出矩阵
ostream& operator<<( ostream& out, M& m )
{
for( int i=0; i<m.m; i++ )
{
for( int j=0; j<m.n; j++ )
{
out<<setw(5)<<(*(m.a+i*m.n+j));
}
out<<endl;
}
return out;
}
//矩阵加法
M& operator+(M& a, M& b)
{
if( !( a.m==b.m && a.n==b.n ) )
{
cout<<"Error: can not +"<<endl;
}
M m(a);
for( int i=0; i<m.m; i++ )
{
for( int j=0; j<m.n; j++ )
{
*(m.a+i*m.n+j) += *(b.a+i*m.n+j);
}
}
M* x = new M(m);
return *x;
}
int main(int argc, char *argv[])
{
M a(2, 3), b(2, 3);
cin>>a>>b;
M c = a + b;
cout<<a<<"--------"<<endl<<b<<"--------"<<endl<<c<<"--------"<<endl;
return 0;
}
#include <iomanip>
#include <string.h>
using namespace std;
//m*n 阶矩阵
class M
{
public:
M(int m, int n);
M(M& m);
virtual ~M();
int m;
int n;
private:
int *a;
friend istream& operator>>( istream& in, M& m );
friend ostream& operator<<( ostream& out, M& m );
friend M& operator+(M& a, M& b);
};
M::M(int m=1, int n=1) : m(m), n(n)
{
a = new int[n*m];
}
//拷贝构造
M::M(M& m) : m(m.m), n(m.n)
{
a = new int[m.n*m.m];
memcpy(a, m.a, m.n*m.m*sizeof(int));
}
M::~M()
{
delete a;
}
//输入矩阵
istream& operator>>( istream& in, M& m )
{
cout<<"input "<<m.m<<"*"<<m.n<<" matrix"<<endl;
for( int i=0; i<m.m; i++ )
{
cout<<"input line "<<i+1<<":";//输入第i+1 行
for( int j=0; j<m.n; j++ )
{
in >> (*(m.a+i*m.n+j));
}
}
return in;
}
//输出矩阵
ostream& operator<<( ostream& out, M& m )
{
for( int i=0; i<m.m; i++ )
{
for( int j=0; j<m.n; j++ )
{
out<<setw(5)<<(*(m.a+i*m.n+j));
}
out<<endl;
}
return out;
}
//矩阵加法
M& operator+(M& a, M& b)
{
if( !( a.m==b.m && a.n==b.n ) )
{
cout<<"Error: can not +"<<endl;
}
M m(a);
for( int i=0; i<m.m; i++ )
{
for( int j=0; j<m.n; j++ )
{
*(m.a+i*m.n+j) += *(b.a+i*m.n+j);
}
}
M* x = new M(m);
return *x;
}
int main(int argc, char *argv[])
{
M a(2, 3), b(2, 3);
cin>>a>>b;
M c = a + b;
cout<<a<<"--------"<<endl<<b<<"--------"<<endl<<c<<"--------"<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询