6.3w+
社区成员
//虽然改造了很多但是还有很多问题
#include <iostream>
using namespace std;
class matrix{
public:
matrix(double*,int,int);
matrix(matrix& it)
{
matrix(it.p, it.m, it.n);
}
~matrix();
matrix& operator+(matrix &s);//重载两个数组相加
double& at(int x, int y){return p[x*n+y];}
int row()const{return m;}
int col()const{return n;}
friend ostream& operator<<(ostream& o, matrix& m);
private:
double* p;
int m,n;
};
matrix::matrix(double* A,int k,int l)
{
m=k;
n=l;
p=new double[m*n];
for(int i=0;i <m;i++)
for(int j=0;j <n;j++)
p[n*i+j]=A[n*i+j];
}
matrix::~matrix()
{
delete [] p;
m=n=0;
}
matrix& matrix::operator+(matrix &s)
{
if(s.m==m&&s.n==n)
{
for(int i=0;i <m;i++)
for(int j=0;j <n;j++)
p[i*n+j]+=s.p[i*n+j];
}
else
cout <<"error!两个数组不能相加" <<endl;
return *this;
}
ostream& operator<<(ostream& o, matrix& m)
{
for (int i = 0; i < m.row(); (o << endl), ++i)
for (int j = 0; j < m.col(); ++j)
o << m.at(i, j) << '\t';
return o;
}
int main()
{
double a[]={2,2,2,2,
2,2,2,2,
2,2,2,2,
2,2,2,2};
matrix amatrix(a,4,4),bmatrix(a,4,4);
cout << (amatrix +bmatrix);
cin.get();
return 0;
}
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4