Skip to content

Commit 6d5befb

Browse files
authored
Laboratory work 1. Adding all functions
- Adding a new element to a set - Creating a set - Power of the set - Output of set elements - Deleting a set (clearing the memory occupied by the list)
1 parent 666a620 commit 6d5befb

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed
48.7 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: Dec 26 10:11 RTZ 2 (ceia)
1+
Start testing: Dec 26 12:56 RTZ 2 (ceia)
22
----------------------------------------------------------
3-
End testing: Dec 26 10:11 RTZ 2 (ceia)
3+
End testing: Dec 26 12:56 RTZ 2 (ceia)
47.1 KB
Binary file not shown.

laba_1/main.cpp

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
#include <string>
23
using namespace std;
34
// Элемент списка
45
struct Node {
@@ -14,19 +15,90 @@ Node* creatingAnEmptySet() {
1415
bool emptySet(Node* first) {
1516
return first == nullptr;
1617
}
17-
// Проверка принадлежности элемента множеству.
18+
// Проверка принадлежности элемента множеству
1819
bool 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

Comments
 (0)