diff options
Implemented (broken) overall functionality of ex3 of sem 08
5 files changed, 292 insertions, 0 deletions
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp new file mode 100644 index 0000000..4f4d52e --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp @@ -0,0 +1,50 @@ +#include "DynStr.h" +#include <cstring> + +/* Private */ + +void DynStr::free() { + if(str != nullptr) delete[] str; +} + +void DynStr::copyFrom(const DynStr& other) { + length = other.length; + + str = new char[length]; + for (int i = 0; i < length; i++) + str[i] = other.str[i]; +} + +/* Public */ + +DynStr::DynStr() { + str = nullptr; + length = 0; +} + +DynStr::DynStr(const char* str) { + int strLen = strlen(str); + this->str = new char[strLen]; + strcpy(this->str, str); + length = strLen; +} + +DynStr::DynStr(const DynStr& copy) { + copyFrom(copy); +} + +DynStr& DynStr::operator=(const DynStr& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +DynStr::~DynStr() { + free(); +} + +const char* DynStr::get_str() const { + return str; +} diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.h b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.h new file mode 100644 index 0000000..8b31da3 --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.h @@ -0,0 +1,17 @@ +class DynStr { + char* str; + int length; + + void free(); + void copyFrom(const DynStr& other); + +public: + DynStr(); + DynStr(const char* str); + + DynStr(const DynStr& copy); + DynStr& operator=(const DynStr& other); + ~DynStr(); + + const char* get_str() const; +}; diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp new file mode 100644 index 0000000..ae5792f --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp @@ -0,0 +1,162 @@ +#include "WordCollection.h" +#include "cstring" +#include <iostream> + +/* Private */ + +void WordCollection::free() { + if(words != nullptr) delete[] words; + if(sortMap != nullptr) delete[] sortMap; +} + +void WordCollection::copyFrom(const WordCollection& other) { + count = other.count; + length = other.length; + + words = new DynStr[length]; + sortMap = new int[length]; + for (int i = 0; i < count; i++) { + words[i] = other.words[i]; + sortMap[i] = other.sortMap[i]; + } +} + +void WordCollection::resize() { + if (length == 0) length = 8; + else length <<= 1; + + DynStr* newWords = new DynStr[length]; + int* newSortMap = new int[length]; + for (int i = 0; i < count; i++) { + newWords[i] = words[i]; + newSortMap[i] = sortMap[i]; + } + + if (words != nullptr) delete[] words; + if (sortMap != nullptr) delete[] sortMap; + + words = newWords; + sortMap = newSortMap; +} + +void WordCollection::insertSortMapIndex(int position, int value) { + if (position == -1) + position = count; + + for (int i = count; i > position; i--) + sortMap[i] = sortMap[i - 1]; + sortMap[position] = value; +} + +void WordCollection::removeSortMapIndex(int position) { + for (int i = position; i < count - 1; i++) + sortMap[i] = sortMap[i + 1]; + sortMap[count--] = 0; +} + +int WordCollection::findSortMapIndexWhen(int condition, const char* str) { + if (condition < -1) condition = -1; + else if (condition > 1) condition = 1; + + // TODO: replace with binary search + for (int i = 0; i < count; i++) { + int cmp = strcmp(words[sortMap[i]].get_str(), str); + if (cmp > 1) cmp = 1; + else if (cmp < -1) cmp = -1; + + if (cmp == condition) + return i; + } + return -1; +} + +/* Public */ + +WordCollection::WordCollection() { + words = nullptr; + sortMap = nullptr; + count = length = 0; +} + +WordCollection::WordCollection(const WordCollection& other) { + copyFrom(other); +} + +WordCollection& WordCollection::operator=(const WordCollection& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +WordCollection::~WordCollection() { + free(); +} + +WordCollection& WordCollection::operator+=(const WordCollection& other) { + for (int i = 0; i < other.count; i++) { + if ((*this)[other.words[i].get_str()]) + (*this) *= other.words[i].get_str(); + } + return *this; +} + +WordCollection& WordCollection::operator-=(const WordCollection& other) { + for (int i = 0; i < other.count; i++) { + if (!(*this)[other.words[i].get_str()]) + (*this) /= other.words[i].get_str(); + } + return *this; +} + +WordCollection& WordCollection::operator*=(const char* str) { + if (count == length) resize(); + + words[count] = DynStr(str); + insertSortMapIndex(findSortMapIndexWhen(1, str), count); + count++; + + return *this; +} + +WordCollection& WordCollection::operator/=(const char* str) { + int ind = findSortMapIndexWhen(0, str); + words[sortMap[ind]] = DynStr(); + removeSortMapIndex(ind); + return *this; +} + +std::ostream& operator<<(std::ostream& ostrm, const WordCollection& collection) { + ostrm << "{"; + for (int i = 0; i <= collection.count - 2; i++) + ostrm << collection.words[collection.sortMap[i]].get_str() << ", "; + ostrm << collection.words[collection.sortMap[collection.count - 1]].get_str() << "}"; + return ostrm; +} + +std::istream& operator>>(std::istream& istrm, WordCollection& collection) { + char buffer[1024]; + istrm.getline(buffer, 1024); + istrm.ignore(); + collection *= buffer; + return istrm; +} + +bool WordCollection::operator[](const char* str) { + return findSortMapIndexWhen(0, str) > -1; +} + +const WordCollection operator+(const WordCollection& lhs, const WordCollection& rhs) { + WordCollection res(lhs); + res += rhs; + + return res; +} + +const WordCollection operator-(const WordCollection& lhs, const WordCollection& rhs) { + WordCollection res(lhs); + res += rhs; + + return res; +} diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h new file mode 100644 index 0000000..f7a7142 --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h @@ -0,0 +1,38 @@ +#include "DynStr.h" +#include <istream> +#include <ostream> + +class WordCollection { + DynStr* words; + int* sortMap; + int count; + int length; + + void free(); + void copyFrom(const WordCollection& other); + + void resize(); + void insertSortMapIndex(int position, int value); + void removeSortMapIndex(int position); + int findSortMapIndexWhen(int condition, const char* str); + +public: + WordCollection(); + + WordCollection(const WordCollection& other); + WordCollection& operator=(const WordCollection& other); + ~WordCollection(); + + WordCollection& operator+=(const WordCollection& other); + WordCollection& operator-=(const WordCollection& other); + WordCollection& operator*=(const char* str); + WordCollection& operator/=(const char* str); + + friend std::ostream& operator<<(std::ostream& ostrm, const WordCollection& collection); + friend std::istream& operator>>(std::istream& istrm, WordCollection& collection); + + bool operator[](const char* str); +}; + +const WordCollection operator+(const WordCollection& lhs, const WordCollection& rhs); +const WordCollection operator-(const WordCollection& lhs, const WordCollection& rhs); diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/source.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/source.cpp new file mode 100644 index 0000000..3a04ba6 --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/source.cpp @@ -0,0 +1,25 @@ +#include "WordCollection.h" +#include <iostream> + +int main() +{ + WordCollection dict; + ((dict *= "Ivan") *= "Petur") *= "Alex"; + std::cout << dict << std::endl; // {Alex, Ivan, Petur} + std::cout << dict["Alex"] << " " << dict["abc"] << std::endl; // 1 0 + + WordCollection dict2; + ((dict *= "Zebra") *= "Alex"); + // + WordCollection result = dict + dict2; + std::cout << result << std::endl; // {Alex, Ivan, Petur, Zebra} + + WordCollection result2 = dict - dict2; + std::cout << result2 << std::endl; // {Ivan, Petur} + + result2 *= "NewWOrd"; + result2 /= "NonExistingWord"; + std::cout << result2 << std::endl; //{Ivan, NewWOrd, Petur} + std::cin >> result2; //въвеждаме "Job" + std::cout << result2 << std::endl; //{Ivan, Job, NewWOrd, Petur} +} |
