Advertisement
m4ly

ScarseMatrix.cpp

Nov 13th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "scarse_matrix.h"
  3. #include <float.h>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. // to do :-)
  10.  
  11. #pragma region CONSTRUCTORS
  12.  
  13. scarse_matrix::scarse_matrix()
  14. {
  15.     nRows = nCols = 0;
  16. }
  17.  
  18. scarse_matrix::scarse_matrix(const scarse_matrix &m)
  19. {
  20.     nRows = m.nRows;
  21.     nCols = m.nCols;
  22.  
  23.     vector<Record>::const_iterator el;
  24.     for (el = m.els.begin(); el != m.els.end(); el++)
  25.         els.push_back(*el);
  26. }
  27.  
  28. scarse_matrix::scarse_matrix(int rows, int cols)
  29. {
  30.     nRows = rows;
  31.     nCols = cols;
  32. }
  33.  
  34. #pragma endregion
  35.  
  36. #pragma region DECONSTRUCTOR
  37.  
  38. scarse_matrix::~scarse_matrix()
  39. {
  40.     els.clear();
  41. }
  42. #pragma endregion
  43.  
  44. #pragma region PUBLIC_METHODS
  45.  
  46. double scarse_matrix::Get(int row, int col)
  47. {
  48.     CheckOutOfBounds(row, col);
  49.  
  50.     double val = 0;
  51.     vector<Record>::iterator el;
  52.  
  53.     for (el = els.begin(); el != els.end(); el++)
  54.     if (el->x == row && el->y == col)
  55.         return el->value;
  56.  
  57.     return val;
  58. }
  59.  
  60. void scarse_matrix::Set(int row, int col, double value)
  61. {
  62.     CheckOutOfBounds(row, col);
  63.  
  64.     bool found = false;
  65.     vector<Record>::iterator el;
  66.  
  67.     for (el = els.begin(); el != els.end(); el++)
  68.     {
  69.         if (el->x == row && el->y == col)
  70.         {
  71.             el->value = value;
  72.             found = true;
  73.         }
  74.     }
  75.  
  76.     if (!found)
  77.     {
  78.         Record tmp;
  79.         tmp.x = row;
  80.         tmp.y = col;
  81.         tmp.value = value;
  82.         els.push_back(tmp);
  83.     }
  84. }
  85.  
  86. double scarse_matrix::GetMax()
  87. {
  88.     double max = -DBL_MAX;
  89.  
  90.     // First option: Thanks to ObserveMAXRecord()
  91.     if (elsMAX > max) return elsMAX;
  92.  
  93.     // Second option:
  94.     /*vector<Record>::iterator el;
  95.     for (el = els.begin(); el != els.end(); el++)
  96.     if (el->value > max) max = el->value;
  97.     */
  98.     return max;
  99. }
  100.  
  101. int scarse_matrix::wczytajDane(const char * szPlik)
  102. {
  103.     int col, row;
  104.     double value;
  105.     int ileDanych = 0;
  106.     ifstream p = ifstream(szPlik, ios::in);
  107.     if (p)
  108.     {
  109.         while (!p.eof())
  110.         {
  111.             p >> row >> col >> value;
  112.             if (row < nRows && row >= 0 &&
  113.                 col < nCols && col >= 0)
  114.             {
  115.                 if (value != 0)
  116.                 {
  117.                     // Observe MAX
  118.                     ObserveMaxRecord(value);
  119.  
  120.                     Record tmp;
  121.                     tmp.x = row;
  122.                     tmp.y = col;
  123.                     tmp.value = value;
  124.  
  125.                     els.push_back(tmp);
  126.                 }
  127.                 ileDanych++;
  128.             }
  129.         }
  130.         p.close();
  131.         return ileDanych;
  132.  
  133.     }
  134.     return 0;
  135. }
  136.  
  137. #pragma endregion
  138.  
  139. #pragma region PRIVATE_METHODS
  140.  
  141. void scarse_matrix::CheckOutOfBounds(int col, int row)
  142. {
  143.     if (row >= nRows || row < 0 ||
  144.         col >= nCols || col < 0)
  145.         throw 2;
  146. }
  147.  
  148. void scarse_matrix::ObserveMaxRecord(double value)
  149. {
  150.     if (value > elsMAX)
  151.         elsMAX = value;
  152. }
  153.  
  154. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement