1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
using namespace std;
class matrix
{
public:
matrix(int sizeX,int sizeY);
matrix();
~matrix();
matrix(const matrix& m);
matrix operator+(const matrix& m);
matrix& operator+=(const matrix& m);
friend matrix operator*(const matrix& m1,const matrix& m2);
friend ostream &operator<<(ostream &out,const matrix& m);
friend istream &operator>>(istream &in,const matrix& m);
int dx,dy;
double **p;
private:
void allocArrays()
{
p=new double*[dx];
for(int i=0;i<dx;i++)
{
p[i]=new double[dy];
}
}
};
matrix::matrix(int sizeX,int sizeY):dx(sizeX),dy(sizeY)
{
allocArrays();
for(int i=0; i<dx; i++)
{
for(int j=0; j<dy; j++)
{
p[i][j]=0;
}
}
}
matrix::matrix(const matrix& m):dx(m.dx),dy(m.dy)
{
allocArrays();
for(int i=0; i<dx; i++)
{
for(int j=0; j<dy; j++)
{
p[i][j]=m.p[i][j];
}
}
}
matrix::~matrix()
{
for(int i=0; i<dx; i++)
{
delete [] p[i];
}
delete []p;
}
matrix& matrix::operator +=(const matrix& m)
{
for(int i=0; i<dx; i++)
{
for(int j=0; j<dy; j++)
{
p[i][j]+=m.p[i][j];
}
}
return *this;
}
matrix matrix::operator +(const matrix& m)
{
matrix temp(*this);
return (temp+=m);
}
ostream& operator<<(ostream &out, const matrix& m)
{
for(int i=0; i<m.dx; i++)
{
for(int j=0; j<m.dy; j++)
out<<m.p[i][j]<<" ";
out<<endl;
}
return out;
}
matrix operator*(const matrix& m1,const matrix& m2)
{
matrix prod(m1.dx,m2.dy);
for(int i=0; i<prod.dx;i++)
{
for(int j=0; j<prod.dy; j++)
{
for(int k=0; k<m1.dy; k++)
{
prod.p[i][j]+=m1.p[i][k]*m2.p[k][j];
}
}
}
return prod;
}
int main()
{
matrix x(2,1);
cout<<x;
return 0;
}
|