Advertisement
Voldemord

zad5/6New Matrix.cpp

Jan 10th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.13 KB | None | 0 0
  1. // Matrix.cpp
  2. #include "matrix.h"
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <time.h>
  6. #include <string.h>
  7.  
  8. Matrix::Matrix(){}
  9.  
  10.  
  11. Matrix::~Matrix(){
  12.     for(int i = 0; i < this->m; i++){
  13.         delete []this->matrix[i];
  14.     }
  15.  
  16.     delete []this->matrix;
  17. }
  18.  
  19.  
  20. Matrix::Matrix(int m, int n){
  21.     this->m = m;
  22.     this->n = n;
  23.     allocate(this->m, this->n);
  24. }
  25.  
  26.  
  27. // KONSTRUKTOR TWORZ¥CY MACIERZ NA PODSTAWIE TABLICY
  28. Matrix::Matrix(double** array, int m, int n){
  29.     this->m = m;
  30.     this->n = n;
  31.     allocate(this->m, this->n);
  32.  
  33.     for(int y = 0; y < m; y++){
  34.         for(int x = 0; x < n; x++){
  35.             this->matrix[y][x] = array[y][x];
  36.         }
  37.     }
  38. }
  39.  
  40.  
  41. Matrix::Matrix(Matrix &matrix){
  42.     this->m = m;
  43.     this->n = n;
  44.     allocate(this->m, this->n);
  45.     int tempM = matrix.getM();
  46.     int tempN = matrix.getN();
  47.  
  48.     for(int y = 0; y < tempM; y++){
  49.         for(int x = 0; tempN < n; x++){
  50.            this->matrix[y][x] = matrix.matrix[y][x];
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56. // ZMIANA ROZMIARU MACIERZY (ze strat¹ danych)
  57. void Matrix::reallocate(double** path, int newM, int newN){
  58.     for(int i = 0; i < newM; i++)
  59.         delete[] path[i];
  60.     delete[] path;
  61.  
  62.     allocate(newM, newN);
  63.     this->m = newM;
  64.     this->n = newN;
  65. }
  66.  
  67.  
  68. // ALOKACJA PAMIÊCI dla macierzy o wymiarze m X n
  69. void Matrix::allocate(int m, int n){
  70.     double** path = new double *[m];
  71.     for(int i = 0; i < m; i++){
  72.         path[i] = new double[n];
  73.     }
  74.  
  75.     for(int i = 0; i < m; i++){
  76.         for(int j = 0; j < n; j++){
  77.             path[i][j] = 0;
  78.         }
  79.     }
  80.     this->matrix = path;
  81. }
  82.  
  83. void Matrix::allocate(double** paths, int m, int n){
  84.     double** path = new double *[m];
  85.     for(int i = 0; i < m; i++){
  86.         path[i] = new double[n];
  87.     }
  88.  
  89.     for(int i = 0; i < m; i++){
  90.         for(int j = 0; j < n; j++){
  91.             path[i][j] = 0;
  92.         }
  93.     }
  94.     paths = path;
  95. }
  96.  
  97.  
  98. // FUNKCJA TWORZ¥CA MACIERZ Z LOSOWYCH DOUBLE
  99. void Matrix::randomDoubleMatrix(){
  100.     srand(time(NULL));
  101.     if(this->matrix == NULL){
  102.         allocate(this->m, this->n);
  103.     }
  104.  
  105.     for(int y = 0; y < m; y++){
  106.         for(int x = 0; x < n; x++){
  107.             this->matrix[y][x] = randDouble();
  108.         }
  109.     }
  110. }
  111.  
  112.  
  113. // FUNKCJA LOSUJ¥CA POJEDYNCZY DOUBLE
  114. double Matrix::randDouble(){
  115.     int range = 100000;
  116.     double rangeF = double(range);
  117.     double random = (rand() % (range + 1)) / rangeF;
  118.  
  119.     return random;
  120. }
  121.  
  122.  
  123. void Matrix::printMatrix(){
  124.     if(this->matrix == NULL){
  125.         std::cout << "Earlier you must init matrix!" << std::endl;
  126.  
  127.     }
  128.     else{
  129.         std::cout << "Matrix:" << std::endl;
  130.         for(int y = 0; y < m; y++){
  131.             for(int x = 0; x < n; x++){
  132.                 std::cout << this->matrix[x][y] << " ";
  133.             }
  134.             std::cout << std::endl;
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. // GET LICZBA WIERSZY
  141. Matrix::getM(){
  142.     return this->m;
  143. }
  144.  
  145.  
  146. // GET LICZBA KOLUMN
  147. Matrix::getN(){
  148.     return this->n;
  149. }
  150.  
  151.  
  152. // ZADANIE 6 - przeciążanie operatorów!
  153.  
  154. void Matrix::operator =(Matrix& b){
  155.     m = b.getM();
  156.     n = b.getN();
  157.     allocate(this->m, this->n);
  158.     int tempM = b.getM();
  159.     int tempN = b.getN();
  160.  
  161.     for(int y = 0; y < tempM; y++){
  162.         for(int x = 0; x < tempN; x++){
  163.            this->matrix[y][x] = b.matrix[y][x];
  164.         }
  165.     }
  166. }
  167.  
  168. void Matrix::operator-(){
  169.     for(int y = 0; y < this->m; y++){
  170.         for(int x = 0; x < this->n; x++){
  171.             if(this->matrix[y][x] > 0){
  172.                 this->matrix[y][x] *= -1;
  173.             }
  174.         }
  175.     }
  176. }
  177.  
  178. void Matrix::operator+(){
  179.     for(int y = 0; y < this->m; y++){
  180.         for(int x = 0; x < this->n; x++){
  181.             if(this->matrix[y][x] < 0){
  182.                 this->matrix[y][x] *= -1;
  183.             }
  184.         }
  185.     }
  186. }
  187.  
  188. Matrix& Matrix::operator+(Matrix& b){
  189.     Matrix * tempMatrix = new Matrix(b.getM(), b.getN());
  190.     if(this->getM() != b.getM() || this->getN() != b.getN()){
  191.         // OBSLUGA BLEDU
  192.         std::cout << "Diffrent size of matrixes' + Failed";
  193.     }
  194.     else{
  195.         for(int y = 0; y < this->m; y++){
  196.             for(int x = 0; x < this->n; x++){
  197.                tempMatrix->matrix[y][x] = this->matrix[y][x] + b.matrix[y][x];
  198.             }
  199.         }
  200.     }
  201.     return *tempMatrix;
  202. }
  203.  
  204. Matrix& Matrix::operator-(Matrix& b){
  205.     Matrix * tempMatrix = new Matrix(b.getM(), b.getN());
  206.     if(this->getM() != b.getM() || this->getN() != b.getN()){
  207.         // OBSLUGA BLEDU
  208.         std::cout << "Diffrent size of matrixes' - Failed";
  209.     }
  210.     else{
  211.         for(int y = 0; y < this->m; y++){
  212.             for(int x = 0; x < this->n; x++){
  213.                 //std::cout << " = " << tempMatrix->matrix[y][x] << std::endl;
  214.                 tempMatrix->matrix[y][x] = this->matrix[y][x] - b.matrix[y][x];
  215.             }
  216.         }
  217.     }
  218.     return *tempMatrix;
  219. }
  220.  
  221. void Matrix::operator*(float f){
  222.     for(int y = 0; y < this->m; y++){
  223.         for(int x = 0; x < this->n; x++){
  224.             this->matrix[y][x] *= f;
  225.         }
  226.     }
  227. }
  228.  
  229. void Matrix::operator*(Matrix& m){
  230.     if(this->getM() != m.getM() || this->getN() != m.getN()){
  231.         // OBSLUGA BLEDU
  232.         std::cout << "Diffrent size of matrixes' * Failed";
  233.     }
  234.     else{
  235.         for(int y = 0; y < this->m; y++){
  236.             for(int x = 0; x < this->n; x++){
  237.                 this->matrix[y][x] *= m.matrix[y][x];
  238.             }
  239.         }
  240.     }
  241. }
  242.  
  243. Matrix& Matrix::operator+=(Matrix& m){
  244.     if(this->getM() != m.getM() || this->getN() != m.getN()){
  245.         // OBSLUGA BLEDU
  246.         std::cout << "Diffrent size of matrixes' += Failed";
  247.     }
  248.     else{
  249.         for(int y = 0; y < this->m; y++){
  250.             for(int x = 0; x < this->n; x++){
  251.                 this->matrix[y][x] += m.matrix[y][x];
  252.             }
  253.         }
  254.     }
  255.     return * this;
  256. }
  257.  
  258. Matrix& Matrix::operator-=(Matrix& m){
  259.     if(this->getM() != m.getM() || this->getN() != m.getN()){
  260.         // OBSLUGA BLEDU
  261.         std::cout << "Diffrent size of matrixes' += Failed";
  262.     }
  263.     else{
  264.         for(int y = 0; y < this->m; y++){
  265.             for(int x = 0; x < this->n; x++){
  266.                 this->matrix[y][x] -= m.matrix[y][x];
  267.             }
  268.         }
  269.     }
  270.     return * this;
  271. }
  272.  
  273. Matrix& Matrix::operator*=(float f){
  274.     for(int y = 0; y < this->m; y++){
  275.         for(int x = 0; x < this->n; x++){
  276.             this->matrix[y][x] *= f;
  277.         }
  278.     }
  279.  
  280.     return * this;
  281. }
  282.  
  283. Matrix& Matrix::operator*=(Matrix& m){
  284.     if(this->getM() != m.getM() || this->getN() != m.getN()){
  285.         // OBSLUGA BLEDU
  286.         std::cout << "Diffrent size of matrixes' += Failed";
  287.     }
  288.     else{
  289.         for(int y = 0; y < this->m; y++){
  290.             for(int x = 0; x < this->n; x++){
  291.                 this->matrix[y][x] *= m.matrix[y][x];
  292.             }
  293.         }
  294.     }
  295.     return * this;
  296. }
  297.  
  298. bool Matrix::operator==(Matrix& m){
  299.     if(this->getM() != m.getM() || this->getN() != m.getN()){
  300.         // OBSLUGA BLEDU
  301.         std::cout << "Diffrent size of matrixes' += Failed";
  302.         return false;
  303.     }
  304.     else{
  305.         for(int y = 0; y < this->m; y++){
  306.             for(int x = 0; x < this->n; x++){
  307.                 if(this->matrix[y][x] != m.matrix[y][x]){
  308.                     return false;
  309.                 }
  310.             }
  311.         }
  312.     }
  313.     return true;
  314. }
  315.  
  316. bool Matrix::operator!=(Matrix& m){
  317.     if(this->getM() != m.getM() || this->getN() != m.getN()){
  318.         // OBSLUGA BLEDU
  319.         std::cout << "Diffrent size of matrixes' += Failed";
  320.         return true;
  321.     }
  322.     else{
  323.         for(int y = 0; y < this->m; y++){
  324.             for(int x = 0; x < this->n; x++){
  325.                 if(this->matrix[y][x] != m.matrix[y][x]){
  326.                     return true;
  327.                 }
  328.             }
  329.         }
  330.     }
  331.     return false;
  332. }
  333.  
  334. double& Matrix::operator[](int k){
  335.     double* tab = new double[this->n];
  336.     for(int i = 0; i < this->n; i++){
  337.         tab[i] = this->matrix[k][i];
  338.     }
  339.     return *tab;
  340. }
  341.  
  342. double Matrix::operator()(int y, int x){
  343.     if( y < 0 || x < 0 || y > this->m || x > this->n){
  344.         std::cout << "Wrong index of Matrix()" << std::endl;
  345.     }
  346.     else{
  347.         return this->matrix[y][x];
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement