aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise10
diff options
context:
space:
mode:
Diffstat (limited to 'week10/Exercise10')
-rw-r--r--week10/Exercise10/ModifiableString.cpp14
-rw-r--r--week10/Exercise10/ModifiableString.h9
-rw-r--r--week10/Exercise10/SaveableString.cpp32
-rw-r--r--week10/Exercise10/SaveableString.h8
-rw-r--r--week10/Exercise10/ShowableString.cpp9
-rw-r--r--week10/Exercise10/ShowableString.h9
-rw-r--r--week10/Exercise10/String.cpp71
-rw-r--r--week10/Exercise10/String.h21
8 files changed, 173 insertions, 0 deletions
diff --git a/week10/Exercise10/ModifiableString.cpp b/week10/Exercise10/ModifiableString.cpp
new file mode 100644
index 0000000..bd42088
--- /dev/null
+++ b/week10/Exercise10/ModifiableString.cpp
@@ -0,0 +1,14 @@
+#include "ModifiableString.h"
+
+char& ModifiableString::operator[](int index) {
+ return str[index];
+}
+
+char& ModifiableString::operator[](int index) const {
+ return str[index];
+}
+
+ModifiableString& ModifiableString::operator+=(const ModifiableString& right) {
+ String::operator=(*this + right);
+ return *this;
+}
diff --git a/week10/Exercise10/ModifiableString.h b/week10/Exercise10/ModifiableString.h
new file mode 100644
index 0000000..971ceb8
--- /dev/null
+++ b/week10/Exercise10/ModifiableString.h
@@ -0,0 +1,9 @@
+#pragma once
+#include "String.h"
+
+class ModifiableString : protected String {
+public:
+ char& operator[](int index);
+ char& operator[](int index) const;
+ ModifiableString& operator+=(const ModifiableString& right);
+};
diff --git a/week10/Exercise10/SaveableString.cpp b/week10/Exercise10/SaveableString.cpp
new file mode 100644
index 0000000..4fa41e8
--- /dev/null
+++ b/week10/Exercise10/SaveableString.cpp
@@ -0,0 +1,32 @@
+#include "SaveableString.h"
+#include <cstring>
+#include <fstream>
+
+void SaveableString::write(const char* fileName) {
+ std::ofstream outFile(fileName);
+ if (!outFile.is_open()) {
+ throw "Coudln't open file!";
+ }
+
+ outFile.write(fileName, sizeof(char) * strlen(str));
+
+ outFile.close();
+}
+
+void SaveableString::read(const char* fileName) {
+ std::ifstream inFile(fileName);
+ if (!inFile.is_open()) {
+ throw "Coudln't open file!";
+ }
+
+ inFile.seekg(0, std::ios::end);
+ unsigned length = inFile.tellg();
+ inFile.seekg(0, std::ios::beg);
+
+ free();
+ str = new char[length + 1];
+ inFile.read(str, sizeof(char) * length);
+ str[length] = '\0';
+
+ inFile.close();
+}
diff --git a/week10/Exercise10/SaveableString.h b/week10/Exercise10/SaveableString.h
new file mode 100644
index 0000000..3952c27
--- /dev/null
+++ b/week10/Exercise10/SaveableString.h
@@ -0,0 +1,8 @@
+#pragma once
+#include "ModifiableString.h"
+
+class SaveableString : ModifiableString {
+public:
+ void write(const char* fileName);
+ void read(const char* fileName);
+};
diff --git a/week10/Exercise10/ShowableString.cpp b/week10/Exercise10/ShowableString.cpp
new file mode 100644
index 0000000..1231a9c
--- /dev/null
+++ b/week10/Exercise10/ShowableString.cpp
@@ -0,0 +1,9 @@
+#include "ShowableString.h"
+
+std::ostream& operator<<(std::ostream& ostr, const ShowableString& str) {
+ return ostr << str;
+}
+
+std::istream& operator>>(std::istream& istr, const ShowableString& str) {
+ return istr >> str; // Лошо!
+}
diff --git a/week10/Exercise10/ShowableString.h b/week10/Exercise10/ShowableString.h
new file mode 100644
index 0000000..ff46f6c
--- /dev/null
+++ b/week10/Exercise10/ShowableString.h
@@ -0,0 +1,9 @@
+#pragma once
+#include "ModifiableString.h"
+#include <iostream>
+
+class ShowableString : ModifiableString {
+public:
+ friend std::ostream& operator<<(std::ostream& ostr, const ShowableString& str);
+ friend std::istream& operator>>(std::istream& istr, const ShowableString& str);
+};
diff --git a/week10/Exercise10/String.cpp b/week10/Exercise10/String.cpp
new file mode 100644
index 0000000..7fd689f
--- /dev/null
+++ b/week10/Exercise10/String.cpp
@@ -0,0 +1,71 @@
+#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() {
+ 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;
+}
+
+String& String::operator=(String&& other) {
+ if (this != &other) {
+ free();
+
+ this->str = other.str;
+ }
+ return *this;
+}
+
+bool operator==(const String& left, const String& right) {
+ int i;
+ for (i = 0; left.str[i] != '\0' && right.str[i] != '\0'; i++) {
+ if (left.str[i] != right.str[i])
+ return false;
+ }
+ return left.str[i] == '\0' && right.str[i] == '\0';
+}
+
+bool operator!=(const String& left, const String& right) {
+ return !(left == right);
+}
+
+String operator+(const String& left, const String& right) {
+ String concat;
+ concat.str = new char[strlen(left.str) + strlen(right.str) + 1];
+ int i;
+ for (i = 0; left.str[i] != '\0'; i++) {
+ concat.str[i] = left.str[i];
+ }
+ int j;
+ for (j = 0; right.str[j] != '\0'; j++) {
+ concat.str[i+j] = right.str[j];
+ }
+ return concat;
+}
diff --git a/week10/Exercise10/String.h b/week10/Exercise10/String.h
new file mode 100644
index 0000000..4ae55c0
--- /dev/null
+++ b/week10/Exercise10/String.h
@@ -0,0 +1,21 @@
+#pragma once
+
+class String {
+protected:
+ char* str;
+
+ void free();
+ void copyFrom(const String& other);
+
+public:
+ String();
+ ~String();
+ String(const String& other);
+ String& operator=(const String& other);
+ String(String&& other);
+ String& operator=(String&& other);
+
+ friend bool operator==(const String& left, const String& right);
+ friend bool operator!=(const String& left, const String& right);
+ friend String operator+(const String& left, const String& right);
+};