Guest User

Untitled

a guest
Sep 30th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. //Объявление класса "Множество":
  9. template <class T>
  10. class set
  11. {
  12. private:
  13.     T *a;
  14.     int max_size;
  15.     int size;
  16. public:
  17.     //Конструкторы:
  18.     set(int maxim);
  19.     set(set <T> &x);
  20.  
  21.     //Деструктор:
  22.     ~set();
  23.  
  24.     void add(T x);//добавление эл-та в мнохество
  25.     void del(T x);//удаление эл-та из множества
  26.     bool check_in(T x);//проверка вхождения эл-та в множество
  27.     void output();//Вывод множества на экран
  28.     set<T> operator= (const set <T> &s);//операция: присвоение одного множества другому
  29.     set<T> operator* (const set <T> &s2);//операция: пересечение двух множеств
  30.     set<T> operator+ (const set <T> &s2);//операция: объединение двух множеств
  31.     set<T> operator- (set <T> s2);//операция: разность двух множеств
  32.     friend bool operator== (set <T> s1, set <T> s2);//операция: сравнение двух множеств
  33. };
  34.  
  35. string rus(char s[]);
  36. void menu();
  37.  
  38. void main()
  39. {
  40.     setlocale(LC_ALL, "Russian");
  41.     cout << "Добро пожаловать в программу!" << endl;
  42.     menu();
  43. }
  44.  
  45.  
  46. void menu()
  47. {
  48.     bool p1, p2; p1 = p2 = true;
  49.     char n;
  50.     char choose;
  51.     int max_1, max_2;
  52.     cout << "Введите максимальный размер первого множества: ";
  53.     cin >> max_1;
  54.     cout << "Введите максимальный размер второго множества: ";
  55.     cin >> max_2;
  56.     set <int> set1(max_1);
  57.     set <int> set2(max_2);
  58.     set <int> set3(max_1 + max_2);
  59.     set <int> seta(max_1 + max_2);
  60.     set <int> setb(max_1 + max_2);
  61.  
  62.     int x;
  63.     do {
  64.         cout << '\n'
  65.             << "Меню:"
  66.             << '\n' << "1. Добавление эл-та в множество"
  67.             << '\n' << "2. Удаление эл-та из множества"
  68.             << '\n' << "3. Проверка вхождения эл-та в множество"
  69.             << '\n' << "4. Присвоение одного множества другому"
  70.             << '\n' << "5. Пересечение двух множеств"
  71.             << '\n' << "6. Объединение двух множеств"
  72.             << '\n' << "7. Разность двух множеств"
  73.             << '\n' << "8. Симметрическая разность двух множеств"
  74.             << '\n' << "0. Выход"
  75.             << '\n' << "<";
  76.         cin >> n;
  77.         system("cls");
  78.         switch (n)
  79.         {
  80.  
  81.         case '1':cout << "Выберите множество для добавления (1 или 2): ";
  82.             cin >> choose;
  83.  
  84.             if (choose == '1')
  85.             {
  86.                 if (p1 == true)
  87.                 {
  88.                     for (int i = 0; i < max_1; i++)
  89.                     {
  90.                         cout << "Введите э-т: ";
  91.                         cin >> x;
  92.                         set1.add(x);
  93.                     }
  94.                     cout << "Получившееся множество 1" << endl;
  95.                     set1.output();
  96.                     cout << " " << endl;
  97.                     p1 = false;
  98.                 }
  99.                 else { cout << "Вы уже ввели множество 1" << endl; set1.output(); }
  100.             }
  101.             if (choose == '2')
  102.             {
  103.                 if (p2 == true){
  104.                     for (int i = 0; i < max_2; i++)
  105.                     {
  106.                         cout << "Введите э-т: ";
  107.                         cin >> x;
  108.                         set2.add(x);
  109.                     }
  110.                     cout << "Получившееся множество 2" << endl;
  111.                     set2.output();
  112.                     cout << " " << endl;
  113.                     p2 = false;
  114.                 }
  115.                 else { cout << "Вы уже ввели множество 2" << endl; set2.output(); }
  116.             }
  117.  
  118.             break;
  119.         case '2':cout << "Выберите множество для удаления(1 или 2)"; //!!!
  120.             cin >> choose;
  121.              
  122.             if (choose == '1')
  123.             {
  124.                 cout << "Исходное множество:" << endl;
  125.                 set1.output();
  126.                 cout << "Введите величину эл-та";
  127.                 cin >> x;
  128.                 set1.del(x);
  129.             }
  130.             if (choose == '2')
  131.             {
  132.                 cout << "Исходное множество:" << endl;
  133.                 set2.output();
  134.                 cout << "Введите величину эл-та";
  135.                 cin >> x;
  136.                 set2.del(x);
  137.             }
  138.             break;
  139.         case '3':cout << "Выберете множество для проверки(1 или 2)";
  140.             cin >> choose;
  141.             cout << "Введите величину эл-та";
  142.             cin >> x;
  143.             if (choose == '1')
  144.             {
  145.                 cout << "Множество:\n" << endl;
  146.                 set1.output();
  147.                 if (set1.check_in(x))
  148.                     cout << "Элемент содержится в данном множестве";
  149.                 else
  150.                     cout << "Элемент не содержится в данном множестве";
  151.             }
  152.             if (choose == '2')
  153.             {
  154.                 cout << "Множество:\n" << endl;
  155.                 set2.output();
  156.                 if (set2.check_in(x))
  157.                     cout << "Элемент содержится в данном множестве";
  158.                 else
  159.                     cout << "Элемент не содержится в данном множестве";
  160.             }
  161.             break;
  162.         case '4':cout << "Выберите в какое множество копировать(1 или 2)";
  163.             cin >> choose;
  164.             if (choose == '1')
  165.             {
  166.                 cout << "Множество 1 до копирования" << endl;
  167.                 set1.output();
  168.                 set1 = set2;
  169.                 cout << "Множество 1 после копирования" << endl;
  170.                 set1.output();
  171.             }
  172.             if (choose == '2')
  173.                 set2 = set1;
  174.             break;
  175.         case '5':set3 = (set1*set2);
  176.             cout << "Исходное множество 1" << endl;
  177.             set1.output();
  178.             cout << "Исходное множество 2" << endl;
  179.             set2.output();
  180.             cout << "Получившееся множество 3" << endl;
  181.             set3.output();
  182.             cout << " " << endl;
  183.             break;
  184.         case '6':set3 = set1 + set2;
  185.             cout << "Исходное множество 1" << endl;
  186.             set1.output();
  187.             cout << "Исходное множество 2" << endl;
  188.             set2.output();
  189.             cout << "Получившееся множество 3" << endl;
  190.             set3.output();
  191.             cout << " " << endl;
  192.             break;
  193.         case '7':cout << "Выберите вычитаемое(1 или 2): ";
  194.             cin >> choose;
  195.             if (choose == '1')
  196.                 set3 = set1 - set2;
  197.             if (choose == '2')
  198.                 set3 = set2 - set1;
  199.  
  200.             cout << "Исходное множество 1" << endl;
  201.             set1.output();
  202.             cout << "Исходное множество 2" << endl;
  203.             set2.output();
  204.             cout << "Получившееся множество 3" << endl;
  205.             set3.output();
  206.             cout << " " << endl;
  207.             break;
  208.  
  209.         case '8':
  210.             seta = set1*set2;
  211.             setb = set2*set1;
  212.             set3 = seta + setb;
  213.  
  214.             cout << "Исходное множество 1" << endl;
  215.             set1.output();
  216.             cout << "Исходное множество 2" << endl;
  217.             set2.output();
  218.             cout << "Получившееся множество 3" << endl;
  219.             set3.output();
  220.             cout << " " << endl;
  221.             break;
  222.         case '0':return;
  223.         default:cout << "Действие не выбрано";
  224.         }
  225.     } while (true);
  226. }
  227.  
  228. template <class T>
  229. void set<T> ::add(T x)
  230. {
  231.     if (size<max_size)
  232.     {
  233.         if (!check_in(x))
  234.         {
  235.             a[size] = x;
  236.             size += 1;
  237.             cout << "Добавление прошло успешно" << endl;
  238.         }
  239.         else
  240.         {
  241.             cout << "Добавление невозможно!!!" << endl;
  242.             cout << "Одинаковые элементы содержаться в множестве только один раз!!!" << endl;
  243.         }
  244.     }
  245.     else
  246.     {
  247.         cout << "Добавление невозможно" << endl;
  248.         cout << "Размер множества не соответствует количеству элементов!!!" << endl;
  249.     }
  250. }
  251.  
  252.  
  253. template <class T>
  254. void set <T>::del(T x)
  255. {
  256.     bool ok;
  257.     ok = false;
  258.     if (size != 0)
  259.     {
  260.         for (int i = 0; i<size; i++)
  261.         {
  262.             if (a[i] == x)
  263.             {
  264.                 for (int j = i; j<size; j++)
  265.                 {
  266.                     a[j] = a[j + 1];
  267.                 }
  268.                 size -= 1;
  269.                 ok = true;;
  270.             }
  271.  
  272.         }
  273.         if (ok)
  274.         {
  275.             cout << "Удаление прошло успешно\nПолучившееся множество:\n";
  276.             set<T>::output();
  277.         }
  278.     }
  279.     else
  280.         cout << "Удаление невозможно";
  281. }
  282.  
  283.  
  284. template <class T>
  285. bool set <T>::check_in(T x)
  286. {
  287.     for (int i = 0; i<size; i++)
  288.     if (a[i] == x)
  289.         return true;
  290.     return false;
  291. }
  292.  
  293.  
  294.  
  295. template <class T>
  296. void set<T>::output()
  297. {
  298.     if (size == 0)
  299.     {
  300.         cout << "{}";
  301.         return;
  302.     }
  303.     cout << "{" << a[0];
  304.     for (int i = 1; i<size; i++)
  305.         cout << ", " << a[i];
  306.     cout << "}" << endl;
  307.  
  308. }
  309.  
  310.  
  311.  
  312. template <class T>
  313. set<T> set<T>:: operator* (const set <T> &s2)
  314. {
  315.     int k;
  316.     k = 0;
  317.     set <T> s(max_size);
  318.     s.size = 0;
  319.     for (int i = 0; i < size; i++)
  320.         cout << a[i] <<  " ";
  321.     cout << endl;
  322.     for (int j = 0; j < s2.size; j++)
  323.         cout << a[j] <<" ";
  324.     for (int i = 0; i < size; i++)
  325.     {
  326.         for (int j = 0; j < s2.size; j++)
  327.         if (s2.a[j] == a[i])
  328.         {
  329.             s.a[k] = a[i];
  330.             k++;
  331.             s.size += 1;
  332.         }
  333.     }
  334.     return s;
  335. }
  336.  
  337.  
  338. template <class T>
  339. set<T> set<T>::operator+ (const set <T> &s2)
  340. {
  341.     bool in;
  342.     set <T> s3(size + s2.size);
  343.     for (int i = 0; i<size; i++)
  344.     {
  345.  
  346.         s3.a[i] = a[i];
  347.         s3.size += 1;
  348.     }
  349.     for (int i = 0; i<s2.size; i++)
  350.     {
  351.         in = s3.check_in(s2.a[i]);
  352.  
  353.         if (!in)
  354.         {
  355.             s3.a[size] = s2.a[i];
  356.             s3.size += 1;
  357.         }
  358.     }
  359.     return s3;
  360. }
  361.  
  362.  
  363. template <class T>
  364. set<T> set<T>::operator- (set <T> s2)
  365. {
  366.     set <T> s3(s2);
  367.     for (int i = 0; i<size; i++)
  368.     for (int j = 0; j<s2.size; j++)
  369.     if (a[i] == s2.a[j])
  370.     for (int k = i; k<s3.size; k++)
  371.     {
  372.         s3.a[i] = s3.a[i + 1];
  373.         size -= 1;
  374.     }
  375.     return s2;
  376. }
  377.  
  378. template <class T>
  379. set<T> set<T>:: operator= (const set <T> &s)
  380. {
  381.     if (this == &s)
  382.         return *this;
  383.     if (max_size != s.max_size)
  384.     {
  385.         delete[]a;
  386.         a = new T[s.max_size];
  387.         size = 0;
  388.         max_size = s.max_size;
  389.     }
  390.     for (int i = 0; i<max_size; i++)
  391.     {
  392.         a[i] = s.a[i];
  393.     }
  394.     size = s.size;
  395.     return *this;
  396. }
  397.  
  398.  
  399.  
  400. template <class T>
  401. bool operator== (set <T> s1, set <T> s2)
  402. {
  403.     bool ok;
  404.     ok = false;
  405.     for (int i = 0; i<s1.size; i++)
  406.     {
  407.         for (int j = 0; j<s2.size; j++)
  408.         if (a[i] == a[j])
  409.             ok = true;
  410.     }
  411.     return ok;
  412. }
  413.  
  414.  
  415.  
  416. template <class T>
  417. set<T>::set(int maxim)
  418. {
  419.     a = new T[maxim];
  420.     for (int i = 0; i<maxim; i++)
  421.         a[i] = 0;
  422.     max_size = maxim;
  423.     size = 0;
  424. }
  425.  
  426.  
  427. template <class T>
  428. set<T>::set(set<T> &x)
  429. {
  430.     max_size = x.max_size;
  431.     a = new T[max_size];
  432.     for (int i = 0; i<max_size; i++)
  433.         a[i] = x.a[i];
  434. }
  435.  
  436.  
  437. template <class T>
  438. set<T>::~set()
  439. {
  440.     delete[]a;
  441. }
Advertisement
Add Comment
Please, Sign In to add comment