Advertisement
AlinaL

Untitled

Oct 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "MatryceRzadkie.h"
  3.  
  4.  
  5. class CSparseCell;
  6.  
  7. class CSparseMatrix {
  8. private:
  9.     std::string name;
  10.     int numberOfDimension; //liczba wymiarów
  11.     std::vector <int> dimensionRanges; //zakresy wymiarów
  12.     int defaultValue; //wartość domyślna
  13.     std::vector <CSparseCell **> sparseCells; //komórki rzadkie
  14. public:
  15.     CSparseMatrix() {
  16.         name = "def_name";
  17.         numberOfDimension = 0;
  18.         defaultValue = 0;
  19.         std::cout << "create: " << name;
  20.     }
  21.  
  22.     CSparseMatrix(std::string matrixName) {
  23.         name = matrixName;
  24.         numberOfDimension = 0;
  25.         defaultValue = 0;
  26.         std::cout << "create: " << name << std::endl;
  27.     }
  28.  
  29.     CSparseMatrix(CSparseMatrix &pOther) {
  30.         name = pOther.name + "_copy";
  31.         numberOfDimension = pOther.numberOfDimension;
  32.         dimensionRanges = pOther.dimensionRanges;
  33.         defaultValue = pOther.defaultValue;
  34.         sparseCells = pOther.sparseCells;
  35.         std::cout << "create: " << name << std::endl;
  36.     }
  37.  
  38.     ~CSparseMatrix() {
  39.         std::cout << "destroy: " << name << std::endl;
  40.     }
  41.  
  42.     void setNumberOfDimension(int number) {
  43.         numberOfDimension = number;
  44.     }
  45.     void addDimensionRange(int range) {
  46.         dimensionRanges.push_back(range); //Dodaje nowy element na końcu kontenera vector.
  47.     }
  48.     void setDefaultValue(int value) {
  49.         defaultValue = value;
  50.     }
  51.     void addSparseCells(CSparseCell **cell) {
  52.         sparseCells.push_back(cell);
  53.     }
  54.     void setName(std::string matrixName) {
  55.         name = matrixName;
  56.     }
  57.     std::string getName() {
  58.         return name;
  59.     }
  60.     int getRange(int index) {
  61.         return dimensionRanges[index];
  62.     }
  63.     int getNumberOfDimension() {
  64.         return numberOfDimension;
  65.     }
  66. };
  67.  
  68. class CSparseCell {
  69. private:
  70.     friend class CSparseMatrix; //CSparseMatrix ma dostęp do części prywatnej CSparseCell
  71.     int sparseValue;
  72.     std::vector <int> cellCoordinates; //współrzędne komórki
  73. public:
  74.     void setSparseValue(int value) {
  75.         sparseValue = value;
  76.     }
  77.     void addCellCoordinate(int coordinate) {
  78.         cellCoordinates.push_back(coordinate); //Dodaje nowy element na końcu kontenera vector.
  79.     }
  80.  
  81. };
  82.  
  83. class Manager {
  84. private:
  85.     std::vector <CSparseMatrix *> vectorOfMatrices;
  86.     std::vector <int> ranges;
  87. public:
  88.  
  89.     void addRange(int range) {
  90.         ranges.push_back(range);
  91.     }
  92.  
  93.     void addMatrix(int numberOfDimension, std::string matrixName, int def) {
  94.         CSparseMatrix *pMatrix = new CSparseMatrix(matrixName);
  95.         vectorOfMatrices.push_back(pMatrix);
  96.         pMatrix->setNumberOfDimension(numberOfDimension);
  97.         int bound = ranges.size();
  98.         for (int i = 0; i < bound; i++) {
  99.             pMatrix->addDimensionRange(ranges[i]);
  100.         }
  101.         pMatrix->setDefaultValue(def);
  102.         ranges.clear();
  103.     }
  104.  
  105.     void list() {
  106.         std::cout << vectorOfMatrices.size() << " matrices: " << std::endl;
  107.         int bound = vectorOfMatrices.size();
  108.         for (int i = 0; i < bound; i++) {
  109.             std::string name = (*vectorOfMatrices[i]).getName();
  110.             std::cout << "[" << i << "] - \"" << name << "\" size: [";
  111.             for (int j = 0; j < (*vectorOfMatrices[i]).getNumberOfDimension(); j++) {
  112.                 std::cout << (*vectorOfMatrices[i]).getRange(j) << " ";
  113.             }
  114.             std::cout << "]" << std::endl;
  115.         }
  116.     }
  117.  
  118. };
  119.  
  120. int main()
  121. {
  122.     /*std::vector <CSparseMatrix **> vectorOfMatrices;
  123.     CSparseMatrix *pMatrix = new CSparseMatrix();
  124.     vectorOfMatrices.push_back(&pMatrix);*/
  125.  
  126.     Manager m;
  127.     m.addRange(5);
  128.     m.addRange(10);
  129.     m.addRange(15);
  130.     m.addMatrix(3, "test", 1);
  131.  
  132.     m.addRange(100);
  133.     m.addRange(10);
  134.     m.addRange(15);
  135.     m.addRange(10);
  136.     m.addRange(20);
  137.     m.addMatrix(5, "test2", 1);
  138.    
  139.     m.list();
  140.  
  141.  
  142.  
  143.     _getch();
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement