Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------------------------------------------------------------------ MATRIX.H -------------------- //
- #include <iostream>
- using namespace std;
- class WrongDimension{}; // wymiary macierzy
- class WrongDimension2{}; // wymiary macierzy
- class IndexOutOfRange{}; // przekroczenie zakresu tablicy
- class FileOpenError{}; // złe otwarcie pliku
- class cMatrix
- {
- private:
- int col;
- int row;
- public:
- struct sMatrix;
- sMatrix *matrix;
- // konstrktory i destruktor
- cMatrix(int _row, int _col);
- cMatrix(const cMatrix &wzor);
- ~cMatrix();
- int Col() const { return col; }
- int Row() const { return row; }
- void Wypelnij(char *nazwa);
- // operacje na macierzach
- void operator+=(const cMatrix &B);
- void operator-=(const cMatrix &B);
- void operator*=(const cMatrix &B);
- //odczyt i zapis macierzy (operacja na pojedynczych elementach)
- float& operator()(const int i, const int j); // ZAPIS
- float operator()(const int i, const int j) const; // ODCZYT
- cMatrix& operator=(const cMatrix &B);
- friend ostream& operator<<(ostream& stream, const cMatrix &A);
- };
- cMatrix operator+(const cMatrix &A, const cMatrix &B);
- cMatrix operator-(const cMatrix &A, const cMatrix &B);
- cMatrix operator*(const cMatrix &A, const cMatrix &B);
- bool operator==(cMatrix &A, cMatrix &B);
- ostream& operator<<(ostream& stream, const cMatrix &A);
- void SetRow(char *nazwa);
- // ------------------------------------------------------------------------------------------ MATRIX.CPP -------------------- //
- #include <iostream>
- #include <fstream>
- #include "Matrix.h"
- using namespace std;
- struct cMatrix::sMatrix // ---------------------------------------------------------------------------------------------------- POCZĄTEK STRUKTURY
- {
- float **data; // macierz
- int n; // licznik referencji
- int r; // wiersze
- int c; // kolumny
- sMatrix(int _r, int _c) // konstruktor
- {
- n = 1; // tworzymy obiekt więc liczba ref ustawiana na 1
- r = _r;
- c = _c;
- if(r<=0 || c<=0)
- throw IndexOutOfRange();
- data = new float *[r];
- for (int i = 0; i < r; i++)
- data[i] = new float[c];
- for (int i = 0; i < r; i++)
- for (int j = 0; j < c; j++)
- data[i][j] = 0;
- }
- sMatrix(int _r, int _c, float** t)
- {
- n = 1;
- r = _r;
- c = _c;
- if(r<=0 || c<=0)
- throw IndexOutOfRange();
- data = new float *[r];
- for (int i = 0; i < r; i++)
- data[i] = new float[c];
- for (int i = 0; i < r; i++)
- for (int j = 0; j < c; j++)
- data[i][j] = t[i][j];
- }
- ~sMatrix()
- {
- for (int i = 0; i < r; i++)
- delete[] data[i];
- delete[] data;
- };
- void wyp(float **tab)
- {
- for (int i = 0; i < r; i++)
- for (int j = 0; j < c; j++)
- data[i][j] = tab[i][j];
- }
- sMatrix* detach()
- {
- if (n == 1)
- return this;
- sMatrix *t = new sMatrix(r, c, data); //jeżeli n > 1, to do danych odwołuje się jeszcze jakiś obiekt, więc tworzę kopie danych i na niej pracuję
- n--;
- return t;
- }
- }; // ------------------------------------------------------------------------------------------------------------------------ KONIEC STRUKTURY
- // ------------------------------------------------------------------------------------------------------------------------ KONSTURKTORY I DESTRUKTOR
- cMatrix::cMatrix(int _row, int _col)
- {
- row = _row;
- col = _col;
- matrix = new sMatrix(row, col);
- }
- cMatrix::cMatrix(const cMatrix &wzor) // konstuktor kopiujący
- {
- row = wzor.row;
- col = wzor.col;
- wzor.matrix->n++;
- matrix = wzor.matrix;
- }
- cMatrix::~cMatrix()
- {
- if (--(matrix->n) == 0) //zmniejszam ilosc odwołań do obiektu i jeżeli nic więcej się do niego nie odwołuje to usuwam
- delete matrix;
- }
- // -------------------------------------------------------------------------------------------------------------- FUNCKJE
- void cMatrix::Wypelnij(char *nazwa)
- {
- fstream file;
- file.open(nazwa, ios::in);
- float **temp;
- temp = new float *[row];
- for (int i = 0; i < row; i++)
- temp[i] = new float[col];
- if (!file.good()) throw FileOpenError();
- file.seekg(3, ios_base::beg);
- for (int i = 0; i < row; i++)
- for (int j = 0; j < col; j++)
- file >> temp[i][j];
- matrix->wyp(temp);
- for (int i = 0; i < row; i++)
- delete[] temp[i];
- delete[] temp;
- file.close();
- }
- cMatrix operator+(const cMatrix &A, const cMatrix &B)
- {
- if(A.Row()!=B.Row() && A.Col()!=B.Col()) throw WrongDimension();
- cMatrix C(A.Row(),A.Col());
- for (int i = 0; i < A.Row(); i++)
- {
- for (int j = 0; j < A.Col(); j++)
- {
- if(i>=A.Row() || j>=B.Col()) throw IndexOutOfRange();
- C(i, j) = A(i, j) + B(i, j);
- }
- }
- return C;
- }
- void cMatrix::operator+=(const cMatrix &B)
- {
- if(row!=B.Row() && col!=B.Col()) throw WrongDimension();
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < col; j++)
- {
- if(i>=row || j>=col) throw IndexOutOfRange();
- matrix->data[i][j] += B(i,j);
- }
- }
- }
- cMatrix operator-(const cMatrix &A, const cMatrix &B)
- {
- if(A.Row()!=B.Row() && A.Col()!=B.Col()) throw WrongDimension();
- cMatrix C(A.Row(), A.Col());
- for (int i = 0; i < A.Row(); i++)
- {
- for (int j = 0; j < A.Col(); j++)
- {
- if(i>=A.Row() || j>=A.Col()) throw IndexOutOfRange();
- C(i, j) = A(i, j) - B(i, j);
- }
- }
- return C;
- }
- void cMatrix::operator-=(const cMatrix &B)
- {
- if(row!=B.Row() && col!=B.Col()) throw WrongDimension();
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < col; j++)
- {
- if(i>=row || j>=col)
- throw IndexOutOfRange();
- matrix->data[i][j] -= B(i,j);
- }
- }
- }
- cMatrix operator*(const cMatrix &A, const cMatrix &B)
- {
- if( A.Col() != B.Row() ) throw WrongDimension();
- cMatrix C(A.Row(),B.Col());
- for (int i = 0; i < A.Row(); i++)
- {
- for (int j = 0; j < B.Col(); j++)
- {
- for (int k = 0; k < A.Col(); k++)
- {
- if(i>=A.Row() || j>=B.Col() || k>=A.Col())
- throw IndexOutOfRange();
- C(i, j) += A(i, k) * B(k, j);
- }
- }
- }
- return C;
- }
- void cMatrix::operator*=(const cMatrix &B)
- {
- if( col != B.Row() ) throw WrongDimension();
- cMatrix C(row, B.Col());
- for (int i = 0; i < matrix->r; i++)
- {
- for (int j = 0; j < B.matrix->c; j++)
- {
- for (int k = 0; k < matrix->c; k++)
- {
- if(i>=matrix->r || j>=B.matrix->c || k>=matrix->c)
- {
- throw IndexOutOfRange();
- }
- C(i, j) += matrix->data[i][k] * B(k, j);
- }
- }
- }
- for (int i = 0; i < matrix->r; i++)
- {
- for (int j = 0; j < B.matrix->c; j++)
- {
- if(i>=matrix->r || j>=B.matrix->c)
- throw IndexOutOfRange();
- matrix->data[i][j] = C(i, j);
- }
- }
- col = B.Col();
- }
- float& cMatrix::operator()(const int i, const int j) // ZAPIS
- {
- matrix = matrix->detach();
- return matrix->data[i][j];
- }
- float cMatrix::operator()(const int i, const int j) const { return matrix->data[i][j]; } // ODCZYT
- cMatrix& cMatrix:: operator=(const cMatrix &B)
- {
- if (matrix->n == 1)
- {
- delete matrix;
- matrix = B.matrix;
- matrix->n++; //zwiekszam bo kolejny obiekt odwołuje się do danych
- }
- else
- {
- matrix->n--; // nie kopiuje zawartości danych, tylko przekazuje adres
- matrix = B.matrix;
- }
- return *this;
- }
- ostream& operator<<(ostream& stream, const cMatrix &A)
- {
- cout << "Macierz: " << A.row << " x " << A.col << endl;
- for (int i = 0; i < A.row; i++)
- {
- cout << endl;
- for (int j = 0; j < A.col; j++)
- cout << " " << A(i,j);
- }
- cout << endl << endl << endl;
- return stream;
- }
- bool operator==(cMatrix &A, cMatrix &B)
- {
- for (int i = 0; i < A.Row(); i++)
- for (int j = 0; j < A.Col(); j++)
- if (A(i, j) != B(i, j))
- return false;
- return true;
- }
- // ------------------------------------------------------------------------------------------ MAIN.CPP -------------------- //
- #include <iostream>
- #include "Matrix.h"
- #include <fstream>
- using namespace std;
- static int row1;
- static int col1;
- int main()
- { // MAIN
- try
- {
- SetRow((char*)"A.txt");
- cMatrix A(row1, col1);
- A.Wypelnij((char*)"A.txt");
- cout << A;
- SetRow((char*)"B.txt");
- cMatrix B(row1, col1);
- B.Wypelnij((char*)"B.txt");
- cout << B;
- cMatrix C(A.Row(), B.Col()); // macierz wynikowa
- int wybor;
- do{
- cout << "1 - Dodawanie" << endl;
- cout << "2 - Odejmowanie" << endl;
- cout << "3 - Mnozenie" << endl;
- cout << "4 - Porownywanie" << endl;
- cout << "5 - Wyjscie" << endl;
- cin >> wybor;
- cout << endl;
- switch (wybor)
- {
- case 1:
- {
- cout << "Operator + " << endl;
- C = A + B;
- cout << endl;
- cout << "A + B = " << endl;
- cout << C;
- cout << endl << endl;
- cout << "Operator += " << endl;
- A += B;
- cout << endl;
- cout << "A + B = " << endl;
- cout << A;
- cout << endl << endl;
- break;
- }
- case 2:
- {
- cout << "Operator - " << endl;
- C = A - B;
- cout << endl;
- cout << "A - B = " << endl;
- cout << C;
- cout << endl << endl;
- cout << "Operator -= " << endl;
- A -= B;
- cout << endl;
- cout << "A - B = " << endl;
- cout << A;
- cout << endl << endl;
- break;
- }
- case 3:
- {
- cout << "Operator * " << endl;
- C = A * B;
- cout << endl;
- cout << "A * B = " << endl;
- cout << C;
- cout << endl << endl;
- cout << "Operator *= " << endl;
- A *= B;
- cout << endl;
- cout << "A * B = " << endl;
- cout << A;
- cout << endl << endl;
- break;
- }
- case 4:
- {
- cout << endl;
- if (A == B)
- {
- cout << "Macierze sa sobie rowne" << endl;
- }
- else
- {
- cout << "Macierze nie sa sobie rowne" << endl;
- }
- cout << endl << endl;
- break;
- }
- default: if (wybor != 5) cout << "WYBRALES ZLA OPCJE!!" << endl;
- }
- } while (wybor != 5);
- cout << endl << endl;
- } // TRY
- // obsługa błędów
- catch(WrongDimension&) { cout << endl << "ERROR: ZLE WYMIARY MACIERZY! WIELKOSCI MACIERZY MUSZA BYC JEDNAKOWE!!" << endl << endl; }
- catch(WrongDimension2&) { cout << endl << "ERROR: ZLE WYMIARY MACIERZY! LICZBA KOLUMN MACIERZY A MUSI BYC ROWNA LICZBIE WIERSZY MACIERZY B!!" << endl << endl; }
- catch(IndexOutOfRange&) { cout << endl << "ERROR: INDEKS TABLICY POZA PRZEDZIALEM!" << endl << endl; }
- catch(FileOpenError&) { cout << endl << "ERROR: BLAD OTWARCIA PLIKU!" << endl << endl; }
- return 0;
- } // MAIN
- void SetRow(char *nazwa)
- {
- fstream file;
- file.open(nazwa, ios::in);
- if (!file.good()) throw FileOpenError();
- file >> row1;
- file >> col1;
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement