Vla_DOS

Untitled

Jun 14th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4. #include <chrono>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <list>
  10. #include <iterator>
  11.  
  12. using namespace std;
  13.  
  14. void ShowList(list<int>& l) {
  15.     for (auto i : l)
  16.     {
  17.         cout << i << " ";
  18.     }
  19. }
  20. int Sum(list<int>& l) {
  21.     int sum = 0;
  22.     for (auto i : l)
  23.         sum += i;
  24.     return sum;
  25. }
  26. void InsertSum(list<int>& l) {
  27.     auto iter = l.begin();
  28.     for (int i = 1; i <= 3; i++) {
  29.         ++iter;
  30.     }
  31.     l.insert(iter, Sum(l));
  32. }
  33.  
  34. void PushBack(list<int>& l, int size, bool isEven) {
  35.     int r = 0;
  36.     for (int i = 0; i < size; i++)
  37.     {
  38.         r = rand() % 27;
  39.         if (isEven == true) {
  40.             if (r % 2 == 0) {
  41.                 l.push_back(r);
  42.             }
  43.         }
  44.         else
  45.             l.push_back(r);
  46.     }
  47. }
  48. int main() {
  49.     setlocale(LC_CTYPE, "");
  50.     int size;
  51.     cout << "Кiлькiсть елементiв: ";
  52.     cin >> size;
  53.     list<int>* l = new list<int>();
  54.     cout << "Всi елементи:\n";
  55.     PushBack(*l, size, false);
  56.     ShowList(*l);
  57.     l->clear();
  58.     cout << "\nПарнi елементи:\n";
  59.  
  60.     PushBack(*l, size, true);
  61.     ShowList(*l);
  62.     InsertSum(*l);
  63.     cout << "\nПiсля додавання суми:\n";
  64.     ShowList(*l);
  65.  
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment