From 9c11fababb9b6b194e646fbeb62d141aacf74c43 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 25 May 2024 16:08:17 +0300 Subject: [w13] Added solutions --- week13/Exercise1/IndexArray.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ week13/Exercise1/IndexArray.h | 18 +++++++++++++++ week13/Exercise1/String.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ week13/Exercise1/String.h | 18 +++++++++++++++ week13/Exercise1/WordString.cpp | 25 ++++++++++++++++++++ week13/Exercise1/WordString.h | 8 +++++++ 6 files changed, 171 insertions(+) create mode 100644 week13/Exercise1/IndexArray.cpp create mode 100644 week13/Exercise1/IndexArray.h create mode 100644 week13/Exercise1/String.cpp create mode 100644 week13/Exercise1/String.h create mode 100644 week13/Exercise1/WordString.cpp create mode 100644 week13/Exercise1/WordString.h (limited to 'week13/Exercise1') 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 + +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 + +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); +}; -- cgit v1.2.3