consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
#include "ConstructionMaterial.h"
|
||||
|
||||
ConstructionMaterial::ConstructionMaterial(std::string item, unsigned int amount): item(item), amount(amount) {}
|
||||
|
||||
std::string ConstructionMaterial::toString() {
|
||||
return "Item: " + item + "\nAmount: " + std::to_string(amount) + "\n";
|
||||
}
|
17
semester_2/programiranje_2/naloga0801/ConstructionMaterial.h
Normal file
17
semester_2/programiranje_2/naloga0801/ConstructionMaterial.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef NALOGA0801_CONSTRUCTIONMATERIAL_H
|
||||
#define NALOGA0801_CONSTRUCTIONMATERIAL_H
|
||||
#include "string"
|
||||
|
||||
class ConstructionMaterial {
|
||||
private:
|
||||
std::string item;
|
||||
unsigned int amount;
|
||||
|
||||
public:
|
||||
ConstructionMaterial(std::string item, unsigned int amount);
|
||||
|
||||
std::string toString();
|
||||
};
|
||||
|
||||
|
||||
#endif //NALOGA0801_CONSTRUCTIONMATERIAL_H
|
60
semester_2/programiranje_2/naloga0801/Element.h
Normal file
60
semester_2/programiranje_2/naloga0801/Element.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef NALOGA0801_ELEMENT_H
|
||||
#define NALOGA0801_ELEMENT_H
|
||||
|
||||
template <typename T>
|
||||
class Element{
|
||||
private:
|
||||
unsigned int x,y;
|
||||
T value;
|
||||
public:
|
||||
Element(unsigned int x, unsigned int y, T value);
|
||||
|
||||
unsigned int getX() const;
|
||||
|
||||
unsigned int getY() const;
|
||||
|
||||
T getValue() const;
|
||||
|
||||
void setX(unsigned int x);
|
||||
|
||||
void setY(unsigned int y);
|
||||
|
||||
void setValue(T value);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Element<T>::Element(unsigned int x, unsigned int y, T value) :
|
||||
x(x), y(y), value(value) {}
|
||||
|
||||
template<typename T>
|
||||
unsigned int Element<T>::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int Element<T>::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T Element<T>::getValue() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Element<T>::setX(unsigned int x) {
|
||||
Element::x = x;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Element<T>::setY(unsigned int y) {
|
||||
Element::y = y;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Element<T>::setValue(T value) {
|
||||
Element::value = value;
|
||||
}
|
||||
|
||||
|
||||
#endif //NALOGA0801_ELEMENT_H
|
57
semester_2/programiranje_2/naloga0801/README.md
Normal file
57
semester_2/programiranje_2/naloga0801/README.md
Normal file
@@ -0,0 +1,57 @@
|
||||
Pri tej nalogi boste narediti šablono razreda, in sicer za redke matrike(https://en.wikipedia.org/wiki/Sparse_matrix) (SparseMatrix2D) za poljuben tip. Gre za matriko, ki ima večino elementov enakih 0 in malo elementov, ki so različni od 0. Te matrike so lahko kar velike in posledično predstavljajo problem za shranjevanje, saj nas večina pozicij ne zanima, ker imajo splošno vrednost 0. To težavo lahko rešimo na način, da beležimo le vrednosti, ki so različne od 0, in njihovo lokacijo (x in y koordinato). To bomo rešili z dodatno šablono razreda Element.
|
||||
|
||||
Implementacijo šablono razredov napišite vse v posamezno datoteko .h (brez .cpp).
|
||||
|
||||
Napišite šablono razreda Element, ki naj ima:
|
||||
instančne spremenljivke:
|
||||
x in y (tipa unsigned int)
|
||||
value, ki je lahko poljubnega tipa
|
||||
konstruktor s 3 paremetri,
|
||||
set in get metode.
|
||||
Napišite šablono razreda SparseMatrix2D, ki naj ima:
|
||||
instančne spremenljivke:
|
||||
elements (tipa std::vector<Element<T>>),
|
||||
sizeX in sizeY, ki predstavljata velikost matrike (tipa unsigned int)
|
||||
defaultElement, ki predstavlja privzeto vrednost matrike in je poljubljena tipa.
|
||||
konstruktor s 3 parametri (sizeX,sizeY, defaultElement),
|
||||
metodo set(unsgined int x, unsigned int y, T value), ki doda element v primeru, da na teh koordinatah element še ne obstaja. V primeru, da že obstaja pa obstoječ element posodobi z novo vrednostjo.
|
||||
metodo at(unsigned int x, unsgined int y), ki vrne element na podanih koordinatah. V primeru, da na tisti lokaciji ni elementa, vrnemo privzeto vrednost matrike.
|
||||
metodo getSizeX in getSizeY, ki vračata posamezno dimenzijo matrike
|
||||
V programu main ustvarite vsaj 2 redki matriki (ena je lahko splošnega tipa in ena, kjer je tip objekt - ni dovoljena uporaba razreda Point) in ju izrišite kot je prikazano v spodnjem primeru.
|
||||
|
||||
int main() {
|
||||
// example for Point
|
||||
Point p(9, 9);
|
||||
Element e(1, 1, p);
|
||||
cout << e.getValue().toString() << endl;
|
||||
SparseMatrix2D bigPointMatrix(10, 10, Point(0, 0));
|
||||
bigPointMatrix.set(5, 5, p);
|
||||
|
||||
// example for int
|
||||
SparseMatrix2D bigIntMatrix(10, 10, 0);
|
||||
bigIntMatrix.set(5, 5, 8);
|
||||
bigIntMatrix.set(2, 3, 7);
|
||||
bigIntMatrix.set(2, 2, 4);
|
||||
|
||||
for (int i = 0; i < bigIntMatrix.getSizeX(); i++) {
|
||||
for (int j = 0; j < bigIntMatrix.getSizeY(); j++) {
|
||||
cout << bigIntMatrix.at(i,j) << (j != bigIntMatrix.getSizeY() - 1 ? " ; " : "");
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Primer izpisa:
|
||||
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 4 ; 7 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 8 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
||||
0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0
|
71
semester_2/programiranje_2/naloga0801/SparseMatrix2D.h
Normal file
71
semester_2/programiranje_2/naloga0801/SparseMatrix2D.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef NALOGA0801_SPARSEMATRIX2D_H
|
||||
#define NALOGA0801_SPARSEMATRIX2D_H
|
||||
|
||||
#include <vector>
|
||||
#include "Element.h"
|
||||
|
||||
template<typename T>
|
||||
class SparseMatrix2D {
|
||||
private:
|
||||
std::vector<Element<T>> elements;
|
||||
unsigned int sizeX, sizeY;
|
||||
T defaultElement;
|
||||
public:
|
||||
SparseMatrix2D(unsigned int sizeX, unsigned int sizeY, T defaultElement);
|
||||
|
||||
void set(unsigned int x, unsigned int y, T value);
|
||||
|
||||
T at(unsigned int x, unsigned int y);
|
||||
|
||||
unsigned int getSizeX();
|
||||
|
||||
unsigned int getSizeY();
|
||||
|
||||
bool compareSize(SparseMatrix2D comp);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
SparseMatrix2D<T>::SparseMatrix2D(unsigned int sizeX, unsigned int sizeY, T defaultElement) :
|
||||
sizeX(sizeX), sizeY(sizeY), defaultElement(defaultElement) {}
|
||||
|
||||
template<typename T>
|
||||
void SparseMatrix2D<T>::set(unsigned int x, unsigned int y, T value) {
|
||||
bool elementExiest = false;
|
||||
for (int i = 0; i < elements.size(); ++i) {
|
||||
if (elements[i].getX() == x and elements[i].getY() == y) {
|
||||
elements[i].setValue(value);
|
||||
elementExiest = true;
|
||||
}
|
||||
}
|
||||
if (!elementExiest) {
|
||||
Element<T> tmp(x, y, value);
|
||||
elements.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T SparseMatrix2D<T>::at(unsigned int x, unsigned int y) {
|
||||
for (int i = 0; i < elements.size(); ++i) {
|
||||
if (elements[i].getX() == x and elements[i].getY() == y) {
|
||||
return elements[i].getValue();
|
||||
}
|
||||
}
|
||||
return defaultElement;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int SparseMatrix2D<T>::getSizeX() {
|
||||
return sizeX;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int SparseMatrix2D<T>::getSizeY() {
|
||||
return sizeY;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SparseMatrix2D<T>::compareSize(SparseMatrix2D comp) {
|
||||
return sizeX == comp.getSizeX() and sizeY == comp.getSizeY();
|
||||
}
|
||||
|
||||
#endif //NALOGA0801_SPARSEMATRIX2D_H
|
35
semester_2/programiranje_2/naloga0801/main.cpp
Normal file
35
semester_2/programiranje_2/naloga0801/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include "SparseMatrix2D.h"
|
||||
#include "ConstructionMaterial.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
ConstructionMaterial a("nothing",0);
|
||||
ConstructionMaterial b("Lumber", 6);
|
||||
ConstructionMaterial c("nails", 9);
|
||||
SparseMatrix2D<ConstructionMaterial> bigPointMatrix(10, 10, a);
|
||||
bigPointMatrix.set(5, 5, b);
|
||||
cout << bigPointMatrix.at(5,5).toString() << endl;
|
||||
bigPointMatrix.set(5, 5, c);
|
||||
cout << bigPointMatrix.at(5,5).toString() << endl;
|
||||
|
||||
|
||||
SparseMatrix2D<int> bigIntMatrix(10, 10, 0);
|
||||
bigIntMatrix.set(5, 5, 8);
|
||||
bigIntMatrix.set(2, 3, 7);
|
||||
bigIntMatrix.set(2, 2, 4);
|
||||
|
||||
for (int i = 0; i < bigIntMatrix.getSizeX(); i++) {
|
||||
for (int j = 0; j < bigIntMatrix.getSizeY(); j++) {
|
||||
cout << bigIntMatrix.at(i,j) << (j != bigIntMatrix.getSizeY() - 1 ? " : " : "");
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
SparseMatrix2D<int> smallInMatrix(10,2,0);
|
||||
cout << endl << (bigIntMatrix.compareSize(smallInMatrix) ? "True" : "False");
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user