This commit is contained in:
Alexander Zhirov 2021-10-04 00:56:20 +03:00
parent 87f1f3e689
commit 56abebdcc8
7 changed files with 102 additions and 114 deletions

View File

@ -1,79 +0,0 @@
/*
* 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
}

View File

@ -1,22 +0,0 @@
/*
* 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();

View File

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

48
lesson_4/exercises.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
* exercise.cpp
*
* Created on: 3 окт. 2021 г.
* Author: alexander
*/
#include "exercises.hpp"
#include <math.h>
void exercise_1()
{
const unsigned int size = 10;
int newIntValue = -3;
double newDoubleValue = 2.2;
vector<int> 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
list<double> 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
}
void exercise_2()
{
vector<double> v(100);
double error = 0;
generator(v, -1000.0, 1000.0);
print(v); // 1-ый вектор - печать исходного вектора
copy(v.begin(), v.end(), ostream_iterator<int> { cout, " " }); // 2-ой вектор - печать целочисленного вектора
for_each(v.begin(), v.end(), [&](const double &i)
{
error += pow(i - static_cast<int>(i), 2);
});
cout << endl << error << endl;
}

View File

@ -6,3 +6,8 @@
*/ */
#pragma once #pragma once
#include "templates.hpp"
void exercise_1();
void exercise_2();

View File

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

46
lesson_4/templates.hpp Normal file
View File

@ -0,0 +1,46 @@
/*
* exercise_1.hpp
*
* Created on: 2 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;
template<template<typename, typename> class TContainer, typename TItem, typename TIterator, typename T>
void generator(TContainer<TItem, TIterator> &c, const T &rangeStart, const T &rangeEnd)
{
random_device rd;
mt19937 mersenne(rd());
uniform_real_distribution<double> urd(rangeStart, rangeEnd);
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);
}