|
| 1 | +#include "SetLab3_Kudashov.h" |
| 2 | +// Получение указателя на верщину |
| 3 | +Node *List::getFirst() { |
| 4 | + return first; |
| 5 | +} |
| 6 | +// Создание пустого множества |
| 7 | +List::List() { |
| 8 | + first = nullptr; |
| 9 | +} |
| 10 | +// Создание пустого множества |
| 11 | +List List::creatingAnEmptySet() { |
| 12 | + return *new List; |
| 13 | +} |
| 14 | +// Проверка на пустое множество |
| 15 | +bool List::emptySet() { |
| 16 | + return first == nullptr; |
| 17 | +} |
| 18 | +// Проверка принадлежности элемента множеству |
| 19 | +bool List::checkingOfExistence(int checking_value) { |
| 20 | + if (this->emptySet()) return false; |
| 21 | + Node* current = first; |
| 22 | + while (current->next && current->value != checking_value) |
| 23 | + current = current->next; |
| 24 | + return current->value == checking_value; |
| 25 | +} |
| 26 | +// Добавление нового элемента в множество в начало списка |
| 27 | +void List::add(int adding_value) { |
| 28 | + if (!checkingOfExistence(adding_value)){ |
| 29 | + Node* new_node = new Node; |
| 30 | + new_node->value = adding_value; |
| 31 | + new_node->next = first; |
| 32 | + first = new_node; |
| 33 | + } |
| 34 | +} |
| 35 | +// Мощность множества |
| 36 | +int List::powerOfTheSet() { |
| 37 | + int power = 0; |
| 38 | + if (this->emptySet()) return 0; |
| 39 | + Node* current = first; |
| 40 | + power++; |
| 41 | + while (current->next) { |
| 42 | + current = current->next; |
| 43 | + power++; |
| 44 | + } |
| 45 | + return power; |
| 46 | +} |
| 47 | +// Создание нового множества |
| 48 | +List List::creatingSet(int quantity, int min, int max, int k) { |
| 49 | + List set = *new List(); |
| 50 | + // k - коэффициент кратности |
| 51 | + // Множество А – множество чисел, кратных 5. Множество В – множество чисел, кратных 10. |
| 52 | + if (k*quantity <= max - min + 1){ |
| 53 | + while (set.powerOfTheSet() < quantity){ |
| 54 | + int temp = rand() % (max-min+1) + min; |
| 55 | + if (temp % k == 0) |
| 56 | + set.add(temp); |
| 57 | + } |
| 58 | + } |
| 59 | + else |
| 60 | + cout << "It is impossible to create a set according to the specified conditions!" << endl; |
| 61 | + return set; |
| 62 | +} |
| 63 | +// Вывод элементов множества |
| 64 | +string List::printSet(const string& separator) { |
| 65 | + if (this->emptySet()) return "It is impossible to output an empty set!"; |
| 66 | + Node* current = first; |
| 67 | + string print; |
| 68 | + while (current->next) { |
| 69 | + print += to_string(current->value) + separator; |
| 70 | + current = current->next; |
| 71 | + } |
| 72 | + print += to_string(current->value); |
| 73 | + return print; |
| 74 | +} |
| 75 | +// Удаление множества |
| 76 | +void List::deleteSet() { |
| 77 | + Node* current = first; |
| 78 | + if (this->emptySet()) // Если список пуст, сообщить об этом |
| 79 | + cout << "The set is already empty!" << endl; |
| 80 | + while (current) { // Удаление элементов, если они есть |
| 81 | + Node* temp = current; |
| 82 | + current = current->next; |
| 83 | + delete temp; |
| 84 | + } |
| 85 | + first = nullptr; |
| 86 | +} |
| 87 | +// Является ли A подмножеством B |
| 88 | +bool List::isSubset(List a, List b) { |
| 89 | + if (a.emptySet()) return true; |
| 90 | + if (a.powerOfTheSet() > b.powerOfTheSet()) return false; |
| 91 | + Node* current = a.getFirst(); |
| 92 | + while (current->next){ |
| 93 | + if(!b.checkingOfExistence(current->value)) |
| 94 | + return false; |
| 95 | + current = current->next; |
| 96 | + } |
| 97 | + return true; |
| 98 | +} |
| 99 | +// Проверка множеств на равенство |
| 100 | +bool List::isEqual(List a, List b) { |
| 101 | + return isSubset(a,b) && (a.powerOfTheSet() == b.powerOfTheSet()); |
| 102 | +} |
| 103 | +// Объединение множеств |
| 104 | +List List::combiningSets(List a, List b) { |
| 105 | + if (a.emptySet() || b.emptySet()) |
| 106 | + return *new List(); |
| 107 | + List c = a; |
| 108 | + Node* current = b.getFirst(); |
| 109 | + while (current->next) { |
| 110 | + if(!c.checkingOfExistence(current->value)) |
| 111 | + c.add(current->value); |
| 112 | + current = current->next; |
| 113 | + } |
| 114 | + if(b.checkingOfExistence(current->value)) |
| 115 | + c.add(current->value); |
| 116 | + return c; |
| 117 | +} |
| 118 | +// Пересечение множеств |
| 119 | +List List::intersectionOfSets(List a, List b) { |
| 120 | + if (a.emptySet() || b.emptySet()) |
| 121 | + return *new List(); |
| 122 | + List c = *new List(); |
| 123 | + Node* current = a.getFirst(); |
| 124 | + while (current->next){ |
| 125 | + if(b.checkingOfExistence(current->value)) |
| 126 | + c.add(current->value); |
| 127 | + current = current->next; |
| 128 | + } |
| 129 | + if(b.checkingOfExistence(current->value)) |
| 130 | + c.add(current->value); |
| 131 | + return c; |
| 132 | +} |
| 133 | +// Разность множеств |
| 134 | +List List::differenceOfSets(List a, List b) { |
| 135 | + if (a.emptySet() || b.emptySet()) |
| 136 | + return *new List(); |
| 137 | + List c = *new List(); |
| 138 | + Node* current = a.getFirst(); |
| 139 | + while (current->next){ |
| 140 | + if(!b.checkingOfExistence(current->value)) |
| 141 | + c.add(current->value); |
| 142 | + current = current->next; |
| 143 | + } |
| 144 | + if(!b.checkingOfExistence(current->value)) |
| 145 | + c.add(current->value); |
| 146 | + return c; |
| 147 | +} |
| 148 | +// Симметричная разность множеств |
| 149 | +List List::symmetricDifferenceOfSets(List a, List b) { |
| 150 | + if (intersectionOfSets(a,b).emptySet()) return combiningSets(a,b); |
| 151 | + return differenceOfSets(combiningSets(a,b), intersectionOfSets(a,b)); |
| 152 | +} |
0 commit comments