You are on page 1of 5

#include <iostream>

using namespace std;


class matrix
{
private:
float **A;
int n, m, err;
public:
matrix();
matrix(int, float);
matrix(int, int, float);
matrix(const matrix&);
~matrix();
void intro();
void show();
void suma(const matrix &matr);
void produs(const matrix &matr);
void diferenta(const matrix &matr);
void inmultire(float val);
float get_element(int, int);
void print_err();
int get_err();
int get_n();
int get_m();
};
/***********************************************/
matrix::matrix()
{
err = 0;
n = m = 1;
if (!(A = new float*[n])) err = 1;
else
if (!(A[0] = new float[m])) err = 1;
else
{
A[n][m] = 0;
}
}
/***********************************************/
matrix::matrix(int dim, float val = 0)
{
err = 0;
if (dim >= 0)
{
n = m = dim;
if (!(A = new float*[n])) err = 1;
else
for (int i = 0; i<n; i++)
if (!(A[i] = new float[m])) err = 1;
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++)
A[i][j] = val;
}
else err = 2;

}
/***********************************************/
matrix::matrix(int n1, int m1, float val = 0)
{
n = n1;
m = m1;
err = 0;
if ((n >= 0) && (m >= 0))
{
if (!(A = new float*[n])) err = 1;
else
for (int i = 0; i<n; i++)
if (!(A[i] = new float[m])) err = 1;
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++) A[i][j] = val;
}
else err = 2;
}
/***********************************************/
matrix::matrix(const matrix& B)
{
err = 0;
n = B.n;
m = B.m;
if (!(A = new float*[n])) err = 1;
else
for (int i = 0; i<n; i++)
if (!(A[i] = new float[m])) err = 1;
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++) A[i][j] = B.A[i][j];
}
/***********************************************/
matrix::~matrix()
{
err = 0;
for (int i = 0; i<n; i++)
delete[] A[i];
delete[] A;
A = NULL;
n = m = 0;
}
/***********************************************/
int matrix::get_n()
{
err = 0;
return n;
}
/***********************************************/
int matrix::get_m()
{
err = 0;
return m;
}
/***********************************************/
int matrix::get_err()
{
return err;
}
/***********************************************/
float matrix::get_element(int i, int j)

{
if ((i<0)&(i>n)&(j<0)&(j>m)) { err = 5; return -1; }
else
return A[i][j];
}
/***********************************************/
void matrix::print_err()
{
switch (err)
{
case 0: { cout << "Totul e OK." << endl; break; }
case 1: { cout << "Nu este de ajuns memorie." << endl; break; }
case 2: { cout << "Dimensiunea matricei trebuie sa fie pozitiva." << end
l; break; }
case 3: { cout << "Trecerea peste limitele matricei." << endl; break; }
case 4: { cout << "Dimensiunile matricelor nu coincid." << endl; break;
}
case 5: { cout << "Matricele nu sunt compatibile pentru inmultire." << e
ndl; break; }
}
cout << "Press any key to continue..." << endl;
}
/***********************************************/
void matrix::intro()
{
cout << "Numarul de linii = " << n <<endl<< "Numarul de coloane = " << m
<< endl;
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++)
cin >> A[i][j];
}
/***********************************************/
void matrix::show()
{
for (int i = 0; i<n; i++)
{
for (int j = 0; j<m; j++)
cout << A[i][j] << " ";
cout << endl;
}
}
/***********************************************/
void matrix::suma(const matrix &matr)
{
err = 0;
//matrix temp(n,m);
if ((n == matr.n) && (m == matr.m))
{
for (int i = 0; i<matr.n; i++)
for (int j = 0; j<matr.m; j++)
A[i][j] += matr.A[i][j];
}
else err = 4;

}
/***********************************************/
void matrix::diferenta(const matrix &matr)
{
err = 0;
if ((n == matr.n) && (m == matr.m))
{
for (int i = 0; i<matr.n; i++)
for (int j = 0; j<matr.m; j++)
A[i][j] -= matr.A[i][j];
}
else err = 4;
}
/***********************************************/
void matrix::produs(const matrix &vec)
{
err = 0;
matrix temp(n, vec.m);
if (m == vec.n)
{
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++)
{
float tmp = 0;
for (int k = 0; k<vec.n; k++)
tmp += A[i][k] * vec.A[k][j];
temp.A[i][j] = tmp;
}
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++)
A[i][j] = temp.A[i][j];
}
else err = 5;
}
/***********************************************/
void matrix::inmultire(float val)
{
for (int i = 0; i<n; i++)
for (int j = 0; j<m; j++)
A[i][j] *= val;
}
/***********************************************/
void main()
{
matrix m1(3);
cout << "Dati prima matrice" << endl;
m1.intro();
cout << "Matricea introdusa: " << endl;
m1.show();
matrix m2(3);
cout << endl << "Dati a doua matrice" << endl;
m2.intro();
cout << "Matricea introdusa: " << endl;
m2.show();

system("cls");
cout << "Matricea 1" << endl;
m1.show();
cout << "Matricea 2" << endl;
m2.show();
m1.suma(m2);
cout << "Suma m1+m2: " << endl;
m1.show();

m2.diferenta(m1);
cout << "Diferenta m2-m1: " << endl;
m2.show();

m1.produs(m2);
cout << "Produsul m1*m2: " << endl;
m1.show();
system("pause");
}