From 112a0d344da9e66d20ee9ab5d488e59c5d52e6ad Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Thu, 9 Apr 2020 18:09:42 +0300 Subject: added new examples --- Classes/String/String.cpp | 194 +++++++++++++++++++++++++++++++++++++ Classes/String/String.h | 45 +++++++++ Classes/String/String.hpp | 240 ---------------------------------------------- 3 files changed, 239 insertions(+), 240 deletions(-) create mode 100644 Classes/String/String.cpp create mode 100644 Classes/String/String.h delete mode 100644 Classes/String/String.hpp (limited to 'Classes/String') diff --git a/Classes/String/String.cpp b/Classes/String/String.cpp new file mode 100644 index 0000000..f4f567b --- /dev/null +++ b/Classes/String/String.cpp @@ -0,0 +1,194 @@ +#include "String.h" + +void String::CopyFrom(const String& other) { + + str = new char[strlen(other.str) + 1]; + strcpy(str, other.str); + + len = other.len; +} + +void String::Free() { + delete[] str; +} + +String::String() { + + str = new char[1]; + str[0] = '\0'; + len = 0; +} + +inline String::String(char ch) +{ + str = new char[2]; + str[0] = ch; + str[1] = '\0'; + len = 1; +} + +String::String(const char* str) { + + len = strlen(str); + + this->str = new char[len + 1]; + strcpy(this->str, str); +} + +String::String(const String& other) { + CopyFrom(other); +} + +String& String::operator=(const String& other) { + + if (this != &other) { + Free(); + CopyFrom(other); + } + return *this; +} + +String::~String() { + Free(); +} + +int String::getLenght() const { + return len; +} + +String String::SubString(int start) { + + if (start < 0 || start > len) + throw std::logic_error("Invalid index.\n"); + + char* newStr = new char[len - start + 2]; + int j = 0; + for (int i = start; i < len; i++) + newStr[j++] = str[i]; + newStr[j] = '\0'; + + String newString(newStr); + delete[] newStr; + + return newString; +} + +String String::SubString(int start, int end) { + + if (start < 0 || start > len || end <= 0 || end > len) + throw std::logic_error("Invalid index.\n"); + + char* newStr = new char[end - start + 2]; + int j = 0; + for (int i = start; i <= end; i++) + newStr[j++] = str[i]; + newStr[j] = '\0'; + + String newString(newStr); + delete[] newStr; + + return newString; +} + +int String::IndexOf(const String& other, int index) { + + if (index < 0) + throw std::logic_error("Invalid index.\n"); + + int iter = 0; + for (int i = index; i <= len; i++) { + + if (iter == other.len) + return i - other.len; + if (str[i] != other.str[iter]) + iter = 0; + else + iter++; + } + return -1; +} + +int String::IndexOf(const char* str, int index) { + + String searched(str); + return IndexOf(searched, index); +} + +int String::CountOcurences(String str) +{ + int count = 0; + int i = IndexOf(str, 0); + while (i != -1) + { + count++; + i = IndexOf(str, i + 1); + } + return count; +} + +inline int String::GetOcurence(String str, int num) +{ + int count = 0; + int i = IndexOf(str, 0); + while (i != -1) + { + count++; + if (count == num) + return i; + i = IndexOf(str, i + 1); + } + return count; +} + +const char& String::operator[](int index) const { + + if (index >= len) + throw std::out_of_range("Out of range!"); + return str[index]; +} + +char& String::operator[](int index) { + return str[index]; +} + +String& String::operator+=(const String& other) { + + char* temp = str; + str = new char[len + other.len + 1]; + strcpy(str, temp); + strcat(str, other.str); + len += other.len; + delete[] temp; + return *this; +} + +bool operator==(const String& lhs, const String& rhs) { + return !strcmp(lhs.str, rhs.str); +} + +bool operator!=(const String& lhs, const String& rhs) { + return strcmp(lhs.str, rhs.str); +} + +std::ostream& operator<<(std::ostream& os, const String& obj) { + return os << obj.str; +} + +std::istream& operator>>(std::istream& is, String& obj) { + + char buff[MAX_SIZE]; + is.getline(buff, MAX_SIZE); + obj.str = new char[strlen(buff) + 1]; + strcpy(obj.str, buff); + + return is; +} + + +const String operator+(const String& lhs, const String& rhs) { + if (lhs.getLenght() == 0) + return rhs; + else if (rhs.getLenght() == 0) + return lhs; + return String(lhs) += rhs; +} diff --git a/Classes/String/String.h b/Classes/String/String.h new file mode 100644 index 0000000..37bbc6d --- /dev/null +++ b/Classes/String/String.h @@ -0,0 +1,45 @@ +#ifndef STRING_HDR +#define STRING_HDR + +#include +#include + +const int MAX_SIZE = 1024; + +class String { + +private: + char* str; + int len; + + void CopyFrom(const String& other); + void Free(); + +public: + String(); + String(char ch); + String(const char* str); + String(const String& other); + String& operator=(const String& other); + ~String(); + + int getLenght() const; + String SubString(int start); + String SubString(int start, int end); + int IndexOf(const String& other, int index); //return the index of the first encounter of other in the object + int IndexOf(const char* str, int index); //reusing IndexOf(const String& other, int index) + int CountOcurences(String str); // How many times a string is found as substring + int GetOcurence(String str, int i); //Get if the i-th substring (count start from 1) + + const char& operator[](int index) const; + char& operator[](int index); + String& operator+=(const String& other); //return the concatenation of the object with other + friend bool operator==(const String& lhs, const String& rhs); //return true if two strings are equivalent + friend bool operator!=(const String& lhs, const String& rhs); //return true if two strings are not equivalent + friend std::ostream& operator<<(std::ostream& os, const String& obj); + friend std::istream& operator>>(std::istream& is, String& obj); + + String* Split(String str); +}; +const String operator+(const String& lhs, const String& rhs); +#endif // !STRING_HDR \ No newline at end of file diff --git a/Classes/String/String.hpp b/Classes/String/String.hpp deleted file mode 100644 index b2c333f..0000000 --- a/Classes/String/String.hpp +++ /dev/null @@ -1,240 +0,0 @@ -#ifndef STRING_HDR -#define STRING_HDR - -#include -#include - -const int MAX_SIZE = 1024; - -class String { - -private: - char* str; - int len; - - void CopyFrom(const String& other); - void Free(); - -public: - String(); - String(char ch); - String(const char* str); - String(const String& other); - String& operator=(const String& other); - ~String(); - - int getLenght() const; - String SubString(int start); - String SubString(int start, int end); - int IndexOf(const String& other, int index); //return the index of the first encounter of other in the object - int IndexOf(const char* str, int index); //reusing IndexOf(const String& other, int index) - int CountOcurences(String str); // How many times a string is found as substring - int GetOcurence(String str,int i); //Get if the i-th substring (count start from 1) - - const char& operator[](int index) const; - char& operator[](int index); - String& operator+=(const String& other); //return the concatenation of the object with other - friend bool operator==(const String& lhs, const String& rhs); //return true if two strings are equivalent - friend bool operator!=(const String& lhs, const String& rhs); //return true if two strings are not equivalent - friend std::ostream& operator<<(std::ostream& os, const String& obj); - friend std::istream& operator>>(std::istream& is, String& obj); - - String* Split(String str); -}; -const String operator+(const String& lhs, const String& rhs); - - -void String::CopyFrom(const String& other) { - - str = new char[strlen(other.str) + 1]; - strcpy(str, other.str); - - len = other.len; -} - -void String::Free() { - delete[] str; -} - -String::String() { - - str = new char[1]; - str[0] = '\0'; - len = 0; -} - -inline String::String(char ch) -{ - str = new char[2]; - str[0] = ch; - str[1] = '\0'; - len = 1; -} - -String::String(const char* str) { - - len = strlen(str); - - this->str = new char[len + 1]; - strcpy(this->str, str); -} - -String::String(const String& other) { - CopyFrom(other); -} - -String& String::operator=(const String& other) { - - if (this != &other) { - Free(); - CopyFrom(other); - } - return *this; -} - -String::~String() { - Free(); -} - -int String::getLenght() const { - return len; -} - -String String::SubString(int start) { - - if (start < 0 || start > len) - throw std::logic_error("Invalid index.\n"); - - char* newStr = new char[len - start + 2]; - int j = 0; - for (int i = start; i < len; i++) - newStr[j++] = str[i]; - newStr[j] = '\0'; - - String newString(newStr); - delete[] newStr; - - return newString; -} - -String String::SubString(int start, int end) { - - if (start < 0 || start > len || end <= 0 || end > len) - throw std::logic_error("Invalid index.\n"); - - char* newStr = new char[end - start + 2]; - int j = 0; - for (int i = start; i <= end; i++) - newStr[j++] = str[i]; - newStr[j] = '\0'; - - String newString(newStr); - delete[] newStr; - - return newString; -} - -int String::IndexOf(const String& other, int index) { - - if(index < 0) - throw std::logic_error("Invalid index.\n"); - - int iter = 0; - for (int i = index; i <= len; i++) { - - if (iter == other.len) - return i - other.len; - if (str[i] != other.str[iter]) - iter = 0; - else - iter++; - } - return -1; -} - -int String::IndexOf(const char* str, int index) { - - String searched(str); - return IndexOf(searched, index); -} - -int String::CountOcurences(String str) -{ - int count = 0; - int i = IndexOf(str,0); - while (i!=-1) - { - count++; - i = IndexOf(str, i + 1); - } - return count; -} - -inline int String::GetOcurence(String str, int num) -{ - int count = 0; - int i = IndexOf(str, 0); - while (i != -1) - { - count++; - if (count == num) - return i; - i = IndexOf(str, i + 1); - } - return count; -} - -const char& String::operator[](int index) const { - - if(index >= len) - throw std::out_of_range("Out of range!"); - return str[index]; -} - -char& String::operator[](int index) { - return str[index]; -} - -String& String::operator+=(const String& other) { - - char* temp = str; - str = new char[len + other.len + 1]; - strcpy(str, temp); - strcat(str, other.str); - len += other.len; - delete[] temp; - return *this; -} - -bool operator==(const String& lhs, const String& rhs) { - return !strcmp(lhs.str, rhs.str); -} - -bool operator!=(const String& lhs, const String& rhs) { - return strcmp(lhs.str, rhs.str); -} - -std::ostream& operator<<(std::ostream& os, const String& obj) { - return os << obj.str; -} - -std::istream& operator>>(std::istream& is, String& obj) { - - char buff[MAX_SIZE]; - is.getline(buff, MAX_SIZE); - obj.str = new char[strlen(buff) + 1]; - strcpy(obj.str, buff); - - return is; -} - - -const String operator+(const String& lhs, const String& rhs) { - if (lhs.getLenght() == 0) - return rhs; - else if (rhs.getLenght() == 0) - return lhs; - return String(lhs) += rhs; -} - -#endif // !STRING_HDR -- cgit v1.3.1-23-gd82d