From be6b34bd229c3049d8d29f5af0cfdaec2a67cba4 Mon Sep 17 00:00:00 2001 From: Andrey Kassaev Date: Sun, 3 Mar 2024 22:15:25 +0400 Subject: [PATCH] init --- .gitignore | 2 ++ Main.cpp | 17 +++++++++++++ MyString.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ MyString.h | 22 ++++++++++++++++ filename.txt | 2 ++ 5 files changed, 115 insertions(+) create mode 100644 .gitignore create mode 100644 Main.cpp create mode 100644 MyString.cpp create mode 100644 MyString.h create mode 100644 filename.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6df1069 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*/* +OOP* \ No newline at end of file diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..7cae82d --- /dev/null +++ b/Main.cpp @@ -0,0 +1,17 @@ +#include +#include "MyString.h" + +int main() { + + MyString myString1; + myString1.set("hello"); + myString1.print(); + + MyString myString2(myString1); + myString2.print(); + myString2.update(); + + system("pause"); + + return 0; +}; \ No newline at end of file diff --git a/MyString.cpp b/MyString.cpp new file mode 100644 index 0000000..58a2b98 --- /dev/null +++ b/MyString.cpp @@ -0,0 +1,72 @@ +#include "MyString.h" +#include + +MyString::MyString() { + cout << "<>\n"; + this->str = new char('\0'); +}; + +MyString::MyString(const char* value) { + cout << "<>\n"; + this->str = new char[strlen(value) + 1]; + strcpy_s(str, strlen(value) + 1, value); + this->str[strlen(value)] = '\0'; +}; + +MyString::MyString(const MyString& obj) { + cout << "<>\n"; + this->str = new char[strlen(obj.str) + 1]; + strcpy_s(str, strlen(obj.str) + 1, obj.str); + this->str[strlen(obj.str)] = '\0'; +}; + +MyString::~MyString() { + cout << "<>\n"; + delete[] this->str; +}; + +char* MyString::getString() { + cout << "<\n"; + + return this->str; +}; + +void MyString::set(const char* value) { + cout << "<>\n"; + + delete[] this->str; + + this->str = new char[strlen(value) + 1]; + strcpy_s(str, strlen(value) + 1, value); + this->str[strlen(value)] = '\0'; +}; + +void MyString::print() { + cout << "<>\n"; + + cout << this->str << endl; +}; + +void MyString::update() { + cout << "<>\n"; + writeToFile(); + if (strlen(this->str) % 2 != 0) + { + int middleIndex = strlen(this->str) / 2; + + for (size_t i = middleIndex; i < strlen(this->str); i++) + { + this->str[i] = this->str[i+1]; + } + this->str[strlen(this->str)] = '\0'; + writeToFile(); + } +}; + +void MyString::writeToFile() { + cout << "<>\n"; + + ofstream MyFile("filename.txt", ios_base::app); + MyFile << this->str << "\n"; + MyFile.close(); +}; \ No newline at end of file diff --git a/MyString.h b/MyString.h new file mode 100644 index 0000000..61643ac --- /dev/null +++ b/MyString.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include +using namespace std; + +class MyString +{ +private: + char* str; + +public: + + MyString(); + MyString(const char* value); + MyString(const MyString&); + ~MyString(); + char* getString(); + void set(const char* value); + void print(); + void update(); + void writeToFile(); +}; \ No newline at end of file diff --git a/filename.txt b/filename.txt new file mode 100644 index 0000000..7d75d96 --- /dev/null +++ b/filename.txt @@ -0,0 +1,2 @@ +hello +helo