From 88f15e35713c9632216931d26443dc588238732f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 7 May 2024 22:17:12 +0300 Subject: [w10] Added rough solutions to ex 1-10 --- week10/Exercise07/FileString.cpp | 43 ++++++++++++++++++++++++++ week10/Exercise07/FileString.h | 10 ++++++ week10/Exercise07/String.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ week10/Exercise07/String.h | 23 ++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 week10/Exercise07/FileString.cpp create mode 100644 week10/Exercise07/FileString.h create mode 100644 week10/Exercise07/String.cpp create mode 100644 week10/Exercise07/String.h (limited to 'week10/Exercise07') diff --git a/week10/Exercise07/FileString.cpp b/week10/Exercise07/FileString.cpp new file mode 100644 index 0000000..e3d601e --- /dev/null +++ b/week10/Exercise07/FileString.cpp @@ -0,0 +1,43 @@ +#include "FileString.h" +#include + +FileString::FileString(const char* fileName) : String() { + this->fileName = fileName; // Конвертиращ конструктор! + + std::ifstream inFile(fileName); + if (!inFile.is_open()) { + throw "Couldn't open file!"; + } + + while (!inFile.eof() && inFile.peek() != '\n') { + inFile.get(); + } + + if (inFile.eof()) { + inFile.seekg(0, std::ios::end); + this->length = inFile.peek(); + } + else { + this->length = inFile.peek() - 1; + } + + inFile.seekg(0, std::ios::beg); + str = new char[this->length + 1]; + inFile.getline(str, this->length); + str[this->length] = '\0'; + + inFile.close(); +} + +void FileString::ChangeAt(unsigned index, char newValue) { + std::fstream file(fileName.GetPtr()); + if (!file.is_open()) { + throw "Couldn't open file!"; + } + + file.seekp(index, std::ios::beg); + file.put(newValue); + str[index] = newValue; + + file.close(); +} diff --git a/week10/Exercise07/FileString.h b/week10/Exercise07/FileString.h new file mode 100644 index 0000000..830dae9 --- /dev/null +++ b/week10/Exercise07/FileString.h @@ -0,0 +1,10 @@ +#pragma once +#include "String.h" + +class FileString : String { + String fileName; // Така си спестяваме повторно писане на голяма петица + +public: + FileString(const char* fileName); + void ChangeAt(unsigned index, char newValue); +}; diff --git a/week10/Exercise07/String.cpp b/week10/Exercise07/String.cpp new file mode 100644 index 0000000..a9276c3 --- /dev/null +++ b/week10/Exercise07/String.cpp @@ -0,0 +1,67 @@ +#include "String.h" +#include + +void String::free() { + delete[] str; +} + +void String::copyFrom(const String& other) { + this->length = other.length; + this->str = new char[strlen(other.str) + 1]; + strcpy(this->str, other.str); +} + +String::String(const char* str) { + this->length = strlen(str); + this->str = new char[this->length + 1]; + strcpy(this->str, str); +} + +String::String() { + this->str = nullptr; + this->length = 0; +} + +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->length = other.length; + this->str = other.str; + other.str = nullptr; +} + +String& String::operator=(String&& other) { + if (this != &other) { + free(); + + this->length = other.length; + this->str = other.str; + other.str = nullptr; + } + return *this; +} + +char& String::At(unsigned index) { + if (index >= length) { + throw "Index too big!"; + } + return str[index]; +} + +const char* String::GetPtr() { + return str; +} diff --git a/week10/Exercise07/String.h b/week10/Exercise07/String.h new file mode 100644 index 0000000..3ea986a --- /dev/null +++ b/week10/Exercise07/String.h @@ -0,0 +1,23 @@ +#pragma once + +class String { +protected: + char* str; + unsigned length; + + void free(); + void copyFrom(const String& other); + +public: + String(const char* str); + + String(); + ~String(); + String(const String& other); + String& operator=(const String& other); + String(String&& other); + String& operator=(String&& other); + + char& At(unsigned index); + const char* GetPtr(); +}; -- cgit v1.2.3