Advertisement
ilyasizov

Untitled

Jun 19th, 2021
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. void PrintList(ostream& file, const list<int>& list) {
  9.     for (std::list<int>::const_iterator it = list.begin(); it != list.end(); it++)
  10.     {
  11.         file << *it;
  12.         if (it != --list.end()) {
  13.             file << endl;
  14.         }
  15.     }
  16. }
  17.  
  18. list<int> ListWithRandomNumbers(int N, int M) {
  19.     list<int> result(N);
  20.     generate(result.begin(), result.end(), [M]() {return rand() % (2 * M) - M; });
  21.     return result;
  22. }
  23.  
  24. fstream FileWithRandomNumbersByStdGenerate(string fileName, int N, int M) {
  25.     fstream file(fileName, ios::in | ios::out | ios::trunc);
  26.     PrintList(file, ListWithRandomNumbers(N, M));
  27.     file.flush();
  28.     return file;
  29. }
  30.  
  31. fstream FileWithRandomNumbersByCycle(string fileName, int N, int M) {
  32.     fstream file(fileName, ios::in | ios::out | ios::trunc);
  33.     for (int i = 0; i < N; i++) {
  34.         file << rand() % (2 * M) - M;
  35.         if (i != N - 1) {
  36.             file << endl;
  37.         }
  38.     }
  39.     file.flush();
  40.     return file;
  41. }
  42.  
  43.  
  44. list<int> ListFromFile(fstream& file) {
  45.     file.seekg(0, ios::beg);
  46.     list<int> result;
  47.     int temp;
  48.     while (file.peek() != EOF)
  49.     {
  50.         file >> temp;
  51.         result.push_back(temp);
  52.     }
  53.     file.close();
  54.     return result;
  55. }
  56.  
  57.  
  58. int FindMax(list<int>::const_iterator begin, list<int>::const_iterator end) {
  59.     list<int>::const_iterator it = begin;
  60.     int result = *it;
  61.     for (; it != end; it++) {
  62.         if (*it > result) {
  63.             result = *it;
  64.         }
  65.     }
  66.     return result;
  67. }
  68.  
  69.  
  70. int Max(const list<int>& list) {
  71.     return FindMax(list.begin(), list.end());
  72. }
  73.  
  74.  
  75. list<int> Modify(const list<int>& list) {
  76.     std::list<int>::const_iterator begin = list.begin();
  77.     std::list<int>::const_iterator end = list.end();
  78.     std::list<int> res(begin, end);
  79.     int max = Max(res);
  80.     std::list<int>::iterator it = res.begin();
  81.     for (; it != end; it++) {
  82.         *it -= max;
  83.     }
  84.     return res;
  85. }
  86.  
  87.  
  88. list<int> Modify(list<int>::const_iterator begin, list<int>::const_iterator end)
  89. {
  90.     std::list<int> res(begin, end);
  91.     int max = Max(res);
  92.     list<int>::iterator it = res.begin();
  93.     for (; it != end; it++) {
  94.         *it -= max;
  95.     }
  96.     return res;
  97. }
  98.  
  99. list<int> ModifyWithTransform(const list<int>& list) {
  100.     int max = Max(list);
  101.     std::list<int> res(list.size());
  102.     transform(list.begin(), list.end(), res.begin(), [max](int num) { return num - max; });
  103.     return res;
  104. }
  105.  
  106. list<int> ModifyWithForEach(const list<int>& list) {
  107.     int max = Max(list);
  108.     std::list<int> res(list.begin(), list.end());
  109.     for_each(res.begin(), res.end(), [max](int &num) { num -= max; });
  110.     return res;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement