Advertisement
Guest User

Matrix

a guest
Apr 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. //
  2. // Created by anna on 20.04.18.
  3. //
  4. //declaration
  5. #ifndef MATRIX_DECLARATION_H
  6. #define MATRIX_DECLARATION_H
  7.  
  8.  
  9. #include <algorithm>
  10. #include <iostream>
  11. using namespace std;
  12.  
  13.  
  14.  
  15. class Matrix{
  16.     private: int m, n;
  17.             int** array;
  18.     public: Matrix(int**, int, int);
  19.             int countColumns();
  20.             int countRows();
  21.             void addColumn(int*);
  22.             void addRow(int*);
  23.             ~ Matrix();
  24.             Matrix(const Matrix &obj);
  25.             Matrix&operator= (const Matrix &obj);
  26.             class Row {
  27.                 private: Matrix *matrix;
  28.                          int i;
  29.                 public: Row(Matrix* matrix, int i){
  30.                             this->matrix = matrix;
  31.                             this->i = i;
  32.                         }
  33.                         int& Row::operator[](int j) {
  34.                             return (*matrix).array[i][j];
  35.                         }
  36.             };
  37.             Row operator [] (int i){
  38.                 return Row(this, i);
  39.             }
  40.  
  41.  
  42. };
  43. #endif MATRIX_DECLARATION_H
  44.  
  45.  
  46.  
  47. //implementation
  48. #include "cmake-build-debug/declaration.h"
  49. // Created by anna on 20.04.18.
  50. //
  51.  
  52. int Matrix::countColumns() {
  53.     return this->n;
  54. }
  55. int Matrix::countRows(){
  56.     return this->m;
  57. }
  58. Matrix::Matrix(int ** arr, int m, int n) {
  59.     this->m = m;
  60.     this->n = n;
  61.     this->array = new int*[m];
  62.     for(int i=0; i<m; i++)
  63.         this->array[i] = new int[n];
  64.     copy(arr, arr + n*m, this->array);
  65. }
  66. Matrix::~Matrix() {
  67.     for(int i=0;i<m; i++)
  68.         delete [] this->array[i];
  69.     delete this->array;
  70. }
  71. Matrix::Matrix(const Matrix &obj) {
  72.     m = obj.m;
  73.     n = obj.n;
  74.     array = new int*[m];
  75.     for(int i=0; i<m; i++)
  76.         array[i] = new int[n];
  77.     copy(obj.array, obj.array+n*m, array);
  78. }
  79. Matrix& Matrix::operator=(const Matrix &obj) {
  80.     swap(n, obj.n);
  81.     swap(m, obj.m);
  82.     swap(array, obj.array);
  83. }
  84. Matrix::addColumn(int *column) {
  85.     n++;
  86.     int ** array = new int*[m];
  87.     for(int i=0; i<m; i++)
  88.         array[i] = new int[n];
  89.     for(int i=0; i<m; i++)
  90.         for(int j=0; j<n; j++)
  91.             if(j == n-1) array[i][j] = column[i];
  92.             else array[i][j] = this->array[i][j];
  93.     swap(array, this->array);
  94.     for(int i=0;i<m; i++)
  95.         delete [] array[i];
  96.     delete array;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement