#include #include #include #include #include #include using namespace std; class SetElem { public: char el; SetElem* next; SetElem(char e, SetElem* n = nullptr) : el(e), next(n) { } ~SetElem() { delete next; } }; class Set { private: static int N, cnt; // мощность универсума и счетчик множеств int n; // мощность множества char S; // тег и память для множества SetElem* A; public: Set operator | (const Set&) const; // объединение Set& operator |= (const Set&); Set operator & (const Set&) const; // пересечение Set& operator &= (const Set&); Set operator ~ () const; // дополнение до универсума void Show(); // вывод множества на экран int power() { return n; } // получение мощности Set(char);// конструктор множества Set(); // еще конструктор — по умолчанию Set(const Set&); // конструктор копии Set(Set&&); // перемещающий конструктор (C++11) Set operator = (const Set&); // оператор присваивания Set operator = (Set&&); //присваивание с перемещением (C++11) ~Set() {} // деструктор }; Set::Set() : n(0), S('A' + cnt++), A(nullptr) {}; Set::Set(char) : S('A' + cnt++), n(0), A(nullptr) { SetElem* pA = A; for (int i = 0; i < N; i++) if (rand() % 2) { if (A) { pA->next = new SetElem(char(i + 'A')); pA = pA->next; } else { A = new SetElem(char(i + 'A')); pA = A; } n++; } cout << '\n' << S << " = ["; for (SetElem* p = A; p; p = p->next) cout << p->el; cout << "]"; } Set::Set(const Set& B) : S('A' + cnt++), n(B.n), A(nullptr) { SetElem* pA = A, * pB = B.A; while (pB) { if (pA) { pA->next = new SetElem(pB->el); pA = pA->next; } else { A = new SetElem(B.A->el); pA = A; } pB = pB->next; } } Set Set:: operator= (const Set& B) { if (this != &B) { n = B.n; A = nullptr; SetElem* pA = A, *pB = B.A; while (pB) { if (pA) { pA->next = new SetElem(B.A->el); pA = pA->next; } else { A = new SetElem(B.A->el); pA = A; } pB = pB->next; } S = 'A' + cnt++; } return *this; } Set& Set :: operator &= (const Set& B) { n = 0; SetElem* last = nullptr, * pA = A; for (SetElem* pB = B.A; pB; pB = pB->next) { while (pA && pA->el < pB->el) pA = pA->next; if (pA && pA->el == pB->el) { if (last == nullptr) { last = pA; A = pA; } else { last->next = pA; last = pA; } } } if (pA) last->next = nullptr; for (pA = A; pA; pA = pA->next) n++; return *this; } Set Set :: operator & (const Set& B) const { Set C(*this); return std::move(C &= B); } Set& Set :: operator |= (const Set& B) { n = 0; SetElem* pA = A; for (SetElem* pB = B.A; pB; pB = pB->next) { if (pA) { while (pA->next && pA->next->el - 'A' < pB->el - 'A') pA = pA->next; if (!pA->next || pA->next->el - 'A' != pB->el - 'A') { if (pA == A) { if (pB->el - 'A' <= pA->el - 'A') { pA = new SetElem(pB->el, A); A = pA; } } else { pA->next = new SetElem(pB->el, pA->next); pA = pA->next; } } } else { pA = new SetElem(pB->el); A = pA; } } for (pA = A; pA; pA = pA->next) n++; return *this; } Set Set :: operator | (const Set& B) const { Set C(*this); return std::move(C |= B); } Set Set :: operator ~ () const { Set C; C.n = N - n; SetElem* pC = C.A, *pA = A; for (char c = 'A'; c <= 'Z'; ++c) { // Цикл по универсуму if (pA && pA->el > c) { if (pC) { pC->next = new SetElem(c); pC = pC->next; } else { C.A = new SetElem(c); pC = C.A; } } else { if(pA) pA = pA->next; else { pC->next = new SetElem(c); pC = pC->next; } } } return std::move(C); } Set::Set(Set&& B) : S('A' + cnt++), n(B.n), A(B.A) { B.A = 0; } // Копирование с переносом Set Set:: operator = (Set&& B) // Присваивание с переносом { if (this != &B) { n = B.n; A = B.A; S = 'A' + cnt++; B.A = 0; } return *this; } void Set::Show() { cout << '\n' << S << " = ["; for (SetElem* p = A; p; p = p->next) cout << p->el; cout << "]"; }