Compare commits

..

4 Commits

Author SHA1 Message Date
Alexander Zhirov 80614e6fbb minor fixes 2021-10-04 14:38:08 +03:00
Alexander Zhirov 56abebdcc8 lesson_4 2021-10-04 00:56:20 +03:00
Alexander Zhirov 87f1f3e689 added exercise 1 2021-10-03 20:45:06 +03:00
Alexander Zhirov 2807151f7e start 2021-09-30 21:17:00 +03:00
4 changed files with 129 additions and 0 deletions

61
lesson_4/exercises.cpp Normal file
View File

@ -0,0 +1,61 @@
/*
* exercise.cpp
*
* Created on: 3 окт. 2021 г.
* Author: alexander
*/
#include <vector>
#include <list>
#include <math.h>
#include <algorithm>
#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<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
cout << endl;
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
cout << endl;
}
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-ой вектор - печать целочисленного вектора
cout << endl;
for_each(v.begin(), v.end(), [&](const double &i)
{
error += pow(i - static_cast<int>(i), 2);
});
cout << endl << "Ошибка между цифровым и аналоговым сигналом равна " << error << endl;
}

11
lesson_4/exercises.hpp Normal file
View File

@ -0,0 +1,11 @@
/*
* exercise_2.hpp
*
* Created on: 2 окт. 2021 г.
* Author: alexander
*/
#pragma once
void exercise_1();
void exercise_2();

16
lesson_4/main.cpp Normal file
View File

@ -0,0 +1,16 @@
/*
* main.cpp
*
* Created on: 30 сент. 2021 г.
* Author: alexander
*/
#include "exercises.hpp"
int main()
{
exercise_1();
exercise_2();
return 0;
}

41
lesson_4/templates.hpp Normal file
View File

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