Skip to content

Commit c1202d5

Browse files
authored
Laboratory work 6
1 parent 9a06cee commit c1202d5

36 files changed

+3921
-0
lines changed

Sets_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(Sets_Kudashov)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
add_executable(Sets_Kudashov Sets_Kudashov.cpp SetLab1_12_Kudashov.cpp SetLab1_12_Kudashov.h SetLab3_Kudashov.cpp SetLab3_Kudashov.h SetLab4_Kudashov.cpp SetLab4_Kudashov.h SetLab5_Kudashov.cpp SetLab5_Kudashov.h)

Sets_Kudashov/SetLab1_12_Kudashov.cpp

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

Sets_Kudashov/SetLab1_12_Kudashov.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef LABA_1_SETLAB1_12_KUDASHOV_H
2+
#define LABA_1_SETLAB1_12_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+
// Создание пустого множества
13+
Node* creatingAnEmptySet();
14+
// Проверка на пустое множество
15+
bool emptySet(Node* first);
16+
// Проверка принадлежности элемента множеству
17+
bool checkingOfExistence(Node* first, int checking_value);
18+
// Добавление нового элемента в множество в начало списка
19+
Node* add(Node*& first, int adding_value);
20+
// Мощность множества
21+
int powerOfTheSet (Node* first);
22+
// Создание нового множества
23+
Node* creatingSet(int quantity, int min, int max, int k);
24+
// Вывод элементов множества
25+
string printSet(Node* first, string separator);
26+
// Удаление множества
27+
Node* deleteSet(Node*& first);
28+
29+
// Является ли A подмножеством B
30+
bool isSubset(Node* A, Node* B);
31+
// Проверка множеств на равенство
32+
bool isEqual(Node* A, Node* B);
33+
// Объединение множеств
34+
Node* combiningSets(Node* A, Node* B);
35+
// Пересечение множеств
36+
Node* intersectionOfSets(Node* A, Node* B);
37+
// Разность множеств
38+
Node* differenceOfSets(Node* A, Node* B);
39+
// Симметричная разность множеств
40+
Node* symmetricDifferenceOfSets(Node* A, Node* B);
41+
#endif //LABA_1_SETLAB1_12_KUDASHOV_H

Sets_Kudashov/SetLab3_Kudashov.cpp

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+
NodeOOP* SetOOP::getFirst() {
4+
return first;
5+
}
6+
// Создание пустого множества
7+
SetOOP::SetOOP() {
8+
first = nullptr;
9+
}
10+
// Создание пустого множества
11+
SetOOP SetOOP::creatingAnEmptySet() {
12+
return *new SetOOP;
13+
}
14+
// Проверка на пустое множество
15+
bool SetOOP::emptySet() {
16+
return first == nullptr;
17+
}
18+
// Проверка принадлежности элемента множеству
19+
bool SetOOP::checkingOfExistence(int checking_value) {
20+
if (this->emptySet()) return false;
21+
NodeOOP* current = first;
22+
while (current->next && current->value != checking_value)
23+
current = current->next;
24+
return current->value == checking_value;
25+
}
26+
// Добавление нового элемента в множество в начало списка
27+
void SetOOP::add(int adding_value) {
28+
if (!checkingOfExistence(adding_value)){
29+
NodeOOP* new_node = new NodeOOP;
30+
new_node->value = adding_value;
31+
new_node->next = first;
32+
first = new_node;
33+
}
34+
}
35+
// Мощность множества
36+
int SetOOP::powerOfTheSet() {
37+
int power = 0;
38+
if (this->emptySet()) return 0;
39+
NodeOOP* current = first;
40+
power++;
41+
while (current->next) {
42+
current = current->next;
43+
power++;
44+
}
45+
return power;
46+
}
47+
// Создание нового множества
48+
SetOOP SetOOP::creatingSet(int quantity, int min, int max, int k) {
49+
SetOOP set = *new SetOOP();
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 SetOOP::printSet(const string& separator) {
65+
if (this->emptySet()) return "It is impossible to output an empty set!";
66+
NodeOOP* 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 SetOOP::deleteSet() {
77+
NodeOOP* current = first;
78+
if (this->emptySet()) // Если список пуст, сообщить об этом
79+
cout << "The set is already empty!" << endl;
80+
while (current) { // Удаление элементов, если они есть
81+
NodeOOP* temp = current;
82+
current = current->next;
83+
delete temp;
84+
}
85+
first = nullptr;
86+
}
87+
// Является ли A подмножеством B
88+
bool SetOOP::isSubset(SetOOP a, SetOOP b) {
89+
if (a.emptySet()) return true;
90+
if (a.powerOfTheSet() > b.powerOfTheSet()) return false;
91+
NodeOOP* 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 SetOOP::isEqual(SetOOP a, SetOOP b) {
101+
return isSubset(a,b) && (a.powerOfTheSet() == b.powerOfTheSet());
102+
}
103+
// Объединение множеств
104+
SetOOP SetOOP::combiningSets(SetOOP a, SetOOP b) {
105+
if (a.emptySet() || b.emptySet())
106+
return *new SetOOP();
107+
SetOOP c = a;
108+
NodeOOP* 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+
SetOOP SetOOP::intersectionOfSets(SetOOP a, SetOOP b) {
120+
if (a.emptySet() || b.emptySet())
121+
return *new SetOOP();
122+
SetOOP c = *new SetOOP();
123+
NodeOOP* 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+
SetOOP SetOOP::differenceOfSets(SetOOP a, SetOOP b) {
135+
if (a.emptySet() || b.emptySet())
136+
return *new SetOOP();
137+
SetOOP c = *new SetOOP();
138+
NodeOOP* 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+
SetOOP SetOOP::symmetricDifferenceOfSets(SetOOP a, SetOOP b) {
150+
if (intersectionOfSets(a,b).emptySet()) return combiningSets(a,b);
151+
return differenceOfSets(combiningSets(a,b), intersectionOfSets(a,b));
152+
}

0 commit comments

Comments
 (0)