1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#pragma once
#include <list>
#include <random>
#include <ctime>
#include <numeric>
#include <functional>
#include <iterator>
template<typename T>
class ListManipulator {
private:
std::list<T> *theList;
public:
ListManipulator() { theList = new std::list<T>[20]; }
ListManipulator(std::list<T> *aList);
~ListManipulator() {}
void fillList();
T sumList() const;
T listAverage() const;
bool findFirst1500_1900(T &num) const;
void divideByTwo();
void swapPlaces();
void findMinMax(T &min, T &max) const;
void sortList();
void clearList();
std::list<T> getList() const;
void saveToFile() const;
void loadFromFile();
T random1();
};
template<typename T> ListManipulator<T>::ListManipulator(std::list<T> *aList) {
theList(aList);
}
template<typename T>
T ListManipulator<T>::random1()
{
std::uniform_real_distribution<double> random(1000, 2000);
std::default_random_engine generator(static_cast<unsigned>(std::time(0)));
return random(generator);
}
// find the first occuring number with value between 1500-1900
template<typename T>
bool ListManipulator<T> ::findFirst1500_1900(T &num) const
{
auto it = find_if(theList->begin(), theList->end(), [num](T a) {
return (a >= 1500 && a <= 1900); });
return true;
return false;
}
// fill the list with 20 random numbers
template<typename T>
void ListManipulator<T>::fillList()
{
std::uniform_real_distribution<double> random(1000, 2000);
std::default_random_engine generator(static_cast<unsigned>(std::time(0)));
auto myGenerator = std::bind(random, generator);
//std::generate(theList->begin(), theList->end(), myGenerator);
std::generate(theList->begin(), theList->end(), myGenerator);
/*default_random_engine randomGenerator(random_device{}());
my_distribution<T> distribution(1000, 2000);
auto myGenerator = bind(distribution, randomGenerator);
generate(theList.begin(), theList.end(), myGenerator);*/
}
template<typename T>
T ListManipulator<T>::sumList() const
{
auto first = theList->begin();
auto last = theList->end();
T total=std::accumulate(first, last, T(0), [](T all, T each) { return all + each; });
return total;
}
template<typename T>
T ListManipulator<T>::listAverage() const
{
auto first = theList->begin();
auto last = theList->end();
return std::accumulate(first, last, T(0)) / theList->size();
}
|