aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise07
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-07 22:17:12 +0300
committerSyndamia <kamen@syndamia.com>2024-05-07 22:17:12 +0300
commit88f15e35713c9632216931d26443dc588238732f (patch)
tree147ff8c72009afc58d3eabb1c026a33b798f20fe /week10/Exercise07
parentf4642325f58172e85b9e41e35f69e8bea46f78c6 (diff)
downloadoop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar.gz
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.zip
[w10] Added rough solutions to ex 1-10
Diffstat (limited to 'week10/Exercise07')
-rw-r--r--week10/Exercise07/FileString.cpp43
-rw-r--r--week10/Exercise07/FileString.h10
-rw-r--r--week10/Exercise07/String.cpp67
-rw-r--r--week10/Exercise07/String.h23
4 files changed, 143 insertions, 0 deletions
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 <fstream>
+
+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 <cstring>
+
+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();
+};