diff --git a/lesson_4/exercise_1.cpp b/lesson_4/exercise_1.cpp new file mode 100644 index 0000000..12ac4e7 --- /dev/null +++ b/lesson_4/exercise_1.cpp @@ -0,0 +1,79 @@ +/* + * exercise_1.cpp + * + * Created on: 2 окт. 2021 г. + * Author: alexander + */ + +#include +#include +#include +#include +#include +#include + +#include "exercise_1.hpp" + +using namespace std; + +template class TContainer, typename TItem, typename TIterator> +void generatorInt(TContainer &c, const unsigned int &i) +{ + random_device rd; + mt19937 mersenne(rd()); + uniform_int_distribution uid(-static_cast(i), i); + generate(c.begin(), c.end(), [&]() + { + return uid(mersenne); + }); +} + +template class TContainer, typename TItem, typename TIterator> +void generatorFloat(TContainer &c, const unsigned int &f) +{ + random_device rd; + mt19937 mersenne(rd()); + uniform_real_distribution urd(-static_cast(f), static_cast(f)); + generate(c.begin(), c.end(), [&]() + { + return urd(mersenne); + }); +} + +template class TContainer, typename TItem, typename TIterator> +void print(const TContainer &c) +{ + copy(c.begin(), c.end(), ostream_iterator { cout, " " }); + cout << endl; +} + +template class TContainer, typename TItem, typename TIterator> +void insert_sorted(TContainer &c, const TItem &i) +{ + typename TContainer::iterator it = find_if(c.begin(), c.end(), [&](const TItem ¤t) + { + return current >= i; + }); + c.insert(it, i); +} + +void exercise_1() +{ + const unsigned int size = 10; + int newIntValue = -3; + double newDoubleValue = 2.2; + + vector v(size); + generatorInt(v, size); + sort(v.begin(), v.end()); + print(v); // -9 -9 -4 3 3 4 6 8 9 10 + insert_sorted(v, newIntValue); + print(v); // -9 -9 -4 -3 3 3 4 6 8 9 10 + + list l(size); + generatorFloat(l, size); + l.sort(); + print(l); // -9.31992 -6.76971 -5.98545 -2.1297 -1.9722 -0.292701 0.715274 3.43089 4.35249 9.45377 + insert_sorted(l, newDoubleValue); + print(l); // -9.31992 -6.76971 -5.98545 -2.1297 -1.9722 -0.292701 0.715274 2.2 3.43089 4.35249 9.45377 +} diff --git a/lesson_4/exercise_1.hpp b/lesson_4/exercise_1.hpp new file mode 100644 index 0000000..bde2d44 --- /dev/null +++ b/lesson_4/exercise_1.hpp @@ -0,0 +1,22 @@ +/* + * exercise_1.hpp + * + * Created on: 2 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +template class TContainer, typename TItem, typename TIterator> +void generatorInt(TContainer &c, const unsigned int &i); + +template class TContainer, typename TItem, typename TIterator> +void generatorFloat(TContainer &c, const unsigned int &i); + +template class TContainer, typename TItem, typename TIterator> +void print(const TContainer &c); + +template class TContainer, typename TItem, typename TIterator> +void insert_sorted(TContainer &c, const TItem &i); + +void exercise_1(); diff --git a/lesson_4/exercise_2.cpp b/lesson_4/exercise_2.cpp new file mode 100644 index 0000000..d62614f --- /dev/null +++ b/lesson_4/exercise_2.cpp @@ -0,0 +1,10 @@ +/* + * exercise_2.cpp + * + * Created on: 2 окт. 2021 г. + * Author: alexander + */ + +#include "exercise_2.hpp" + + diff --git a/lesson_4/exercise_2.hpp b/lesson_4/exercise_2.hpp new file mode 100644 index 0000000..b023320 --- /dev/null +++ b/lesson_4/exercise_2.hpp @@ -0,0 +1,8 @@ +/* + * exercise_2.hpp + * + * Created on: 2 окт. 2021 г. + * Author: alexander + */ + +#pragma once diff --git a/lesson_4/main.cpp b/lesson_4/main.cpp index a4a0cf0..f0377c4 100644 --- a/lesson_4/main.cpp +++ b/lesson_4/main.cpp @@ -5,6 +5,12 @@ * Author: alexander */ +#include "exercise_1.hpp" +#include "exercise_2.hpp" +int main() +{ + exercise_1(); - + return 0; +}