Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "scarse_matrix.h"
- #include <float.h>
- #include <iostream>
- #include <fstream>
- using namespace std;
- // to do :-)
- #pragma region CONSTRUCTORS
- scarse_matrix::scarse_matrix()
- {
- nRows = nCols = 0;
- }
- scarse_matrix::scarse_matrix(const scarse_matrix &m)
- {
- nRows = m.nRows;
- nCols = m.nCols;
- vector<Record>::const_iterator el;
- for (el = m.els.begin(); el != m.els.end(); el++)
- els.push_back(*el);
- }
- scarse_matrix::scarse_matrix(int rows, int cols)
- {
- nRows = rows;
- nCols = cols;
- }
- #pragma endregion
- #pragma region DECONSTRUCTOR
- scarse_matrix::~scarse_matrix()
- {
- els.clear();
- }
- #pragma endregion
- #pragma region PUBLIC_METHODS
- double scarse_matrix::Get(int row, int col)
- {
- CheckOutOfBounds(row, col);
- double val = 0;
- vector<Record>::iterator el;
- for (el = els.begin(); el != els.end(); el++)
- if (el->x == row && el->y == col)
- return el->value;
- return val;
- }
- void scarse_matrix::Set(int row, int col, double value)
- {
- CheckOutOfBounds(row, col);
- bool found = false;
- vector<Record>::iterator el;
- for (el = els.begin(); el != els.end(); el++)
- {
- if (el->x == row && el->y == col)
- {
- el->value = value;
- found = true;
- }
- }
- if (!found)
- {
- Record tmp;
- tmp.x = row;
- tmp.y = col;
- tmp.value = value;
- els.push_back(tmp);
- }
- }
- double scarse_matrix::GetMax()
- {
- double max = -DBL_MAX;
- // First option: Thanks to ObserveMAXRecord()
- if (elsMAX > max) return elsMAX;
- // Second option:
- /*vector<Record>::iterator el;
- for (el = els.begin(); el != els.end(); el++)
- if (el->value > max) max = el->value;
- */
- return max;
- }
- int scarse_matrix::wczytajDane(const char * szPlik)
- {
- int col, row;
- double value;
- int ileDanych = 0;
- ifstream p = ifstream(szPlik, ios::in);
- if (p)
- {
- while (!p.eof())
- {
- p >> row >> col >> value;
- if (row < nRows && row >= 0 &&
- col < nCols && col >= 0)
- {
- if (value != 0)
- {
- // Observe MAX
- ObserveMaxRecord(value);
- Record tmp;
- tmp.x = row;
- tmp.y = col;
- tmp.value = value;
- els.push_back(tmp);
- }
- ileDanych++;
- }
- }
- p.close();
- return ileDanych;
- }
- return 0;
- }
- #pragma endregion
- #pragma region PRIVATE_METHODS
- void scarse_matrix::CheckOutOfBounds(int col, int row)
- {
- if (row >= nRows || row < 0 ||
- col >= nCols || col < 0)
- throw 2;
- }
- void scarse_matrix::ObserveMaxRecord(double value)
- {
- if (value > elsMAX)
- elsMAX = value;
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement