Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
diff options
context:
space:
mode:
Commit1e4762258f3e2d7db1045f3034e870f84cea7977 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-15 09:44:49 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-15 09:44:49 +0300
Filesfe597b9edc741b3d31968b10438a772168bcc3e1 /Year 1/Object-oriented Programming
uni-projects-1e4762258f3e2d7db1045f3034e870f84cea7977.tar
uni-projects-1e4762258f3e2d7db1045f3034e870f84cea7977.tar.gz
uni-projects-1e4762258f3e2d7db1045f3034e870f84cea7977.zip
Parentf522aaa802e4641118c3ce56f47f0115a36cdb13 (diff)
Moved compare to DynStr
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
index bac5c83..97a2fb4 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp
@@ -48,3 +48,11 @@ DynStr::~DynStr() {
const char* DynStr::get_str() const {
return str;
}
+
+int DynStr::compare(const char* other) const {
+ int cmp = strcmp(str, other);
+ // On some compilers (gcc) strcmp returns the difference, but we want it to return -1, 0 or 1
+ if (cmp > 1) cmp = 1;
+ else if (cmp < -1) cmp = -1;
+ return cmp;
+}
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
index 8b31da3..5978a72 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.h
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.h
@@ -14,4 +14,5 @@ public:
~DynStr();
const char* get_str() const;
+ int compare(const char* other) 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
index 4a375e5..ff0b48b 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
@@ -30,10 +30,12 @@ void WordCollection::resize() {
DynStr* newWords = new DynStr[length];
int* newSortMap = new int[length];
+
for (int i = 0; i < oldLength; i++) {
newWords[i] = words[i];
newSortMap[i] = sortMap[i];
}
+
delete[] words;
delete[] sortMap;
@@ -62,11 +64,7 @@ int WordCollection::findSortMapIndexWhen(int condition, const char* str) {
// TODO: replace with binary search
for (int i = 0; i < sortMapCount; i++) {
- int cmp = strcmp(words[sortMap[i]].get_str(), str);
- if (cmp > 1) cmp = 1;
- else if (cmp < -1) cmp = -1;
-
- if (cmp == condition)
+ if (words[sortMap[i]].compare(str) == condition)
return i;
}
return -1;