From 571dc57f812a685bda7bd3baf8aaf2ef56feae65 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 28 Mar 2026 16:19:58 +0200 Subject: feat(Week05): Add solutions --- Week05/Exercise02/Text.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Week05/Exercise02/Text.cpp (limited to 'Week05/Exercise02/Text.cpp') diff --git a/Week05/Exercise02/Text.cpp b/Week05/Exercise02/Text.cpp new file mode 100644 index 0000000..b6143c0 --- /dev/null +++ b/Week05/Exercise02/Text.cpp @@ -0,0 +1,67 @@ +#include "Text.h" + +void Text::free() { + delete[] lines; +} + +void Text::copyFrom(const Text& other) { + lineCount = other.lineCount; + + lines = new String[lineCount]; + for (unsigned i = 0; i < lineCount; ++i) + lines[i] = other.lines[i]; +} + +Text::Text() { + lines = nullptr; + lineCount = 0; +} + +Text::~Text() { + free(); +} + +Text::Text(const Text& other) { + copyFrom(other); +} + +Text& Text::operator=(const Text& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Text::Text(Text&& other) { + lines = other.lines; + lineCount = other.lineCount; + + other.lines = nullptr; + other.lineCount = 0; +} + +unsigned Text::LineCount() const { + return lineCount; +} + +unsigned Text::WordCount() const { + unsigned totalCount = 0; + + for (unsigned i = 0; i < lineCount; ++i) + totalCount += lines[i].CountWords(); + + return totalCount; +} + +Text Text::SubText(unsigned startLine, unsigned startCol, unsigned endLine, unsigned endCol) const { + Text result; + result.lineCount = endLine - startLine + 1; + result.lines = new String[lineCount]; + + for (unsigned i = 0; i < result.lineCount; ++i) { + result.lines[i] = lines[i + startLine].Substring(startCol, endCol - startCol + 1); + } + + return result; +} -- cgit v1.3.1-23-gd82d