Skip to content

Commit f7e1b7a

Browse files
authored
Laboratory work 4
1 parent 4fc37c6 commit f7e1b7a

27 files changed

+3100
-0
lines changed
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(Set_Cont_List_Kudashov)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
add_executable(Set_Cont_List_Kudashov Set_Cont_List_Kudashov.cpp SetLab4_Kudashov.cpp SetLab4_Kudashov.h)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// Created by sasha on 25.02.2022.
3+
//
4+
5+
#include "SetLab4_Kudashov.h"
6+
// Проверка на пустое множество
7+
bool SetListContainer::emptySet() {
8+
return set.empty();
9+
}
10+
// Проверка принадлежности элемента множеству
11+
bool SetListContainer::checkingOfExistence(int checking_value) {
12+
if (this->emptySet()) return false;
13+
for (int iter : set)
14+
if (iter == checking_value)
15+
return true;
16+
return false;
17+
}
18+
// Добавление нового элемента в множество в начало списка
19+
void SetListContainer::add(int adding_value) {
20+
set.push_front(adding_value);
21+
}
22+
// Мощность множества
23+
int SetListContainer::powerOfTheSet() {
24+
return set.size();
25+
}
26+
// Создание нового множества
27+
SetListContainer SetListContainer::creatingSet(int quantity, int min, int max, int k) {
28+
SetListContainer set = *new SetListContainer();
29+
// k - коэффициент кратности
30+
// Множество А – множество чисел, кратных 5. Множество В – множество чисел, кратных 10.
31+
if (k*quantity <= max - min + 1){
32+
while (set.powerOfTheSet() < quantity){
33+
int temp = rand() % (max-min+1) + min;
34+
if (temp % k == 0)
35+
set.add(temp);
36+
}
37+
}
38+
else
39+
cout << "It is impossible to create a set according to the specified conditions!" << endl;
40+
return set;
41+
}
42+
// Вывод элементов множества
43+
string SetListContainer::printSet(const string &separator) {
44+
if (this->emptySet()) return "It is impossible to output an empty set!";
45+
string print;
46+
for (auto iter = set.begin(); iter != set.end(); iter++)
47+
print += to_string(*iter) + separator;
48+
print.pop_back();
49+
print.pop_back();
50+
return print;
51+
}
52+
// Удаление множества
53+
void SetListContainer::deleteSet() {
54+
set.clear();
55+
}
56+
// Является ли A подмножеством B
57+
bool SetListContainer::isSubset(SetListContainer a, SetListContainer b) {
58+
if (a.emptySet()) return true;
59+
if (a.powerOfTheSet() > b.powerOfTheSet()) return false;
60+
for (int iter: a.set)
61+
if (!b.checkingOfExistence(iter))
62+
return false;
63+
return true;
64+
}
65+
// Проверка множеств на равенство
66+
bool SetListContainer::isEqual(SetListContainer a, SetListContainer b) {
67+
return isSubset(a,b) && (a.powerOfTheSet() == b.powerOfTheSet());
68+
}
69+
// Объединение множеств
70+
SetListContainer SetListContainer::combiningSets(SetListContainer a, SetListContainer b) {
71+
if (a.emptySet() || b.emptySet())
72+
return *new SetListContainer();
73+
a.set.sort();
74+
b.set.sort();
75+
a.set.merge(b.set);
76+
a.set.unique();
77+
return a;
78+
}
79+
// Пересечение множеств
80+
SetListContainer SetListContainer::intersectionOfSets(SetListContainer a, SetListContainer b) {
81+
if (a.emptySet() || b.emptySet())
82+
return *new SetListContainer();
83+
SetListContainer c = *new SetListContainer();
84+
for (int iter: a.set)
85+
if(b.checkingOfExistence(iter))
86+
c.add(iter);
87+
return c;
88+
}
89+
// Разность множеств
90+
SetListContainer SetListContainer::differenceOfSets(const SetListContainer &a, SetListContainer b) {
91+
SetListContainer c = *new SetListContainer();
92+
for (int iter: a.set)
93+
if(!b.checkingOfExistence(iter))
94+
c.add(iter);
95+
return c;
96+
}
97+
// Симметричная разность множеств
98+
SetListContainer SetListContainer::symmetricDifferenceOfSets(const SetListContainer &a, const SetListContainer &b) {
99+
if (intersectionOfSets(a,b).emptySet()) return combiningSets(a,b);
100+
return differenceOfSets(combiningSets(a,b), intersectionOfSets(a,b));
101+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef SET_CONT_LIST_KUDASHOV_SETLAB4_KUDASHOV_H
2+
#define SET_CONT_LIST_KUDASHOV_SETLAB4_KUDASHOV_H
3+
#include <iostream>
4+
#include <list>
5+
using namespace std;
6+
7+
class SetListContainer {
8+
private:
9+
list <int> set;
10+
public:
11+
// Проверка на пустое множество
12+
bool emptySet();
13+
// Проверка принадлежности элемента множеству
14+
bool checkingOfExistence(int checking_value);
15+
// Добавление нового элемента в множество в начало списка
16+
void add(int adding_value);
17+
// Мощность множества
18+
int powerOfTheSet();
19+
// Создание нового множества
20+
static SetListContainer creatingSet(int quantity, int min, int max, int k);
21+
// Вывод элементов множества
22+
string printSet(const string& separator);
23+
// Удаление множества
24+
void deleteSet();
25+
// Является ли A подмножеством B
26+
static bool isSubset(SetListContainer a, SetListContainer b);
27+
// Проверка множеств на равенство
28+
static bool isEqual(SetListContainer a, SetListContainer b);
29+
// Объединение множеств
30+
static SetListContainer combiningSets(SetListContainer a, SetListContainer b);
31+
// Пересечение множеств
32+
static SetListContainer intersectionOfSets(SetListContainer a, SetListContainer b);
33+
// Разность множеств
34+
static SetListContainer differenceOfSets(const SetListContainer& a, SetListContainer b);
35+
// Симметричная разность множеств
36+
static SetListContainer symmetricDifferenceOfSets(const SetListContainer& a, const SetListContainer& b);
37+
};
38+
#endif //SET_CONT_LIST_KUDASHOV_SETLAB4_KUDASHOV_H
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "SetLab4_Kudashov.h"
2+
3+
int main() {
4+
srand( time(nullptr));
5+
setlocale(LC_ALL, "Russian");
6+
cout << "=================ÄÅÌÎ ËÀÁÛ 4=================" << endl;
7+
SetListContainer setA = SetListContainer::creatingSet (rand() % 4 + 6, 5, 500, 5);
8+
cout << "Âûâîä ìíîæåñòâà A: " << endl << setA.printSet(" | ") << endl;
9+
cout << "Ìîùíîñòü ìíîæåñòâà A: " << endl << setA.powerOfTheSet() << endl;
10+
SetListContainer setB = SetListContainer::creatingSet (rand() % 4 + 6, 5, 500, 10);
11+
cout << "Âûâîä ìíîæåñòâà B: " << endl << setB.printSet(" | ") << endl;
12+
cout << "Ìîùíîñòü ìíîæåñòâà B: " << endl << setB.powerOfTheSet() << endl;
13+
cout << "----------------Ïîñëå î÷èñòêè----------------" << endl;
14+
setA.deleteSet();
15+
cout << "Âûâîä ìíîæåñòâà A: " << endl << setA.printSet(" | ") << endl;
16+
cout << "Ìîùíîñòü ìíîæåñòâà A: " << endl << setA.powerOfTheSet() << endl;
17+
setB.deleteSet();
18+
cout << "Âûâîä ìíîæåñòâà B: " << endl << setB.printSet(" | ") << endl;
19+
cout << "Ìîùíîñòü ìíîæåñòâà B: " << endl << setB.powerOfTheSet() << endl << endl;
20+
SetListContainer A = SetListContainer::creatingSet (rand() % 4 + 6, 5, 200, 10);
21+
SetListContainer B = SetListContainer::creatingSet (rand() % 4 + 6, 5, 200, 5);
22+
cout << "Âûâîä ìíîæåñòâà A: " << endl << A.printSet(" | ") << endl;
23+
cout << "Âûâîä ìíîæåñòâà B: " << endl << B.printSet(" | ") << endl;
24+
cout << "ßâëÿåòñÿ ëè A ïîäìíîæåñòâîì B: " << SetListContainer::isSubset(A, B) << endl;
25+
cout << "Ðàâíû ëè ìíîæåñòâà A è B: " << SetListContainer::isEqual(A, B) << endl;
26+
SetListContainer C = SetListContainer::combiningSets(A, B);
27+
cout << "Âûâîä îáúåäèíåíèÿ ìíîæåñòâ: " << endl << C.printSet(" | ") << endl;
28+
SetListContainer D = SetListContainer::intersectionOfSets(A, B);
29+
cout << "Âûâîä ïåðåñå÷åíèÿ ìíîæåñòâ: " << endl << D.printSet(" | ") << endl;
30+
SetListContainer E = SetListContainer::differenceOfSets(A, B);
31+
cout << "Âûâîä ðàçíîñòè ìíîæåñòâ A è B: " << endl << E.printSet(" | ") << endl;
32+
SetListContainer F = SetListContainer::differenceOfSets(B, A);
33+
cout << "Âûâîä ðàçíîñòè ìíîæåñòâ B è A:" << endl << F.printSet(" | ") << endl;
34+
SetListContainer G = SetListContainer::symmetricDifferenceOfSets(A, B);
35+
cout << "Âûâîä ñèììåòðè÷íîé ðàçíîñòè ìíîæåñòâ: " << endl << G.printSet(" | ") << endl;
36+
return 0;
37+
}

0 commit comments

Comments
 (0)