avatar
Untitled

Guest 90 15th Apr, 2024

CPP 6.40 KB
                                           
                         // ---------------------------- matrix.cpp 
#include <xstring>
#include <stdexcept>
#include <vector>
#include <iostream>
using namespace std;

class Matrix {
private:
    
    int rows;
    int cols;
    int** tab;
    std::vector<std::vector<int>> data;
    
public:
    friend void trans(Matrix& matrix);
    // Konstruktor domyślny
    Matrix() : rows(0), cols(0), tab(nullptr) {}

    // Konstruktor z określonymi wymiarami
    Matrix(int rows, int cols) : rows(rows), cols(cols) {
        tab = new int* [rows];
        for (int i = 0; i < rows; ++i) {
            tab[i] = new int[cols];
        }
    }

    // Konstruktor kopiujący
    Matrix(const Matrix& matrix) : rows(matrix.rows), cols(matrix.cols) {
        tab = new int* [rows];
        for (int i = 0; i < rows; ++i) {
            tab[i] = new int[cols];
            for (int j = 0; j < cols; ++j) {
                tab[i][j] = matrix.tab[i][j];
            }
        }
    }

    // Destruktor
    ~Matrix() {
        for (int i = 0; i < rows; ++i) {
            delete[] tab[i];
        }
        delete[] tab;
    }
    // ------------------- OPERATORY 
    Matrix& operator=(const Matrix& matrix) {
        if (this == &matrix) {
            return *this; // Self-assignment guard
        }

        // Dealocate existing memory
        for (int i = 0; i < rows; ++i) {
            delete[] tab[i];
        }
        delete[] tab;

        // Copy dimensions
        rows = matrix.rows;
        cols = matrix.cols;

        // Allocate new memory
        tab = new int* [rows];
        for (int i = 0; i < rows; ++i) {
            tab[i] = new int[cols];
            for (int j = 0; j < cols; ++j) {
                tab[i][j] = matrix.tab[i][j];
            }
        }

        return *this;
    }
    bool operator==(const Matrix& other) const {
        if (rows != other.rows || cols != other.cols) {
            return false; // Różne wymiary, więc nie są równe
        }

        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (tab[i][j] != other.tab[i][j]) {
                    return false; // Różne wartości, więc nie są równe
                }
            }
        }

        return true; // Wszystkie elementy są takie same
    }
    //operator indeksowania 
    int& operator()(int row, int col) {
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            return tab[row][col];
        }
        else {
            // Obsłuż błąd, np. rzucając wyjątek lub zwracając domyślną wartość
            throw std::out_of_range("Invalid matrix indices");
        }
    }
    // Metoda do wyświetlania zawartości macierzy
    void display() {
        cout << "Zawartość macierzy: " << endl;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                cout << tab[i][j] << " ";
            }
            cout << endl << endl;
        }
    }
    Matrix operator+(const Matrix& matrix) const {
        if (rows != matrix.rows || cols != matrix.cols) {
            throw std::runtime_error("Nie można dodać macierzy o różnych wymiarach.");
        }
        Matrix result(rows, cols);
        for (size_t i = 0; i < rows; ++i) {
            for (size_t j = 0; j < cols; ++j) {
                result.tab[i][j] = tab[i][j] + matrix.tab[i][j];
            }
        }
        return result;
    }
    Matrix operator-(const Matrix& matrix) const {
        if (rows != matrix.rows || cols != matrix.cols) {
            throw std::runtime_error("Nie można odejmować macierzy o różnych wymiarach.");
        }

        Matrix result(rows, cols);
        for (size_t i = 0; i < rows; ++i) {
            for (size_t j = 0; j < cols; ++j) {
                result.tab[i][j] = tab[i][j] -matrix.tab[i][j];
            }
        }
        return result;
    }
    Matrix operator*(double scalar) const {
        Matrix result(rows, cols);
        for (size_t i = 0; i < rows; ++i) {
            for (size_t j = 0; j < cols; ++j) {
                result.tab[i][j] = tab[i][j] * scalar;
            }
        }
        return result;
    }
    // Przeciążony operator mnożenia macierzy
    Matrix operator*(const Matrix& matrix) const {
        if (cols != matrix.rows) {
            throw std::runtime_error("Nie można pomnożyć macierzy o nieodpowiednich wymiarach.");
        }

        Matrix result(rows, matrix.cols);
        for (size_t i = 0; i < rows; ++i) {
            for (size_t j = 0; j < matrix.cols; ++j) {
                double sum = 0.0;
                for (size_t k = 0; k < cols; ++k) {
                    sum += tab[i][k] * matrix.tab[k][j];
                }
                result.tab[i][j] = sum;
            }
        }
        return result;
    }
    // Przeciążony operator wyjścia
    friend ostream& operator<<(ostream& os, const Matrix& matrix) {
        for (size_t i = 0; i < matrix.rows; ++i) {
            for (size_t j = 0; j < matrix.cols; ++j) {
                os << matrix.tab[i][j] << " ";
            }
            os << "\n";
        }
        return os;
    }
    void trans(const Matrix& matrix) {
        // Tworzymy nową macierz o wymiarach odwrotnych do macierzy wejściowej
        Matrix trans(matrix.cols, matrix.rows);

        // Transponujemy macierz
        for (int i = 0; i < matrix.rows; ++i) {
            for (int j = 0; j < matrix.cols; ++j) {
                trans(j, i) = matrix(i, j);
            }
        }

        // Przypisujemy transponowaną macierz do macierzy wejściowej
        matrix = trans;
    }


    /*friend void transponowanie_macierzy(double**& matrix, int size)
    {
        for (int i = 0; i < size; ++i)
            for (int j = i + 1; j < size; ++j)
                swap(matrix[i][j], matrix[j][i]);
    }*/
};
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM