Skip to content

Commit 4fc37c6

Browse files
authored
Laboratory work 3
1 parent abb180c commit 4fc37c6

27 files changed

+3162
-0
lines changed

OOP_Set_Kudashov/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
project(OOP_Set_Kudashov)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
add_executable(OOP_Set_Kudashov OOP_Set_Kudashov.cpp OOP_Set_Kudashov.cpp SetLab3_Kudashov.cpp SetLab3_Kudashov.h)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "SetLab3_Kudashov.h"
2+
int main() {
3+
srand( time(nullptr));
4+
setlocale(LC_ALL, "Russian");
5+
cout << "=================ÄÅÌÎ ËÀÁÛ 3=================" << endl;
6+
List setA = List::creatingSet (rand() % 4 + 6, 5, 500, 5);
7+
cout << "Âûâîä ìíîæåñòâà A: " << endl << setA.printSet(" | ") << endl;
8+
cout << "Ìîùíîñòü ìíîæåñòâà A: " << endl << setA.powerOfTheSet() << endl;
9+
List setB = List::creatingSet (rand() % 4 + 6, 5, 500, 10);
10+
cout << "Âûâîä ìíîæåñòâà B: " << endl << setB.printSet(" | ") << endl;
11+
cout << "Ìîùíîñòü ìíîæåñòâà B: " << endl << setB.powerOfTheSet() << endl;
12+
cout << "----------------Ïîñëå î÷èñòêè----------------" << endl;
13+
setA.deleteSet();
14+
cout << "Âûâîä ìíîæåñòâà A: " << endl << setA.printSet(" | ") << endl;
15+
cout << "Ìîùíîñòü ìíîæåñòâà A: " << endl << setA.powerOfTheSet() << endl;
16+
setB.deleteSet();
17+
cout << "Âûâîä ìíîæåñòâà B: " << endl << setB.printSet(" | ") << endl;
18+
cout << "Ìîùíîñòü ìíîæåñòâà B: " << endl << setB.powerOfTheSet() << endl << endl;
19+
List A = List::creatingSet (rand() % 4 + 6, 5, 200, 10);
20+
List B = List::creatingSet (rand() % 4 + 6, 5, 200, 5);
21+
cout << "Âûâîä ìíîæåñòâà A: " << endl << A.printSet(" | ") << endl;
22+
cout << "Âûâîä ìíîæåñòâà B: " << endl << B.printSet(" | ") << endl;
23+
cout << "ßâëÿåòñÿ ëè A ïîäìíîæåñòâîì B: " << List::isSubset(A,B) << endl;
24+
cout << "Ðàâíû ëè ìíîæåñòâà A è B: " << List::isEqual(A,B) << endl;
25+
List C = List::combiningSets(A,B);
26+
cout << "Âûâîä îáúåäèíåíèÿ ìíîæåñòâ: " << endl << C.printSet(" | ") << endl;
27+
List D = List::intersectionOfSets(A,B);
28+
cout << "Âûâîä ïåðåñå÷åíèÿ ìíîæåñòâ: " << endl << D.printSet(" | ") << endl;
29+
List E = List::differenceOfSets(A,B);
30+
cout << "Âûâîä ðàçíîñòè ìíîæåñòâ A è B: " << endl << E.printSet(" | ") << endl;
31+
List F = List::differenceOfSets(B,A);
32+
cout << "Âûâîä ðàçíîñòè ìíîæåñòâ B è A:" << endl << F.printSet(" | ") << endl;
33+
List G = List::symmetricDifferenceOfSets(A,B);
34+
cout << "Âûâîä ñèììåòðè÷íîé ðàçíîñòè ìíîæåñòâ: " << endl << G.printSet(" | ") << endl;
35+
return 0;
36+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef OOP_SET_KUDASHOV_SETLAB3_KUDASHOV_H
2+
#define OOP_SET_KUDASHOV_SETLAB3_KUDASHOV_H
3+
#pragma once
4+
#include <iostream>
5+
#include <string>
6+
using namespace std;
7+
// Элемент списка
8+
struct Node {
9+
int value; // Данные - числовое значение
10+
Node* next; // Указатель на следующий элемент списка
11+
};
12+
class List {
13+
private:
14+
Node* first;
15+
public:
16+
// Получение указателя на верщину
17+
Node* getFirst();
18+
// Создание пустого множества
19+
List();
20+
// Создание пустого множества
21+
static List creatingAnEmptySet();
22+
// Проверка на пустое множество
23+
bool emptySet();
24+
// Проверка принадлежности элемента множеству
25+
bool checkingOfExistence(int checking_value);
26+
// Добавление нового элемента в множество в начало списка
27+
void add(int adding_value);
28+
// Мощность множества
29+
int powerOfTheSet ();
30+
// Создание нового множества
31+
static List creatingSet(int quantity, int min, int max, int k);
32+
// Вывод элементов множества
33+
string printSet(const string& separator);
34+
// Удаление множества
35+
void deleteSet();
36+
// Является ли A подмножеством B
37+
static bool isSubset(List a, List b);
38+
// Проверка множеств на равенство
39+
static bool isEqual(List a, List b);
40+
// Объединение множеств
41+
static List combiningSets(List a, List b);
42+
// Пересечение множеств
43+
static List intersectionOfSets(List a, List b);
44+
// Разность множеств
45+
static List differenceOfSets(List a, List b);
46+
// Симметричная разность множеств
47+
static List symmetricDifferenceOfSets(List a, List b);
48+
};
49+
50+
#endif

0 commit comments

Comments
 (0)