Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <assert.h>
- class Matrix
- {
- public:
- //dekladace parametrickeho konstruktoru
- Matrix(const unsigned int columnsCount,const unsigned int rowsCount);
- //kopirovaci konstruktor
- Matrix(const Matrix& MatrixToCopy)
- : _rowsCount(MatrixToCopy._rowsCount), _columnsCount(MatrixToCopy._columnsCount)
- {
- matrix = new int*[_rowsCount];
- for (size_t i = 0; i < _rowsCount; i++) {
- matrix[i] = new int[_columnsCount];
- }
- for (size_t i = 0; i < _rowsCount; i++) {
- for (size_t j = 0; j < _columnsCount; ++j) {
- matrix[i][j] = MatrixToCopy.matrix[i][j];
- }
- }
- }
- //dekladace destruktoru
- ~Matrix();
- void fillMatrixWithRandom(int bottomBound, int upperBound){
- for (int i = 0; i < _rowsCount; ++i){
- for (size_t j = 0; j < _columnsCount; j++)
- {
- matrix[i][j] = generateNumber(bottomBound, upperBound);
- }
- }
- }
- //operátor +
- Matrix operator+(const Matrix& right) const
- {
- assert(Matrix::doesMatrixDimensionsMatch(right));
- Matrix MatrixToReturn = Matrix(_rowsCount, _columnsCount);
- for (size_t i = 0; i < _rowsCount; i++) {
- for (size_t j = 0; j < _columnsCount; j++) {
- MatrixToReturn.matrix[i][j] = matrix[i][j] + right.matrix[i][j];
- }
- }
- return MatrixToReturn;
- }
- //operátor -
- Matrix operator-(const Matrix& right) const
- {
- assert(Matrix::doesMatrixDimensionsMatch(right));
- Matrix MatrixToReturn = Matrix(_rowsCount, _columnsCount);
- for (size_t i = 0; i < _rowsCount; i++) {
- for (size_t j = 0; j < _columnsCount; j++) {
- MatrixToReturn.matrix[i][j] = matrix[i][j] - right.matrix[i][j];
- }
- }
- return MatrixToReturn;
- }
- //operátor ==
- bool operator==(const Matrix& right)const{
- if (this == &right){
- //if (*this == right) Nejde protoze by nastalo zacykleni rekurze;
- return true;
- }
- if (!doesMatrixDimensionsMatch(right))
- return false;
- for (size_t i = 0; i < _rowsCount; i++)
- {
- for (size_t j = 0; j < _columnsCount; j++)
- {
- if (this->matrix[i][j] != right.matrix[i][j]){
- return false;
- }
- }
- }
- return true;
- }
- //operátor !=
- bool operator!=(const Matrix& right)const{
- if (this == &right){
- //if (*this == right) Nejde protoze by nastalo zacykleni rekurze;
- return false;
- }
- if (!doesMatrixDimensionsMatch(right))
- return true;
- for (size_t i = 0; i < _rowsCount; i++)
- {
- for (size_t j = 0; j < _columnsCount; j++)
- {
- if (this->matrix[i][j] != right.matrix[i][j]){
- return true;
- }
- }
- }
- return false;
- }
- //operátor přiřazení
- Matrix& operator=(const Matrix& right){
- if (this == &right)
- return *this;
- if (_columnsCount == right._columnsCount && _rowsCount == right._columnsCount){
- for (size_t i = 0; i < _rowsCount; i++)
- {
- for (size_t j = 0; j < _columnsCount; j++)
- {
- this->matrix[i][j] = right.matrix[i][j];
- }
- }
- return *this;
- }
- for (int i = 0; i < _rowsCount; i++) {
- delete[] matrix[i];
- matrix[i] = nullptr;
- }
- delete[] matrix;
- _columnsCount = right._columnsCount;
- _rowsCount = right._rowsCount;
- matrix = new int*[_rowsCount];
- for (size_t i = 0; i < _rowsCount; i++) {
- matrix[i] = new int[_columnsCount];
- }
- for (size_t i = 0; i < _rowsCount; i++)
- {
- for (size_t j = 0; j < _columnsCount; j++)
- {
- matrix[i][j] = right.matrix[i][j];
- }
- }
- return *this;
- }
- //operátor *
- Matrix operator*(int number) const
- {
- Matrix MatrixToReturn = Matrix(_rowsCount, _columnsCount);
- for (size_t i = 0; i < _rowsCount; i++) {
- for (size_t j = 0; j < _columnsCount; j++) {
- MatrixToReturn.matrix[i][j] = this->matrix[i][j] * number;
- }
- }
- return MatrixToReturn;
- }
- void MatrixToString()const{
- for (size_t i = 0; i < _rowsCount; i++) {
- for (size_t j = 0; j < _columnsCount; j++) {
- std::cout << matrix[i][j] << " ";
- }
- std::cout << " " << std::endl;
- }
- std::cout << std::endl;
- }
- int& At(size_t indexX, size_t indexY){
- for (size_t i = 0; i < _rowsCount; i++)
- {
- for (size_t j = 0; j < _columnsCount; j++)
- {
- if (i == indexX && j == indexY)
- return matrix[i][j];
- }
- }
- throw "IndexX or indexY is out of Matrix";
- }
- int At(size_t indexX, size_t indexY)const{
- for (size_t i = 0; i < _rowsCount; i++)
- {
- for (size_t j = 0; j < _columnsCount; j++)
- {
- if (i == indexX && j == indexY)
- return matrix[i][j];
- }
- }
- throw "IndexX or indexY is out of Matrix";
- }
- private:
- size_t _columnsCount;
- size_t _rowsCount;
- int** matrix;
- //metoda na generovani nahodneho cisla z intervalu
- int generateNumber(int bottomBound, int upperBound){
- return bottomBound + rand() % (upperBound - bottomBound + 1);
- }
- //privatni metoda na overeni rozmeru matice
- bool doesMatrixDimensionsMatch(const Matrix& right)const{
- if (_rowsCount == right._rowsCount && _columnsCount == right._columnsCount){
- return true;
- }
- else{
- return false;
- }
- }
- };
- Matrix::Matrix(const unsigned int columnsCount, const unsigned int rowsCount)
- {
- matrix = new int*[rowsCount];
- this->_columnsCount = columnsCount;
- this->_rowsCount = rowsCount;
- //alokace + vynulovani
- for (int i = 0; i < rowsCount; ++i){
- matrix[i] = new int[columnsCount];
- for (size_t j = 0; j < columnsCount; j++)
- {
- matrix[i][j] = 0;
- }
- }
- }
- std::ostream& operator<<(std::ostream& stream, const Matrix& matrix){
- matrix.MatrixToString();
- return stream;
- }
- //destruktor je treba protoze new se alokuje na heapu
- Matrix::~Matrix()
- {
- for (int i = 0; i < _rowsCount; i++) {
- delete[] matrix[i];
- matrix[i] = nullptr;
- }
- delete[] matrix;
- matrix = nullptr;
- }
- int main(int argc, char** argv)
- {
- Matrix M = Matrix(5, 4);
- Matrix m1 = Matrix(3, 3);
- Matrix m2 = Matrix(3, 3);
- M.fillMatrixWithRandom(10, 10);
- m1.fillMatrixWithRandom(10, 20);
- m2.fillMatrixWithRandom(5, 15);
- M.MatrixToString();
- m1.MatrixToString();
- m2.MatrixToString();
- Matrix m3 = m1 * 2;
- m3.MatrixToString();
- bool test = false;
- test = m1 == m2;
- std::cout << test << std::endl;
- test = m1 != m2;
- std::cout << test << std::endl;
- M.MatrixToString();
- try{
- std::cout << M.At(9, 1) << std::endl;
- }
- catch (const char* msg){
- std::cout << msg << std::endl;
- }
- try{
- std::cout << M.At(3, 1) << std::endl;
- }
- catch (const char* msg){
- std::cout << msg << std::endl;
- }
- std::cout << M << std::endl;
- Matrix mError = m1 + M; //vynuti abort
- mError.MatrixToString();
- system("pause");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement