aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise1
diff options
context:
space:
mode:
Diffstat (limited to 'week13/Exercise1')
-rw-r--r--week13/Exercise1/IndexArray.cpp51
-rw-r--r--week13/Exercise1/IndexArray.h18
-rw-r--r--week13/Exercise1/String.cpp51
-rw-r--r--week13/Exercise1/String.h18
-rw-r--r--week13/Exercise1/WordString.cpp25
-rw-r--r--week13/Exercise1/WordString.h8
6 files changed, 171 insertions, 0 deletions
diff --git a/week13/Exercise1/IndexArray.cpp b/week13/Exercise1/IndexArray.cpp
new file mode 100644
index 0000000..ed7a212
--- /dev/null
+++ b/week13/Exercise1/IndexArray.cpp
@@ -0,0 +1,51 @@
+#include "IndexArray.h"
+#include <cstring>
+
+void IndexArray::free() {
+ delete[] indecies;
+}
+
+void IndexArray::copyFrom(const IndexArray& other) {
+ this->size = other.size;
+ this->indecies = new int[size];
+ for (int i = 0; i < size; i++) {
+ this->indecies[i] = other.indecies[i];
+ }
+}
+
+IndexArray::IndexArray() {
+ this->indecies = nullptr;
+ this->size = 0;
+}
+
+IndexArray::~IndexArray() {
+ free();
+}
+
+IndexArray::IndexArray(const IndexArray& other) {
+ copyFrom(other);
+}
+
+IndexArray& IndexArray::operator=(const IndexArray& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+IndexArray::IndexArray(IndexArray&& other) {
+ this->indecies = other.indecies;
+ other.indecies = nullptr;
+}
+
+IndexArray& IndexArray::operator=(IndexArray&& other) {
+ if (this != &other) {
+ free();
+
+ this->indecies = other.indecies;
+ other.indecies = nullptr;
+ }
+ return *this;
+}
+
diff --git a/week13/Exercise1/IndexArray.h b/week13/Exercise1/IndexArray.h
new file mode 100644
index 0000000..90c948e
--- /dev/null
+++ b/week13/Exercise1/IndexArray.h
@@ -0,0 +1,18 @@
+#pragma once
+
+class IndexArray {
+ void free();
+ void copyFrom(const IndexArray& other);
+
+protected:
+ int *indecies;
+ unsigned size;
+
+public:
+ IndexArray();
+ virtual ~IndexArray();
+ IndexArray(const IndexArray& other);
+ IndexArray& operator=(const IndexArray& other);
+ IndexArray(IndexArray&& other);
+ IndexArray& operator=(IndexArray&& other);
+};
diff --git a/week13/Exercise1/String.cpp b/week13/Exercise1/String.cpp
new file mode 100644
index 0000000..2b41e96
--- /dev/null
+++ b/week13/Exercise1/String.cpp
@@ -0,0 +1,51 @@
+#include "String.h"
+#include <cstring>
+
+void String::free() {
+ delete[] str;
+}
+
+void String::copyFrom(const String& other) {
+ this->str = new char[strlen(other.str) + 1];
+ strcpy(this->str, other.str);
+}
+
+String::String() {
+ this->str = nullptr;
+}
+
+String::~String() {
+ free();
+}
+
+String::String(const String& other) {
+ copyFrom(other);
+}
+
+String& String::operator=(const String& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+String::String(String&& other) {
+ this->str = other.str;
+ other.str = nullptr;
+}
+
+String& String::operator=(String&& other) {
+ if (this != &other) {
+ free();
+
+ this->str = other.str;
+ other.str = nullptr;
+ }
+ return *this;
+}
+
+String::String(const char* str) {
+ this->str = new char[strlen(str) + 1];
+ strcpy(this->str, str);
+}
diff --git a/week13/Exercise1/String.h b/week13/Exercise1/String.h
new file mode 100644
index 0000000..b2f0ee9
--- /dev/null
+++ b/week13/Exercise1/String.h
@@ -0,0 +1,18 @@
+#pragma once
+
+class String {
+ char *str;
+
+ void free();
+ void copyFrom(const String& other);
+
+public:
+ String();
+ virtual ~String();
+ String(const String& other);
+ String& operator=(const String& other);
+ String(String&& other);
+ String& operator=(String&& other);
+
+ String(const char* str);
+};
diff --git a/week13/Exercise1/WordString.cpp b/week13/Exercise1/WordString.cpp
new file mode 100644
index 0000000..4ba3cca
--- /dev/null
+++ b/week13/Exercise1/WordString.cpp
@@ -0,0 +1,25 @@
+#include "WordString.h"
+
+bool isBlank(char ch) {
+ return ch == ' ' || ch == '\n' || ch == '\t';
+}
+
+WordString::WordString(const char* str) : String(str), IndexArray() {
+ this->size = 1;
+ for (int i = 1; str[i] != '\0'; i++) {
+ if (isBlank(str[i - 1]) && !isBlank(str[i])) {
+ this->size++;
+ }
+ }
+
+ this->indecies = new int[this->size];
+
+ this->indecies[0] = 0;
+ unsigned indeciesI = 1;
+
+ for (int i = 1; str[i] != '\0'; i++) {
+ if (isBlank(str[i - 1]) && !isBlank(str[i])) {
+ indecies[indeciesI++] = i;
+ }
+ }
+}
diff --git a/week13/Exercise1/WordString.h b/week13/Exercise1/WordString.h
new file mode 100644
index 0000000..fc91e3d
--- /dev/null
+++ b/week13/Exercise1/WordString.h
@@ -0,0 +1,8 @@
+#pragma once
+#include "String.h"
+#include "IndexArray.h"
+
+class WordString : public String, public IndexArray {
+public:
+ WordString(const char* str);
+};