Maxim_Leo

Untitled

Feb 23rd, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #include<string>
  4. #include <vector>
  5. #include <set>
  6. #include <algorithm>
  7. /*1. Организовать процедуру для задания элементов множества А. Организовать процедуру printset, пе-чатающую элементы множества А с определением их числа.
  8. 2. Задать два текстовых предложения. Разбить первое и второе предложения на отдельные слова и поместить эти слова в два множества. Написать функцию сравнения полученных множеств, не используя опе-рации объединения и пересечения множеств, результатом работы которой будет являться массив слов, встре-чающихся в обоих предложениях.
  9. 3. Задать множество S состоящее из символов. Разделить множество S на три подмножества: А – символы русского алфавита, Б – латинские символы, В – цифры.
  10.  
  11.  
  12. */
  13. set<int> enterset(set<int> N, int n) {
  14. cout <<"Введите "<< n<<" чисел: " << endl;
  15. for (int i = 0; i < n; i++) {
  16. cout<< i + 1<< ") ";
  17. int dig;
  18. cin >> dig;
  19. N.insert(dig);
  20. }
  21. cout<< endl;
  22. return N;
  23. }
  24.  
  25. void print(set<int>arr) {
  26. cout << "Элементы множества A: ";
  27. copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
  28. cout << endl;
  29. }
  30. void compareset(set<string> A, set<string> B) {
  31. vector <string> H;
  32. vector <string> A1;
  33. vector <string> B1;
  34. set<string> ::iterator it1; // объявляем итератор
  35. set<string> ::iterator it2;
  36. for (it1 = A.begin(); it1 != A.end(); ++it1) // пока итератор не достигнет последнего элемента
  37. {
  38. for (it2 = B.begin(); it2 != B.end(); ++it2) {
  39. if (*it1 == *it2) H.push_back(*it1);// и переходим к следующему элементу
  40. }
  41. }
  42.  
  43. for (string i : H) {
  44. cout << i<<" ";
  45. }
  46.  
  47.  
  48. }
  49.  
  50.  
  51. set<string> separate(string query) {
  52. string word;
  53. set<string> arr;
  54. int k = 0;
  55. for (int i = 0; i <= query.size(); ++i)
  56. {
  57. if (query[i] != ' ' && i != query.size()) word += query[i];
  58. if (i == query.size()) {
  59. word += query[i];
  60. arr.insert(word);
  61. }
  62. else if (query[i] == ' ') {
  63. arr.insert(word);
  64. ++k;
  65. word = ""s;
  66.  
  67. }
  68. }
  69. copy(arr.begin(), arr.end(), ostream_iterator<string>(cout, " "));
  70. cout << endl;
  71. return arr;
  72. }
  73.  
  74.  
  75. int main() {
  76. setlocale(LC_ALL,"Russian");
  77. srand(time(NULL));
  78. set<int> N;
  79. N=enterset(N, 5);
  80. print(N);
  81.  
  82. set<string> A;
  83. set <string> B;
  84.  
  85. int k = 0;
  86.  
  87. string word, word1;
  88. string s1 = "Parrot of Britain";
  89.  
  90.  
  91. cout << "Множество A:" << endl;
  92. A=separate(s1);
  93. cout << endl;
  94.  
  95.  
  96. cout << endl;
  97. string s2 = "London is the capital of Great Britain";
  98. cout << "Множество B:" << endl;
  99. B=separate(s2);
  100. cout << endl;
  101.  
  102.  
  103.  
  104. cout << "Сравнение Множества А и B: ";
  105. compareset(A, B);
  106. cout << endl;
  107.  
  108.  
  109.  
  110. char S[10] = { 'А','5','i','б','L','o','Т','y','9','и'};
  111. set <char> A1;
  112. set <char> B1;
  113. set <char> C1;
  114. for (int i = 0; i < 10; ++i) {
  115. if (S[i] >= 'а' && S[i] <= 'я' || S[i] >= 'А' && S[i] <= 'Я') A1.insert(S[i]);
  116. if (S[i] >= 'a' && S[i] <= 'z' || S[i] >= 'A' && S[i] <= 'Z') B1.insert(S[i]);
  117. if (S[i] >= '0' && S[i] <= '9') C1.insert(S[i]);
  118. }
  119. cout << "Множество А: ";
  120. copy(A1.begin(), A1.end(), ostream_iterator<char>(cout, " "));
  121. cout << endl;
  122. cout << "Множество Б: ";
  123. copy(B1.begin(), B1.end(), ostream_iterator<char>(cout, " "));
  124. cout << endl;
  125. cout << "Множество В: ";
  126. copy(C1.begin(), C1.end(), ostream_iterator<char>(cout, " "));
  127. cout << endl;
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment