Advertisement
VictoriaLodochkina

lab 8 test1

May 13th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "arrays.h"
  4. void task1(int& l, vector <vector<string>>& v);
  5. void task2(int& l, vector <vector<string>>& v);
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     /*int n, m;
  11.     cout << "Enter n, m:" << endl;
  12.     cin >> n;
  13.     cin >> m;
  14.     vector<string> A(n);
  15.     vector<string> B(m);
  16.     vector<string> C(0);
  17.     cout << "Enter A: " << endl;
  18.     cin.ignore(100, '\n');
  19.     cin.clear();
  20.     for (int i = 0; i < n; i++)
  21.     {
  22.         getline(cin, A[i]);
  23.     }
  24.     cout << "Enter B: " << endl;
  25.     for (int i = 0; i < m; i++)
  26.     {
  27.         getline(cin, B[i]);
  28.     }
  29.     arrays::Union(A, B, C);
  30.     for (int i = 0; i < C.size(); i++)
  31.     {
  32.         std::cout << C[i] << " ";
  33.     }
  34.     std::cout << std::endl;
  35.     C.clear();
  36.     arrays::Intersection(A, B, C);
  37.     for (int i = 0; i < C.size(); i++)
  38.     {
  39.         std::cout << C[i] << " ";
  40.     }
  41.     C.clear();
  42.     std::cout << std::endl;
  43.     arrays::Difference(A, B, C);
  44.     for (int i = 0; i < C.size(); i++)
  45.     {
  46.         std::cout << C[i] << " ";
  47.     }
  48.     std::cout << std::endl;
  49.     arrays::Affiliation("4", A);*/
  50.  
  51.  
  52.     int l;
  53.     int count = 0;
  54.     cout << "Enter l:" << endl;
  55.     cin >> l;
  56.     cin.ignore(100, '\n');
  57.     cin.clear();
  58.     string str;
  59.     vector <vector<string>> v(l);
  60.     for (int i = 0; i < l; i++)
  61.     {
  62.         cout << "Enter " << i + 1 << " vector:" << endl;
  63.         getline(cin, str);
  64.         while (str != "vika")
  65.         {
  66.             //getline(cin, str);
  67.             //v[i].push_back(str);
  68.             //str.clear();
  69.             v[i].push_back(str);
  70.             getline(cin, str);
  71.             count++;
  72.         }
  73.         str.clear();
  74.         count = 0;//потом проверить и убрать
  75.     }
  76.     //---------------------------
  77.     //№1
  78.     vector<string> C(0);
  79.     for (int i = 0; i < l-1; i++)
  80.     {
  81.         arrays::Intersection(v[i], v[i+1], C);
  82.  
  83.         v[i + 1].clear();
  84.         for (int j = 0; j < C.size(); j++)
  85.         {
  86.             v[i + 1].push_back(C[j]);
  87.         }
  88.         C.clear();
  89.     }
  90.     vector<string> rez1;
  91.     rez1.clear();
  92.     for (int j = 0; j < v[l-1].size(); j++)
  93.     {
  94.         rez1.push_back(v[l-1][j]);
  95.     }
  96.     for (int j = 0; j < rez1.size(); j++)
  97.     {
  98.         cout << rez1[j] << " ";
  99.     }
  100.     cout << endl;
  101.     //------------------------------
  102.     //#2
  103.     bool affiliation;
  104.     for (int i = 1; i < l; i++)
  105.     {
  106.         for (int j = 0; j < v[0].size(); j++)
  107.         {
  108.             if (arrays::Affiliation(v[0][j], v[i]))
  109.             {
  110.                 if ((i == l - 1)&&(j==v[0].size()-1))
  111.                 {
  112.                     affiliation = 1;
  113.                 }
  114.                 continue;
  115.             }
  116.             else
  117.             {
  118.                 affiliation = 0;
  119.                 break;
  120.             }
  121.         }
  122.     }
  123.     if (affiliation)
  124.     {
  125.         cout << "Yes" << endl;
  126.     }
  127.     else
  128.     {
  129.         cout << "No" << endl;
  130.     }
  131.     /*for (int i = 0; i < l; i++)
  132.     {
  133.         for (int j = 0; j < v[i].size(); j++)
  134.         {
  135.             cout << v[i][j] << " ";
  136.         }
  137.     }*/
  138.     return 0;
  139. }
  140.  
  141.  
  142. #include "arrays.h"
  143.  
  144. namespace arrays {
  145.     /*std::vector<std::string> Union(std::vector<std::string> First, std::vector<std::string> Second)
  146.     {
  147.         std::vector<std::string> _union (First.size()+ Second.size());
  148.         for (int i = 0; i < First.size(); i++)
  149.         {
  150.             _union[i] = First[i];
  151.         }
  152.         for (int i = 0; i < Second.size(); i++)
  153.         {
  154.             _union[First.size()+i] = Second[i];
  155.         }
  156.         for (int i = 0; i < First.size(); i++)
  157.         {
  158.             for (int j = First.size(); j < _union.size(); j++)
  159.             {
  160.                 if (_union[i] == _union[j])
  161.                 {
  162.                     _union.erase(_union.begin() + j);
  163.                     j--;//
  164.                 }
  165.             }
  166.         }
  167.         for (int i = 0; i < _union.size(); i++)
  168.         {
  169.             std::cout << _union[i] << " ";
  170.         }
  171.         std::cout << std::endl;
  172.         return _union;
  173.     }*/
  174.     void Union(std::vector<std::string> First, std::vector<std::string> Second, std::vector<std::string> &_union)
  175.     {
  176.         _union.resize(First.size() + Second.size());
  177.         for (int i = 0; i < First.size(); i++)
  178.         {
  179.             _union[i] = First[i];
  180.         }
  181.         for (int i = 0; i < Second.size(); i++)
  182.         {
  183.             _union[First.size() + i] = Second[i];
  184.         }
  185.         for (int i = 0; i < First.size(); i++)
  186.         {
  187.             for (int j = First.size(); j < _union.size(); j++)
  188.             {
  189.                 if (_union[i] == _union[j])
  190.                 {
  191.                     _union.erase(_union.begin() + j);
  192.                     j--;//
  193.                 }
  194.             }
  195.         }
  196.     }
  197. //-------------------------------- 
  198.     /*std::vector<std::string> Intersection(std::vector<std::string> First, std::vector<std::string> Second)
  199.     {
  200.         std::vector<std::string> _intersection(0);        
  201.         for (int i = 0; i < First.size(); i++)
  202.         {
  203.             for (int j = 0; j < Second.size(); j++)
  204.             {
  205.                 if (First[i] == Second[j])
  206.                 {
  207.                     _intersection.push_back(First[i]);
  208.                     break;
  209.                 }
  210.             }
  211.         }
  212.         for (int i = 0; i < _intersection.size(); i++)
  213.         {
  214.             std::cout << _intersection[i] << " ";
  215.         }
  216.         std::cout << std::endl;
  217.         return _intersection;
  218.     }*/
  219.  
  220.     void Intersection(std::vector<std::string> First, std::vector<std::string> Second, std::vector<std::string>& _intersection)
  221.     {
  222.         _intersection.resize(0);
  223.         for (int i = 0; i < First.size(); i++)
  224.         {
  225.             for (int j = 0; j < Second.size(); j++)
  226.             {
  227.                 if (First[i] == Second[j])
  228.                 {
  229.                     _intersection.push_back(First[i]);
  230.                     break;
  231.                 }
  232.             }
  233.         }
  234.         /*for (int i = 0; i < _intersection.size(); i++)
  235.         {
  236.             std::cout << _intersection[i] << " ";
  237.         }
  238.         std::cout << std::endl;*/
  239.         //return _intersection;
  240.     }
  241. //-------------------------------
  242.     /*std::vector<std::string> Difference(std::vector<std::string> First, std::vector<std::string> Second)
  243.     {
  244.         std::vector<std::string> _difference(0);
  245.         for (int i = 0; i < First.size(); i++)
  246.         {
  247.             for (int j = 0; j < Second.size(); j++)
  248.             {
  249.                 if (First[i] == Second[j])
  250.                 {
  251.                     break;
  252.                 }
  253.                 else
  254.                 {
  255.                     if (Second.size()-1==j)
  256.                     _difference.push_back(First[i]);
  257.                 }
  258.             }
  259.  
  260.         }
  261.         for (int i = 0; i < _difference.size(); i++)
  262.         {
  263.             std::cout << _difference[i] << " ";
  264.         }
  265.         std::cout << std::endl;
  266.         return _difference;
  267.     }*/
  268.  
  269.     void Difference(std::vector<std::string> First, std::vector<std::string> Second, std::vector<std::string>& _difference)
  270.     {
  271.         _difference.resize(0);
  272.         for (int i = 0; i < First.size(); i++)
  273.         {
  274.             for (int j = 0; j < Second.size(); j++)
  275.             {
  276.                 if (First[i] == Second[j])
  277.                 {
  278.                     break;
  279.                 }
  280.                 else
  281.                 {
  282.                     if (Second.size() - 1 == j)
  283.                         _difference.push_back(First[i]);
  284.                 }
  285.             }
  286.  
  287.         }
  288.         //return _difference;
  289.     }
  290. //----------------------------------
  291.     bool Affiliation(std::string element, std::vector<std::string> First)
  292.     {
  293.         for (int i = 0; i < First.size(); i++)
  294.         {
  295.             if (element == First[i])
  296.             {
  297.                 //std::cout << "+";
  298.                 return true;
  299.             }
  300.         }
  301.         return false;
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement