diff --git a/lesson_4/exercises.cpp b/lesson_4/exercises.cpp new file mode 100644 index 0000000..eab21dd --- /dev/null +++ b/lesson_4/exercises.cpp @@ -0,0 +1,61 @@ +/* + * exercise.cpp + * + * Created on: 3 окт. 2021 г. + * Author: alexander + */ + +#include +#include +#include +#include + +#include "exercises.hpp" +#include "templates.hpp" + +using namespace std; + +void exercise_1() +{ + const unsigned int size = 10; + int newIntValue = -3; + double newDoubleValue = 2.2; + + vector v(size); + generator(v, -10, 10); + 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 + + cout << endl; + + list l(size); + generator(l, -10.0, 10.0); + 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 + + cout << endl; +} + +void exercise_2() +{ + vector v(100); + + double error = 0; + generator(v, -1000.0, 1000.0); + + print(v); // 1-ый вектор - печать исходного вектора + copy(v.begin(), v.end(), ostream_iterator { cout, " " }); // 2-ой вектор - печать целочисленного вектора + + cout << endl; + + for_each(v.begin(), v.end(), [&](const double &i) + { + error += pow(i - static_cast(i), 2); + }); + + cout << endl << "Ошибка между цифровым и аналоговым сигналом равна " << error << endl; +} diff --git a/lesson_4/exercises.hpp b/lesson_4/exercises.hpp new file mode 100644 index 0000000..6b619b3 --- /dev/null +++ b/lesson_4/exercises.hpp @@ -0,0 +1,11 @@ +/* + * exercise_2.hpp + * + * Created on: 2 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +void exercise_1(); +void exercise_2(); diff --git a/lesson_4/main.cpp b/lesson_4/main.cpp new file mode 100644 index 0000000..43bb716 --- /dev/null +++ b/lesson_4/main.cpp @@ -0,0 +1,16 @@ +/* + * main.cpp + * + * Created on: 30 сент. 2021 г. + * Author: alexander + */ + +#include "exercises.hpp" + +int main() +{ + exercise_1(); + exercise_2(); + + return 0; +} diff --git a/lesson_4/templates.hpp b/lesson_4/templates.hpp new file mode 100644 index 0000000..093273d --- /dev/null +++ b/lesson_4/templates.hpp @@ -0,0 +1,41 @@ +/* + * exercise_1.hpp + * + * Created on: 2 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +#include +#include +#include + +template class TContainer, typename TItem, typename TIterator, typename T> +void generator(TContainer &c, const T &rangeStart, const T &rangeEnd) +{ + std::random_device rd; + std::mt19937 mersenne(rd()); + std::uniform_real_distribution urd(rangeStart, rangeEnd); + 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(), std::ostream_iterator { std::cout, " " }); + std::cout << std::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); +}