From 300d1ae955278a01911740c5bbb48946c80ce3af Mon Sep 17 00:00:00 2001 From: Leon Wilzer Date: Fri, 10 Mar 2023 18:38:13 +0100 Subject: [PATCH] sheet09+prob --- 1 | 5 ----- Makefile | 5 +++++ src/HTable.cpp | 37 +++++++++++++++++++++++++------------ src/probability.cpp | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 17 deletions(-) delete mode 100644 1 create mode 100644 src/probability.cpp diff --git a/1 b/1 deleted file mode 100644 index 2eb6352..0000000 --- a/1 +++ /dev/null @@ -1,5 +0,0 @@ -00000 -01110 -00000 -00000 - \ No newline at end of file diff --git a/Makefile b/Makefile index a483142..897d1a4 100755 --- a/Makefile +++ b/Makefile @@ -65,3 +65,8 @@ pizza: $(CC) src/pizza.cpp -o build/pizza chmod +x build/pizza build/pizza + +probability: + $(CC) src/probability.cpp -o build/probability + chmod +x build/probability + build/probability \ No newline at end of file diff --git a/src/HTable.cpp b/src/HTable.cpp index 411dc73..c98078e 100644 --- a/src/HTable.cpp +++ b/src/HTable.cpp @@ -69,17 +69,23 @@ class HTable friend std::ostream& operator<<(std::ostream &os, const HTable &h) { os << '{'; - for(auto &pair : h.data) + for(auto pair = h.data.cbegin(); pair != h.data.cend(); ++pair) { os << "(\""; - os << pair.first; + os << pair[0].first; os << "\": "; - os << pair.second; - if(std::distance(pair, *h.data.end())==1) - { os << ")}"; } + os << pair[0].second; + + if(pair == h.data.cend()-1) + { + os << ")"; + } else - { os << "), "; } + { + os << "), "; + } } + os << '}'; return os; } @@ -100,28 +106,35 @@ class HTable position = 0; } } - throw std::runtime_error("Could not find key!"); - } + } + + void clear() + { + for(size_t i=0; i(); + } + } }; int main() { HTable t1(5); - std::cout << t1 << '\n'; for(size_t i=0; i<5;++i) { t1.insert(std::to_string(i), i); } - std::cout << t1 << '\n'; t1.insert("1", 10); - std::cout << t1 << '\n'; - t1.erase("1"); + t1.erase("4"); + std::cout << t1 << '\n'; + t1.clear(); std::cout << t1 << '\n'; } \ No newline at end of file diff --git a/src/probability.cpp b/src/probability.cpp new file mode 100644 index 0000000..d5b55a7 --- /dev/null +++ b/src/probability.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +constexpr size_t TRIALS_AMOUNT = 1000000; + +int main() +{ + size_t monday_heads = 0; + size_t monday_tails = 0; + size_t tuesday_tails = 0; + std::srand(std::time(nullptr)); + for(std::size_t i=0; i