From b7f94d1a14bbebb76c110d3047a6e38dd410a4b5 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 24 Jun 2020 18:15:19 +0200 Subject: chore: add exercise --- I_anno/Programmazione_2/exercises/matrice-adj.cc | 151 +++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 I_anno/Programmazione_2/exercises/matrice-adj.cc (limited to 'I_anno/Programmazione_2/exercises') diff --git a/I_anno/Programmazione_2/exercises/matrice-adj.cc b/I_anno/Programmazione_2/exercises/matrice-adj.cc new file mode 100644 index 0000000..7830f97 --- /dev/null +++ b/I_anno/Programmazione_2/exercises/matrice-adj.cc @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +using namespace std; + +template +class matrix_graph { +public: + ~matrix_graph() { + delete[] _m; + delete[] _k; + } + matrix_graph(int len) : _len{len}, _nodes{0}, _edges{0} { + _m = new int*[len]; + _k = new H*[len]; + for(int i = 0; i < len; ++i) { + _m[i] = new int[len]; + _k[i] = nullptr; + for(int j = 0; j < len; ++j) + _m[i][j] = 0; + } + } + + matrix_graph* add_node(H k) { + if(_len == _nodes) return this; // no more space for new nodes + if(_index(k) > -1) return this; // nodes already exists + + _k[_nodes++] = new H(k); + + return this; + } + + matrix_graph* add_edge(H x, H y) { + int i = _index(x); + int j = _index(y); + if(i < 0 || j < 0) return this; + if(!_m[i][j]) { + _m[i][j] = 1; + _edges++; + } + return this; + } + vector> print() { + vector> v; + for(int i = 0; i < _len; ++i) { + v.push_back(vector{*_k[i]}); + vector temp; + for(int j = 0; j < _len; ++j) { + if(_m[i][j]) { + temp.push_back(*_k[j]); + } + } + sort(begin(temp), end(temp)); + for(auto const& j : temp) + v[i].push_back(j); + } + + sort(begin(v), end(v)); + return v; + } +private: + int _len, _nodes, _edges; + int** _m; + H** _k; + int _index(H x) { + for(int i = 0; i < _nodes; ++i) + if(*_k[i] == x) return i; + return -1; + } +}; + +template +string result(vector> v) { + string s{""}; + ostringstream ss; + for(auto const& i : v) { + s += "("; + for(auto const& j : i) { + ss << j; + s+= ss.str(); + s+= ' '; + ss.str(""); ss.clear(); + } + s.pop_back(); + s += ") "; + } + return s; +} + +int main() { + ifstream in("input.txt"); + ofstream out("output.txt"); + for(int ts = 0; ts < 100; ++ts) { + int n, m; + in >> n >> m; + string t; + in >> t; + if(t == "int") { + matrix_graph* g = new matrix_graph(n); + int x,y; + for(int i = 0; i < n; ++i) { + in >> x; + g->add_node(x); + } + char ex; + for(int i = 0; i < m; ++i) { + in >> ex >> x >> y >> ex; + g->add_edge(x, y); + } + auto v = g->print(); + out << result(v) << endl; + delete g; + } else if(t == "double") { + matrix_graph* g = new matrix_graph(n); + double x,y; + for(int i = 0; i < n; ++i) { + in >> x; + g->add_node(x); + } + char ex; + for(int i = 0; i < m; ++i) { + in >> ex >> x >> y >> ex; + g->add_edge(x, y); + } + auto v = g->print(); + out << result(v) << endl; + delete g; + } else { + matrix_graph* g = new matrix_graph(n); + char x,y; + for(int i = 0; i < n; ++i) { + in >> x; + g->add_node(x); + } + char ex; + for(int i = 0; i < m; ++i) { + in >> ex >> x >> y >> ex; + g->add_edge(x, y); + } + + auto v = g->print(); + out << result(v) << endl; + delete g; + } + } + in.close(); + out.close(); + return 0; +} -- cgit v1.2.3-18-g5258