added exercise 1

This commit is contained in:
Alexander Zhirov 2021-10-03 20:45:06 +03:00
parent 2807151f7e
commit 87f1f3e689
5 changed files with 126 additions and 1 deletions

79
lesson_4/exercise_1.cpp Normal file
View File

@ -0,0 +1,79 @@
/*
* exercise_1.cpp
*
* Created on: 2 окт. 2021 г.
* Author: alexander
*/
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
#include <list>
#include "exercise_1.hpp"
using namespace std;
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void generatorInt(TContainer<TItem, TIterator> &c, const unsigned int &i)
{
random_device rd;
mt19937 mersenne(rd());
uniform_int_distribution<TItem> uid(-static_cast<TItem>(i), i);
generate(c.begin(), c.end(), [&]()
{
return uid(mersenne);
});
}
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void generatorFloat(TContainer<TItem, TIterator> &c, const unsigned int &f)
{
random_device rd;
mt19937 mersenne(rd());
uniform_real_distribution<TItem> urd(-static_cast<TItem>(f), static_cast<TItem>(f));
generate(c.begin(), c.end(), [&]()
{
return urd(mersenne);
});
}
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void print(const TContainer<TItem, TIterator> &c)
{
copy(c.begin(), c.end(), ostream_iterator<TItem> { cout, " " });
cout << endl;
}
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void insert_sorted(TContainer<TItem, TIterator> &c, const TItem &i)
{
typename TContainer<TItem, TIterator>::iterator it = find_if(c.begin(), c.end(), [&](const TItem &current)
{
return current >= i;
});
c.insert(it, i);
}
void exercise_1()
{
const unsigned int size = 10;
int newIntValue = -3;
double newDoubleValue = 2.2;
vector<int> 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<double> 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
}

22
lesson_4/exercise_1.hpp Normal file
View File

@ -0,0 +1,22 @@
/*
* exercise_1.hpp
*
* Created on: 2 окт. 2021 г.
* Author: alexander
*/
#pragma once
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void generatorInt(TContainer<TItem, TIterator> &c, const unsigned int &i);
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void generatorFloat(TContainer<TItem, TIterator> &c, const unsigned int &i);
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void print(const TContainer<TItem, TIterator> &c);
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
void insert_sorted(TContainer<TItem, TIterator> &c, const TItem &i);
void exercise_1();

10
lesson_4/exercise_2.cpp Normal file
View File

@ -0,0 +1,10 @@
/*
* exercise_2.cpp
*
* Created on: 2 окт. 2021 г.
* Author: alexander
*/
#include "exercise_2.hpp"

8
lesson_4/exercise_2.hpp Normal file
View File

@ -0,0 +1,8 @@
/*
* exercise_2.hpp
*
* Created on: 2 окт. 2021 г.
* Author: alexander
*/
#pragma once

View File

@ -5,6 +5,12 @@
* Author: alexander * Author: alexander
*/ */
#include "exercise_1.hpp"
#include "exercise_2.hpp"
int main()
{
exercise_1();
return 0;
}