This repository has been archived on 2022-06-05. You can view files and clone it, but cannot push or open issues or pull requests.
Files
AiSD/Part1/Lab2/Set4.h
2022-05-14 15:22:55 +03:00

208 lines
4.1 KiB
C++

#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <cstdio>
#include <time.h>
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 << "]";
}