1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "NaturalNumbers.h"
#include <cstring>
/* Private */
void NaturalNumbers::copyFrom(const NaturalNumbers& other) {
this->nameLength = other.nameLength;
this->elementsCount = other.elementsCount;
this->name = new char[this->nameLength];
this->elements = new int[this->elementsCount];
strcpy(this->name, other.name);
for (unsigned i = 0; i < this->elementsCount; i++)
this->elements[i] = other.elements[i];
}
void NaturalNumbers::free() {
delete[] elements;
delete[] name;
}
void NaturalNumbers::move(NaturalNumbers&& other) {
this->elementsCount = other.elementsCount;
this->nameLength = other.nameLength;
this->name = other.name;
other.name = nullptr;
this->elements = other.elements;
other.elements = nullptr;
}
void NaturalNumbers::resize() {
unsigned newLength = (this->elementsLength == 0) ? 8 : this->elementsLength << 1;
int* largerElementsArray = new int[newLength];
for (int i = 0; i < this->elementsLength; i++)
largerElementsArray[i] = this->elements[i];
delete[] this->elements;
this->elements = largerElementsArray;
this->elementsLength = newLength;
}
int NaturalNumbers::indexOfNum(int number) const {
for (unsigned i = 0; i < this->elementsCount; i++) {
if (this->elements[i] == number)
return i;
}
return -1;
}
/* Public */
NaturalNumbers::NaturalNumbers() : NaturalNumbers(nullptr, 0, "Empty set")
{}
NaturalNumbers::NaturalNumbers(const int* elements, unsigned elementsCount, const char* name) {
this->elementsCount = this->elementsLength = 0;
this->elements = nullptr;
for (unsigned i = 0; i < elementsCount; i++) {
this->addElement(elements[i]);
}
this->name = nullptr;
this->set_name(name);
}
NaturalNumbers::~NaturalNumbers() {
this->free();
}
NaturalNumbers::NaturalNumbers(const NaturalNumbers& other) {
copyFrom(other);
}
NaturalNumbers& NaturalNumbers::operator=(const NaturalNumbers& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
bool NaturalNumbers::operator[](int number) const {
return this->indexOfNum(number) > -1;
}
NaturalNumbers& NaturalNumbers::operator+=(const NaturalNumbers& other) {
for (unsigned i = 0; i < other.elementsCount; i++) {
this->addElement(other.elements[i]);
}
unsigned newNameLength = 19 + this->nameLength + other.nameLength;
char* newName = new char[newNameLength];
strcat(newName, "Union of \'");
strcat(newName, this->name);
strcat(newName, "\' and \'");
strcat(newName, other.name);
strcat(newName, "\'");
delete[] this->name;
this->name = newName;
this->nameLength = newNameLength;
return *this;
}
const int* NaturalNumbers::get_elements() const {
return this->elements;
}
bool NaturalNumbers::addElement(int number) {
if (indexOfNum(number) == -1 && number > -1) {
if (this->elementsLength == this->elementsCount)
this->resize();
this->elements[this->elementsCount++] = number;
return true;
}
return false;
}
unsigned NaturalNumbers::get_elementsCount() const {
return this->elementsCount;
}
const char* NaturalNumbers::get_name() const {
return this->name;
}
void NaturalNumbers::set_name(const char* newName) {
this->nameLength = strlen(newName);
delete[] this->name;
this->name = new char[this->nameLength];
strcpy(this->name, newName);
}
/* External functions */
NaturalNumbers operator+(const NaturalNumbers& lhs, const NaturalNumbers& rhs) {
NaturalNumbers copy(lhs);
copy += rhs;
return copy;
}
std::ostream& operator<<(std::ostream& ostr, const NaturalNumbers& numbers) {
ostr << numbers.get_name() << " {";
if (numbers.get_elementsCount() > 0) {
for (int i = 0; i < numbers.get_elementsCount() - 1; i++)
ostr << numbers.get_elements()[i] << ", ";
ostr << numbers.get_elements()[numbers.get_elementsCount() - 1];
}
ostr << "}";
return ostr;
}
std::istream& operator>>(std::istream& istr, NaturalNumbers& numbers) {
unsigned count, buffer;
istr >> count;
while (count > 0) {
istr >> buffer;
numbers.addElement(buffer);
count--;
}
return istr;
}
|