Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Solutions Sem. 08 / ex3 / WordCollection.cpp
diff options
context:
space:
mode:
Commit970ddb225bc2064903be787ea81afa129a1b83a6 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-15 07:45:32 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-15 07:45:32 +0300
Files59f6268f2cf3588a8ea28e622d6cfd3d0f1af972 /Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp
uni-projects-970ddb225bc2064903be787ea81afa129a1b83a6.tar
uni-projects-970ddb225bc2064903be787ea81afa129a1b83a6.tar.gz
uni-projects-970ddb225bc2064903be787ea81afa129a1b83a6.zip
Parent9829b368ce529df277397e3b465fc536fa65ebfd (diff)
Fixed removing of words and improved adding words and cin and cout
1 files changed, 16 insertions, 15 deletions
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp31
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;
}