|
| 1 | +// |
| 2 | +// Created by sasha on 25.02.2022. |
| 3 | +// |
| 4 | + |
| 5 | +#include "SetLab4_Kudashov.h" |
| 6 | +// Проверка на пустое множество |
| 7 | +bool SetListContainer::emptySet() { |
| 8 | + return set.empty(); |
| 9 | +} |
| 10 | +// Проверка принадлежности элемента множеству |
| 11 | +bool SetListContainer::checkingOfExistence(int checking_value) { |
| 12 | + if (this->emptySet()) return false; |
| 13 | + for (int iter : set) |
| 14 | + if (iter == checking_value) |
| 15 | + return true; |
| 16 | + return false; |
| 17 | +} |
| 18 | +// Добавление нового элемента в множество в начало списка |
| 19 | +void SetListContainer::add(int adding_value) { |
| 20 | + set.push_front(adding_value); |
| 21 | +} |
| 22 | +// Мощность множества |
| 23 | +int SetListContainer::powerOfTheSet() { |
| 24 | + return set.size(); |
| 25 | +} |
| 26 | +// Создание нового множества |
| 27 | +SetListContainer SetListContainer::creatingSet(int quantity, int min, int max, int k) { |
| 28 | + SetListContainer set = *new SetListContainer(); |
| 29 | + // k - коэффициент кратности |
| 30 | + // Множество А – множество чисел, кратных 5. Множество В – множество чисел, кратных 10. |
| 31 | + if (k*quantity <= max - min + 1){ |
| 32 | + while (set.powerOfTheSet() < quantity){ |
| 33 | + int temp = rand() % (max-min+1) + min; |
| 34 | + if (temp % k == 0) |
| 35 | + set.add(temp); |
| 36 | + } |
| 37 | + } |
| 38 | + else |
| 39 | + cout << "It is impossible to create a set according to the specified conditions!" << endl; |
| 40 | + return set; |
| 41 | +} |
| 42 | +// Вывод элементов множества |
| 43 | +string SetListContainer::printSet(const string &separator) { |
| 44 | + if (this->emptySet()) return "It is impossible to output an empty set!"; |
| 45 | + string print; |
| 46 | + for (auto iter = set.begin(); iter != set.end(); iter++) |
| 47 | + print += to_string(*iter) + separator; |
| 48 | + print.pop_back(); |
| 49 | + print.pop_back(); |
| 50 | + return print; |
| 51 | +} |
| 52 | +// Удаление множества |
| 53 | +void SetListContainer::deleteSet() { |
| 54 | + set.clear(); |
| 55 | +} |
| 56 | +// Является ли A подмножеством B |
| 57 | +bool SetListContainer::isSubset(SetListContainer a, SetListContainer b) { |
| 58 | + if (a.emptySet()) return true; |
| 59 | + if (a.powerOfTheSet() > b.powerOfTheSet()) return false; |
| 60 | + for (int iter: a.set) |
| 61 | + if (!b.checkingOfExistence(iter)) |
| 62 | + return false; |
| 63 | + return true; |
| 64 | +} |
| 65 | +// Проверка множеств на равенство |
| 66 | +bool SetListContainer::isEqual(SetListContainer a, SetListContainer b) { |
| 67 | + return isSubset(a,b) && (a.powerOfTheSet() == b.powerOfTheSet()); |
| 68 | +} |
| 69 | +// Объединение множеств |
| 70 | +SetListContainer SetListContainer::combiningSets(SetListContainer a, SetListContainer b) { |
| 71 | + if (a.emptySet() || b.emptySet()) |
| 72 | + return *new SetListContainer(); |
| 73 | + a.set.sort(); |
| 74 | + b.set.sort(); |
| 75 | + a.set.merge(b.set); |
| 76 | + a.set.unique(); |
| 77 | + return a; |
| 78 | +} |
| 79 | +// Пересечение множеств |
| 80 | +SetListContainer SetListContainer::intersectionOfSets(SetListContainer a, SetListContainer b) { |
| 81 | + if (a.emptySet() || b.emptySet()) |
| 82 | + return *new SetListContainer(); |
| 83 | + SetListContainer c = *new SetListContainer(); |
| 84 | + for (int iter: a.set) |
| 85 | + if(b.checkingOfExistence(iter)) |
| 86 | + c.add(iter); |
| 87 | + return c; |
| 88 | +} |
| 89 | +// Разность множеств |
| 90 | +SetListContainer SetListContainer::differenceOfSets(const SetListContainer &a, SetListContainer b) { |
| 91 | + SetListContainer c = *new SetListContainer(); |
| 92 | + for (int iter: a.set) |
| 93 | + if(!b.checkingOfExistence(iter)) |
| 94 | + c.add(iter); |
| 95 | + return c; |
| 96 | +} |
| 97 | +// Симметричная разность множеств |
| 98 | +SetListContainer SetListContainer::symmetricDifferenceOfSets(const SetListContainer &a, const SetListContainer &b) { |
| 99 | + if (intersectionOfSets(a,b).emptySet()) return combiningSets(a,b); |
| 100 | + return differenceOfSets(combiningSets(a,b), intersectionOfSets(a,b)); |
| 101 | +} |
0 commit comments