From 7da5cae134b3f5dc0e3bc6641f2ae5364fa5f512 Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 24 May 2022 00:13:26 +0300 Subject: added check to MyString for length when input comes for a stream --- Sem. 09/MyString (with move)/MyString.cpp | 43 +++++++++++++++++++++------- Sem. 09/MyString (with move)/MyString.h | 10 +++---- Sem. 09/StringPool/MyString/MyString.cpp | 45 ++++++++++++++++++++++-------- Sem. 09/StringPool/MyString/MyString.h | 9 +++--- Sem. 10/Examples/MyString/MyString.cpp | 17 +++++------ Sem. 10/Examples/MyString/MyString.h | 3 +- Sem. 11/Examples/Bar/MyString/MyString.cpp | 44 ++++++++++++++++++++++------- Sem. 11/Examples/Bar/MyString/MyString.h | 9 +++--- 8 files changed, 122 insertions(+), 58 deletions(-) diff --git a/Sem. 09/MyString (with move)/MyString.cpp b/Sem. 09/MyString (with move)/MyString.cpp index 1ca4762..9ce6701 100644 --- a/Sem. 09/MyString (with move)/MyString.cpp +++ b/Sem. 09/MyString (with move)/MyString.cpp @@ -1,6 +1,7 @@ #include "MyString.h" #include #include +#include void MyString::copyFrom(const MyString& other) { @@ -13,13 +14,6 @@ void MyString::free() delete[] str; } -MyString::MyString() -{ - str = new char[1]; - str[0] = '\0'; - size = 0; -} - MyString::MyString(const char* data) { if(data == nullptr) @@ -36,6 +30,35 @@ MyString::MyString(const char* data) } } +size_t getNumSize(size_t num) +{ + size_t res = 0; + while (num) + { + res++; + num /= 10; + } + return res; +} +MyString::MyString(size_t n) +{ + int numSize = getNumSize(n); + str = new char[numSize + 1]; + + str[numSize] = '\0'; + + for (size_t i = 0; i < numSize; i++, n /= 10) + str[numSize - 1 - i] = (n % 10) + '0'; + size = numSize; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 1; +} + MyString::MyString(const MyString& other) { copyFrom(other); @@ -59,7 +82,7 @@ size_t MyString::getSize() const { return size; } -void MyString::concat(const MyString& other) +void MyString::concatFrom(const MyString& other) { char* temp = new char[getSize() + other.getSize() + 1]; strcpy(temp, str); @@ -77,7 +100,7 @@ const char* MyString::c_str() const MyString& MyString::operator+=(const MyString& other) { - concat(other); + concatFrom(other); return *this; } @@ -127,11 +150,11 @@ MyString::MyString(MyString&& otherString) str = otherString.str; size = otherString.size; otherString.str = nullptr; - std::cout << "move constr" << std::endl; } MyString& MyString::operator=(MyString&& otherString) { + if (this != &otherString) { free(); diff --git a/Sem. 09/MyString (with move)/MyString.h b/Sem. 09/MyString (with move)/MyString.h index 57c298a..9e300d6 100644 --- a/Sem. 09/MyString (with move)/MyString.h +++ b/Sem. 09/MyString (with move)/MyString.h @@ -8,15 +8,16 @@ class MyString void copyFrom(const MyString& other); void free(); - - void concat(const MyString& other); + + void concatFrom(const MyString& other); public: MyString(); MyString(const char*); + MyString(size_t n); //convert constructor MyString(const MyString&); - + MyString(MyString&&); //move constructor that accepts rvalue; MyString& operator=(const MyString& other); @@ -40,6 +41,3 @@ MyString operator+(const MyString& lhs, const MyString& rhs); bool operator==(const MyString& lhs, const MyString& rhs); bool operator<=(const MyString& lhs, const MyString& rhs); bool operator<(const MyString& lhs, const MyString& rhs); - - -//MyString name = "sdfsdf" diff --git a/Sem. 09/StringPool/MyString/MyString.cpp b/Sem. 09/StringPool/MyString/MyString.cpp index cad878a..ca5f738 100644 --- a/Sem. 09/StringPool/MyString/MyString.cpp +++ b/Sem. 09/StringPool/MyString/MyString.cpp @@ -1,6 +1,7 @@ #include "MyString.h" #include #include +#include void MyString::copyFrom(const MyString& other) { @@ -13,13 +14,6 @@ void MyString::free() delete[] str; } -MyString::MyString() -{ - str = new char[1]; - str[0] = '\0'; - size = 0; -} - MyString::MyString(const char* data) { if(data == nullptr) @@ -36,6 +30,35 @@ MyString::MyString(const char* data) } } +size_t getNumSize(size_t num) +{ + size_t res = 0; + while (num) + { + res++; + num /= 10; + } + return res; +} +MyString::MyString(size_t n) +{ + int numSize = getNumSize(n); + str = new char[numSize + 1]; + + str[numSize] = '\0'; + + for (size_t i = 0; i < numSize; i++, n /= 10) + str[numSize - 1 - i] = (n % 10) + '0'; + size = numSize; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 1; +} + MyString::MyString(const MyString& other) { copyFrom(other); @@ -59,7 +82,7 @@ size_t MyString::getSize() const { return size; } -void MyString::concat(const MyString& other) +void MyString::concatFrom(const MyString& other) { char* temp = new char[getSize() + other.getSize() + 1]; strcpy(temp, str); @@ -77,7 +100,7 @@ const char* MyString::c_str() const MyString& MyString::operator+=(const MyString& other) { - concat(other); + concatFrom(other); return *this; } @@ -98,7 +121,7 @@ std::istream& operator>>(std::istream& stream, MyString& str) { delete[] str.str; char buff[1024]; - stream >> buff; + stream >> std::setw(1024) >> buff; str.size = strlen(buff); str.str = new char[str.size + 1]; @@ -127,11 +150,11 @@ MyString::MyString(MyString&& otherString) str = otherString.str; size = otherString.size; otherString.str = nullptr; - std::cout << "move constr" << std::endl; } MyString& MyString::operator=(MyString&& otherString) { + if (this != &otherString) { free(); diff --git a/Sem. 09/StringPool/MyString/MyString.h b/Sem. 09/StringPool/MyString/MyString.h index cabc311..15f7b55 100644 --- a/Sem. 09/StringPool/MyString/MyString.h +++ b/Sem. 09/StringPool/MyString/MyString.h @@ -9,12 +9,15 @@ class MyString void copyFrom(const MyString& other); void free(); + void concatFrom(const MyString& other); + public: MyString(); MyString(const char*); + MyString(size_t n); //convert constructor MyString(const MyString&); - + MyString(MyString&&); //move constructor that accepts rvalue; MyString& operator=(const MyString& other); @@ -23,7 +26,6 @@ public: ~MyString(); size_t getSize() const; - void concat(const MyString& other); const char* c_str() const; @@ -39,6 +41,3 @@ MyString operator+(const MyString& lhs, const MyString& rhs); bool operator==(const MyString& lhs, const MyString& rhs); bool operator<=(const MyString& lhs, const MyString& rhs); bool operator<(const MyString& lhs, const MyString& rhs); - - -//MyString name = "sdfsdf" \ No newline at end of file diff --git a/Sem. 10/Examples/MyString/MyString.cpp b/Sem. 10/Examples/MyString/MyString.cpp index e404d7f..9ce6701 100644 --- a/Sem. 10/Examples/MyString/MyString.cpp +++ b/Sem. 10/Examples/MyString/MyString.cpp @@ -1,6 +1,7 @@ #include "MyString.h" #include #include +#include void MyString::copyFrom(const MyString& other) { @@ -51,11 +52,11 @@ MyString::MyString(size_t n) size = numSize; } -MyString::MyString(const char* data) +MyString::MyString() { - size = strlen(data); - str = new char[size + 1]; - strcpy(str, data); + str = new char[1]; + str[0] = '\0'; + size = 1; } MyString::MyString(const MyString& other) @@ -64,8 +65,6 @@ MyString::MyString(const MyString& other) } MyString& MyString::operator=(const MyString& other) { - std::cout << "op=" << std::endl; - if (this != &other) { free(); @@ -83,7 +82,7 @@ size_t MyString::getSize() const { return size; } -void MyString::concat(const MyString& other) +void MyString::concatFrom(const MyString& other) { char* temp = new char[getSize() + other.getSize() + 1]; strcpy(temp, str); @@ -101,7 +100,7 @@ const char* MyString::c_str() const MyString& MyString::operator+=(const MyString& other) { - concat(other); + concatFrom(other); return *this; } @@ -151,12 +150,10 @@ MyString::MyString(MyString&& otherString) str = otherString.str; size = otherString.size; otherString.str = nullptr; - std::cout << "move constr" << std::endl; } MyString& MyString::operator=(MyString&& otherString) { - std::cout << "move op=" << std::endl; if (this != &otherString) { diff --git a/Sem. 10/Examples/MyString/MyString.h b/Sem. 10/Examples/MyString/MyString.h index 17f9a01..9e300d6 100644 --- a/Sem. 10/Examples/MyString/MyString.h +++ b/Sem. 10/Examples/MyString/MyString.h @@ -9,6 +9,8 @@ class MyString void copyFrom(const MyString& other); void free(); + void concatFrom(const MyString& other); + public: MyString(); MyString(const char*); @@ -24,7 +26,6 @@ public: ~MyString(); size_t getSize() const; - void concat(const MyString& other); const char* c_str() const; diff --git a/Sem. 11/Examples/Bar/MyString/MyString.cpp b/Sem. 11/Examples/Bar/MyString/MyString.cpp index 3e60278..9ce6701 100644 --- a/Sem. 11/Examples/Bar/MyString/MyString.cpp +++ b/Sem. 11/Examples/Bar/MyString/MyString.cpp @@ -1,6 +1,7 @@ #include "MyString.h" #include #include +#include void MyString::copyFrom(const MyString& other) { @@ -13,13 +14,6 @@ void MyString::free() delete[] str; } -MyString::MyString() -{ - str = new char[1]; - str[0] = '\0'; - size = 0; -} - MyString::MyString(const char* data) { if(data == nullptr) @@ -36,6 +30,35 @@ MyString::MyString(const char* data) } } +size_t getNumSize(size_t num) +{ + size_t res = 0; + while (num) + { + res++; + num /= 10; + } + return res; +} +MyString::MyString(size_t n) +{ + int numSize = getNumSize(n); + str = new char[numSize + 1]; + + str[numSize] = '\0'; + + for (size_t i = 0; i < numSize; i++, n /= 10) + str[numSize - 1 - i] = (n % 10) + '0'; + size = numSize; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 1; +} + MyString::MyString(const MyString& other) { copyFrom(other); @@ -59,7 +82,7 @@ size_t MyString::getSize() const { return size; } -void MyString::concat(const MyString& other) +void MyString::concatFrom(const MyString& other) { char* temp = new char[getSize() + other.getSize() + 1]; strcpy(temp, str); @@ -77,7 +100,7 @@ const char* MyString::c_str() const MyString& MyString::operator+=(const MyString& other) { - concat(other); + concatFrom(other); return *this; } @@ -98,7 +121,7 @@ std::istream& operator>>(std::istream& stream, MyString& str) { delete[] str.str; char buff[1024]; - stream >> buff; + stream >> std::setw(1024) >> buff; str.size = strlen(buff); str.str = new char[str.size + 1]; @@ -131,6 +154,7 @@ MyString::MyString(MyString&& otherString) MyString& MyString::operator=(MyString&& otherString) { + if (this != &otherString) { free(); diff --git a/Sem. 11/Examples/Bar/MyString/MyString.h b/Sem. 11/Examples/Bar/MyString/MyString.h index b37733a..9e300d6 100644 --- a/Sem. 11/Examples/Bar/MyString/MyString.h +++ b/Sem. 11/Examples/Bar/MyString/MyString.h @@ -9,12 +9,15 @@ class MyString void copyFrom(const MyString& other); void free(); + void concatFrom(const MyString& other); + public: MyString(); MyString(const char*); + MyString(size_t n); //convert constructor MyString(const MyString&); - + MyString(MyString&&); //move constructor that accepts rvalue; MyString& operator=(const MyString& other); @@ -23,7 +26,6 @@ public: ~MyString(); size_t getSize() const; - void concat(const MyString& other); const char* c_str() const; @@ -39,6 +41,3 @@ MyString operator+(const MyString& lhs, const MyString& rhs); bool operator==(const MyString& lhs, const MyString& rhs); bool operator<=(const MyString& lhs, const MyString& rhs); bool operator<(const MyString& lhs, const MyString& rhs); - - -//MyString name = "sdfsdf" \ No newline at end of file -- cgit v1.3.1-23-gd82d