diff options
1 files changed, 16 insertions, 15 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp | 31 |
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 index ae5792f..aa24c3a 100644 --- a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp @@ -49,9 +49,9 @@ void WordCollection::insertSortMapIndex(int position, int value) { } void WordCollection::removeSortMapIndex(int position) { - for (int i = position; i < count - 1; i++) + for (int i = position; i <= count - 2; i++) sortMap[i] = sortMap[i + 1]; - sortMap[count--] = 0; + sortMap[count - 1] = 0; } int WordCollection::findSortMapIndexWhen(int condition, const char* str) { @@ -96,34 +96,36 @@ WordCollection::~WordCollection() { 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(); + (*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(); + (*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++; + if (!(*this)[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); + if (ind > -1) { + words[sortMap[ind]] = DynStr(); + removeSortMapIndex(ind); + count--; + } return *this; } @@ -138,7 +140,6 @@ std::ostream& operator<<(std::ostream& ostrm, const WordCollection& collection) std::istream& operator>>(std::istream& istrm, WordCollection& collection) { char buffer[1024]; istrm.getline(buffer, 1024); - istrm.ignore(); collection *= buffer; return istrm; } @@ -156,7 +157,7 @@ const WordCollection operator+(const WordCollection& lhs, const WordCollection& const WordCollection operator-(const WordCollection& lhs, const WordCollection& rhs) { WordCollection res(lhs); - res += rhs; + res -= rhs; return res; } |
