Advertisement
ortem_kats

salibek_lab3

Feb 4th, 2020
2,553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
  5. C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <iostream>
  10. #include <set>
  11. #include <cstdlib>
  12. #include <ctime>
  13. #include <algorithm>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. int main()
  19. {
  20.     srand(time(0));
  21.    
  22.     //Способ задания универсального множества U: N случайных чисел из диапазона [a, b];
  23.     int N = 20, a = -100, b = 100;
  24.     set<int> s;
  25.     for (int i = 0; i < N; i++){
  26.         int x = rand() % (b - a + 1) + a;
  27.         if (s.find(x) != s.end()){
  28.             i--;
  29.             continue;
  30.         }
  31.         s.insert(x);
  32.     }
  33.    
  34.    
  35.     cout << "U:\n";
  36.     for (auto &x : s)
  37.         cout << x << " | ";
  38.     cout << "\n\n";
  39.        
  40.     //Способ выбора элементов множества A из универсального множества: Случайным образом (случайным образом выбираются индексы универсального множества)
  41.     int N1 = 15;
  42.     set<int> s1;
  43.     for (int i = 0; i < N1; i++){
  44.         auto it = s.begin();
  45.         advance(it, rand() % N);
  46.         int x = *(it);
  47.         if (s1.find(x) != s1.end()){
  48.             i--;
  49.             continue;
  50.         }
  51.         s1.insert(x);
  52.     }
  53.    
  54.     cout << "A:\n";
  55.     for (auto &x : s1)
  56.         cout << x << " | ";
  57.     cout << "\n\n";
  58.    
  59.     //Способы выбора элементов множества B из универсального множества: Элементы, принадлежащие диапазону [c, d]
  60.     int c = -50, d = 50;
  61.     set<int> s2;
  62.     for (auto it = s.lower_bound(c); it != s.upper_bound(d); it++)
  63.         s2.insert(*it);
  64.        
  65.     cout << "B:\n";
  66.     for (auto &x : s2)
  67.         cout << x << " | ";
  68.     cout << "\n\n";
  69.    
  70.     //Реализовать два преобразования над множествами A и B и показать идентичность их результата: (A or B и /(/A and /B))
  71.     vector<int> or_vec(N);
  72.     auto it = set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), or_vec.begin());
  73.     or_vec.resize(it - or_vec.begin());
  74.    
  75.     cout << "A or B:\n";
  76.     for (auto &x : or_vec)
  77.         cout << x << " | ";
  78.     cout << "\n\n";
  79.    
  80.     vector<int> not_s1(N), not_s2(N), and_vec(N), not_and_vec(N);
  81.     it = set_difference(s.begin(), s.end(), s1.begin(), s1.end(), not_s1.begin());
  82.     not_s1.resize(it - not_s1.begin());
  83.    
  84.     it = set_difference(s.begin(), s.end(), s2.begin(), s2.end(), not_s2.begin());
  85.     not_s2.resize(it - not_s2.begin());
  86.    
  87.     it = set_intersection(not_s1.begin(), not_s1.end(), not_s2.begin(), not_s2.end(), and_vec.begin());
  88.     and_vec.resize(it - and_vec.begin());
  89.    
  90.     it = set_difference(s.begin(), s.end(), and_vec.begin(), and_vec.end(), not_and_vec.begin());
  91.     not_and_vec.resize(it - not_and_vec.begin());
  92.    
  93.     cout << "/(/A and /B):\n";
  94.     for (auto &x : not_and_vec)
  95.         cout << x << " | ";
  96.     cout << "\n\n";
  97.    
  98.     //Вывод результата: Нечетные по значению элементы
  99.     cout << "Вывод результата:\n";
  100.     for (auto &x : not_and_vec)
  101.         if (x%2) cout << x << " | ";
  102.     cout << "\n\n";
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement