Advertisement
Smudla

Matrix C++

Jun 2nd, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <assert.h>
  3.  
  4. class Matrix
  5. {
  6. public:
  7.     //dekladace parametrickeho konstruktoru
  8.     Matrix(const unsigned int columnsCount,const unsigned int rowsCount);
  9.     //kopirovaci konstruktor
  10.     Matrix(const Matrix& MatrixToCopy)
  11.         : _rowsCount(MatrixToCopy._rowsCount), _columnsCount(MatrixToCopy._columnsCount)
  12.     {
  13.         matrix = new int*[_rowsCount];
  14.         for (size_t i = 0; i < _rowsCount; i++) {
  15.             matrix[i] = new int[_columnsCount];
  16.         }
  17.  
  18.         for (size_t i = 0; i < _rowsCount; i++) {
  19.             for (size_t j = 0; j < _columnsCount; ++j) {
  20.                 matrix[i][j] = MatrixToCopy.matrix[i][j];
  21.             }
  22.         }
  23.     }
  24.    
  25.     //dekladace destruktoru
  26.     ~Matrix();
  27.  
  28.     void fillMatrixWithRandom(int bottomBound, int upperBound){
  29.         for (int i = 0; i < _rowsCount; ++i){
  30.             for (size_t j = 0; j < _columnsCount; j++)
  31.             {
  32.                 matrix[i][j] = generateNumber(bottomBound, upperBound);
  33.             }
  34.         }
  35.     }
  36.  
  37.     //operátor +
  38.     Matrix operator+(const Matrix& right) const
  39.     {
  40.         assert(Matrix::doesMatrixDimensionsMatch(right));
  41.         Matrix MatrixToReturn = Matrix(_rowsCount, _columnsCount);
  42.         for (size_t i = 0; i < _rowsCount; i++) {
  43.             for (size_t j = 0; j < _columnsCount; j++) {
  44.                 MatrixToReturn.matrix[i][j] = matrix[i][j] + right.matrix[i][j];
  45.             }
  46.         }
  47.         return MatrixToReturn;
  48.     }
  49.  
  50.     //operátor -
  51.     Matrix operator-(const Matrix& right) const
  52.     {
  53.         assert(Matrix::doesMatrixDimensionsMatch(right));
  54.         Matrix MatrixToReturn = Matrix(_rowsCount, _columnsCount);
  55.         for (size_t i = 0; i < _rowsCount; i++) {
  56.             for (size_t j = 0; j < _columnsCount; j++) {
  57.                 MatrixToReturn.matrix[i][j] = matrix[i][j] - right.matrix[i][j];
  58.             }
  59.         }
  60.         return MatrixToReturn;
  61.     }
  62.  
  63.     //operátor ==
  64.     bool operator==(const Matrix& right)const{
  65.         if (this == &right){
  66.             //if (*this == right) Nejde protoze by nastalo zacykleni rekurze;
  67.             return true;
  68.         }
  69.         if (!doesMatrixDimensionsMatch(right))
  70.             return false;
  71.         for (size_t i = 0; i < _rowsCount; i++)
  72.         {  
  73.             for (size_t j = 0; j < _columnsCount; j++)
  74.             {
  75.                 if (this->matrix[i][j] != right.matrix[i][j]){
  76.                     return false;
  77.                 }
  78.             }
  79.         }
  80.         return true;
  81.     }
  82.  
  83.     //operátor !=
  84.     bool operator!=(const Matrix& right)const{
  85.         if (this == &right){
  86.             //if (*this == right) Nejde protoze by nastalo zacykleni rekurze;
  87.             return false;
  88.         }
  89.         if (!doesMatrixDimensionsMatch(right))
  90.             return true;
  91.         for (size_t i = 0; i < _rowsCount; i++)
  92.         {
  93.             for (size_t j = 0; j < _columnsCount; j++)
  94.             {
  95.                 if (this->matrix[i][j] != right.matrix[i][j]){
  96.                     return true;
  97.                 }
  98.             }
  99.         }
  100.         return false;
  101.     }
  102.  
  103.  
  104.  
  105.     //operátor přiřazení
  106.     Matrix& operator=(const Matrix& right){
  107.         if (this == &right)
  108.             return *this;
  109.         if (_columnsCount == right._columnsCount && _rowsCount == right._columnsCount){
  110.             for (size_t i = 0; i < _rowsCount; i++)
  111.             {
  112.                 for (size_t j = 0; j < _columnsCount; j++)
  113.                 {
  114.                     this->matrix[i][j] = right.matrix[i][j];
  115.                 }
  116.             }
  117.             return *this;
  118.         }
  119.         for (int i = 0; i < _rowsCount; i++) {
  120.             delete[] matrix[i];
  121.             matrix[i] = nullptr;
  122.         }
  123.         delete[] matrix;
  124.         _columnsCount = right._columnsCount;
  125.         _rowsCount = right._rowsCount;
  126.         matrix = new int*[_rowsCount];
  127.  
  128.         for (size_t i = 0; i < _rowsCount; i++) {
  129.             matrix[i] = new int[_columnsCount];
  130.         }
  131.  
  132.         for (size_t i = 0; i < _rowsCount; i++)
  133.         {
  134.             for (size_t j = 0; j < _columnsCount; j++)
  135.             {
  136.                 matrix[i][j] = right.matrix[i][j];
  137.             }
  138.         }
  139.         return *this;
  140.     }
  141.  
  142.     //operátor *
  143.     Matrix operator*(int number) const
  144.     {
  145.         Matrix MatrixToReturn = Matrix(_rowsCount, _columnsCount);
  146.         for (size_t i = 0; i < _rowsCount; i++) {
  147.             for (size_t j = 0; j < _columnsCount; j++) {
  148.                 MatrixToReturn.matrix[i][j] = this->matrix[i][j] * number;
  149.             }
  150.         }
  151.         return MatrixToReturn;
  152.     }
  153.  
  154.     void MatrixToString()const{
  155.         for (size_t i = 0; i < _rowsCount; i++) {
  156.             for (size_t j = 0; j < _columnsCount; j++) {
  157.                 std::cout << matrix[i][j] << " ";
  158.             }
  159.             std::cout << " " << std::endl;
  160.         }
  161.         std::cout << std::endl;
  162.     }
  163.  
  164.     int& At(size_t indexX, size_t indexY){
  165.         for (size_t i = 0; i < _rowsCount; i++)
  166.         {
  167.             for (size_t j = 0; j < _columnsCount; j++)
  168.             {
  169.                 if (i == indexX && j == indexY)
  170.                     return matrix[i][j];
  171.             }
  172.         }
  173.         throw "IndexX or indexY is out of Matrix";
  174.     }
  175.  
  176.     int At(size_t indexX, size_t indexY)const{
  177.         for (size_t i = 0; i < _rowsCount; i++)
  178.         {
  179.             for (size_t j = 0; j < _columnsCount; j++)
  180.             {
  181.                 if (i == indexX && j == indexY)
  182.                     return matrix[i][j];
  183.             }
  184.         }
  185.         throw "IndexX or indexY is out of Matrix";
  186.     }
  187.  
  188.  
  189. private:
  190.     size_t _columnsCount;
  191.     size_t _rowsCount;
  192.     int** matrix;
  193.  
  194.     //metoda na generovani nahodneho cisla z intervalu
  195.     int generateNumber(int bottomBound, int upperBound){
  196.         return bottomBound + rand() % (upperBound - bottomBound + 1);
  197.     }
  198.  
  199.     //privatni metoda na overeni rozmeru matice
  200.     bool doesMatrixDimensionsMatch(const Matrix& right)const{
  201.         if (_rowsCount == right._rowsCount && _columnsCount == right._columnsCount){
  202.             return true;
  203.         }
  204.         else{
  205.             return false;
  206.         }
  207.     }
  208.  
  209. };
  210.  
  211. Matrix::Matrix(const unsigned int columnsCount, const unsigned int rowsCount)
  212. {
  213.     matrix = new int*[rowsCount];
  214.     this->_columnsCount = columnsCount;
  215.     this->_rowsCount = rowsCount;
  216.     //alokace + vynulovani
  217.     for (int i = 0; i < rowsCount; ++i){
  218.         matrix[i] = new int[columnsCount];
  219.         for (size_t j = 0; j < columnsCount; j++)
  220.         {
  221.             matrix[i][j] = 0;
  222.         }
  223.     }
  224. }
  225.  
  226. std::ostream& operator<<(std::ostream& stream, const Matrix& matrix){
  227.     matrix.MatrixToString();
  228.     return stream;
  229. }
  230.  
  231. //destruktor je treba protoze new se alokuje na heapu
  232. Matrix::~Matrix()
  233. {
  234.     for (int i = 0; i < _rowsCount; i++) {
  235.         delete[] matrix[i];
  236.         matrix[i] = nullptr;
  237.     }
  238.     delete[] matrix;
  239.     matrix = nullptr;
  240. }
  241.  
  242. int main(int argc, char** argv)
  243. {
  244.     Matrix M = Matrix(5, 4);
  245.     Matrix m1 = Matrix(3, 3);
  246.     Matrix m2 = Matrix(3, 3);
  247.     M.fillMatrixWithRandom(10, 10);
  248.     m1.fillMatrixWithRandom(10, 20);
  249.     m2.fillMatrixWithRandom(5, 15);
  250.     M.MatrixToString();
  251.     m1.MatrixToString();
  252.     m2.MatrixToString();
  253.     Matrix m3 = m1 * 2;
  254.     m3.MatrixToString();
  255.     bool test = false;
  256.     test = m1 == m2;
  257.     std::cout << test << std::endl;
  258.     test = m1 != m2;
  259.     std::cout << test << std::endl;
  260.     M.MatrixToString();
  261.     try{
  262.         std::cout << M.At(9, 1) << std::endl;
  263.     }
  264.     catch (const char* msg){
  265.         std::cout << msg << std::endl;
  266.     }
  267.     try{
  268.         std::cout << M.At(3, 1) << std::endl;
  269.     }
  270.     catch (const char* msg){
  271.         std::cout << msg << std::endl;
  272.     }
  273.  
  274.     std::cout << M << std::endl;
  275.     Matrix mError = m1 + M;      //vynuti abort
  276.     mError.MatrixToString();
  277.     system("pause");
  278.     return 1;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement