11#include < iostream>
2+ #include < string>
23using namespace std ;
34// Элемент списка
45struct Node {
@@ -14,19 +15,90 @@ Node* creatingAnEmptySet() {
1415bool emptySet (Node* first) {
1516 return first == nullptr ;
1617}
17- // Проверка принадлежности элемента множеству.
18+ // Проверка принадлежности элемента множеству
1819bool checkingOfExistence (Node* first, int checking_value) {
1920 if (emptySet (first)) return false ;
2021 Node* current = first;
21- while (current && current->value != checking_value)
22+ while (current-> next && current->value != checking_value)
2223 current = current->next ;
2324 return current->value == checking_value;
24-
2525};
26- int main () {
27- std::cout << " Hello, World!" << std::endl;
26+ // Добавление нового элемента в множество в начало списка
27+ Node* add (Node*& first, int adding_value) {
28+ if (!checkingOfExistence (first, adding_value)){
29+ Node* new_node = new Node;
30+ new_node->value = adding_value;
31+ new_node->next = emptySet (first) ? nullptr : first;
32+ first = new_node;
33+ }
34+ return first;
35+ }
36+ // Мощность множества
37+ int powerOfTheSet (Node* first) {
38+ int power = 0 ;
39+ if (emptySet (first)) return 0 ;
40+ Node* current = first;
41+ power++;
42+ while (current->next ) {
43+ current = current->next ;
44+ power++;
45+ }
46+ return power;
47+ }
48+ // Создание нового множества
49+ Node* creatingSet (int quantity, int min, int max, int k){
2850 Node* set = creatingAnEmptySet ();
29- cout << set;
30- cout << emptySet (set);
31- return 0 ;
51+ // int k = 5; // коэффициент кратности
52+ // Множество А – множество чисел, кратных 5. Множество В – множество чисел, кратных 10.
53+ if (k*quantity <= max - min + 1 ){
54+ while (powerOfTheSet (set) < quantity){
55+ int temp = rand () % (max-min+1 ) + min;
56+ if (temp % k == 0 )
57+ add (set, temp);
58+ }
59+ }
60+ cout << " Creating a set is not possible" ;
61+ return set;
62+ }
63+ // Вывод элементов множества
64+ string printSet (Node* first, string separator){
65+ if (emptySet (first)) return " Elements not found" ;
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+ Node* deleteSet (Node*& first){
77+ Node* current = first;
78+ if (emptySet (first)) // Если список пуст, сообщить об этом
79+ cout << " The list is empty!" ;
80+ while (current) { // Удаление элементов, если они есть
81+ Node* temp = current;
82+ current = current->next ;
83+ delete temp;
84+ }
85+ first = nullptr ;
86+ return first;
3287}
88+ int main () {
89+ srand ( time (0 ) );
90+ Node* setA = creatingSet (rand () % 4 + 6 , 5 , 500 , 5 );
91+ cout << " Print set A: " << printSet (setA, " | " ) << endl;
92+ cout << " Power set A: " << powerOfTheSet (setA) << endl;
93+ Node* setB = creatingSet (rand () % 4 + 6 , 5 , 500 , 10 );
94+ cout << " Print set B: " << printSet (setB, " | " ) << endl;
95+ cout << " Power set B: " << powerOfTheSet (setB) << endl;
96+ deleteSet (setA);
97+ cout << " ---------------After cleaning---------------" << endl;
98+ cout << " Print set A: " << printSet (setA, " | " ) << endl;
99+ cout << " Power set A: " << powerOfTheSet (setA) << endl;
100+ deleteSet (setB);
101+ cout << " Print set B: " << printSet (setB, " | " ) << endl;
102+ cout << " Power set B: " << powerOfTheSet (setB) << endl;
103+ return 0 ;
104+ }
0 commit comments