Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Exam 1 / ex1 / NaturalNumbers.cpp
diff options
context:
space:
mode:
Commit9f61b59ca6ceb76cd634240d118f5771c0534f3d (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-20 18:43:21 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-20 18:43:21 +0300
Files3fa15098faa79b5df0927e92dae0125c447ac1be /Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp
uni-projects-9f61b59ca6ceb76cd634240d118f5771c0534f3d.tar
uni-projects-9f61b59ca6ceb76cd634240d118f5771c0534f3d.tar.gz
uni-projects-9f61b59ca6ceb76cd634240d118f5771c0534f3d.zip
Parent26cf424c42f9cdc172191faa5b2d1c1fcf350143 (diff)
Added my work from todays exam
1 files changed, 167 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp167
diff --git a/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp b/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp
new file mode 100644
index 0000000..84845d8
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp
@@ -0,0 +1,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;
+}