Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- #include <cstring>
- #include <string>
- const int INITSIZE = 4;
- class CString
- {
- public:
- CString() : string(new char[INITSIZE]), buffSize(INITSIZE), realSize(0)
- {
- }
- CString(size_t buffsize);
- CString(const char* str);
- CString(const std::string &str) : CString(str.c_str())
- {
- }
- ~CString();
- void Add(char value);
- void AddSubStr(char* value, int start);
- void DelSubStr(int start, int finish);
- void PrintStr() const;
- void operator + (char* s);
- bool operator == (char* s) const;
- void operator = (char* s);
- char operator [] (int t) const;
- private:
- char* string;
- size_t buffSize;
- size_t realSize;
- };
- CString::CString(size_t buffsize)
- : buffSize(buffsize)
- {
- string = new char[buffsize];
- realSize = 0;
- }
- CString::CString(const char* str) : string(new char[strlen(str)+1]), buffSize(strlen(str)), realSize(buffSize)
- {
- memcpy(string, str, buffSize);
- string[realSize] = '\0';
- }
- CString::~CString()
- {
- delete[] string;
- }
- void CString::Add(char value)
- {
- if (realSize == buffSize)
- {
- buffSize *= 2;
- char* buffer = new char[buffSize];
- memcpy(buffer, string, realSize);
- delete[] string;
- string = buffer;
- }
- string[realSize] = value;
- realSize++;
- string[realSize] = '\0';
- }
- void CString::AddSubStr(char* value, int start)
- {
- size_t a = strlen(value);
- if (buffSize < realSize + a)
- {
- buffSize = realSize + a;
- char* buffer = new char[buffSize + 1];
- memcpy(buffer, string, realSize);
- delete[] string;
- string = buffer;
- }
- char* buff = new char[realSize - start];
- memmove(buff, string + start + 1, realSize - start - 1);
- memmove(string + start + 1, value, a);
- memmove(string + start + a + 1, buff, realSize - start - 1);
- delete[] buff;
- realSize += a;
- string[realSize] = '\0';
- }
- void CString::DelSubStr(int start, int finish)
- {
- int lenght = finish - start + 1;
- char* buffer = new char[realSize - lenght + 1];
- memmove(buffer, string, start);
- memmove(buffer + start, string + finish + 1, realSize - finish - 1);
- delete[] string;
- string = buffer;
- realSize = realSize - lenght;
- buffSize = realSize;
- string[realSize] = '\0';
- }
- void CString::PrintStr() const
- {
- for (int i = 0; i < realSize; i++)
- std::cout << string[i];
- std::cout << "\n";
- }
- void CString::operator + (char* value)
- {
- if (buffSize < realSize + strlen(value))
- {
- buffSize = realSize + strlen(value);
- char* buffer = new char[buffSize + 1];
- memcpy(buffer, string, realSize);
- delete[] string;
- string = buffer;
- }
- memmove(string + realSize, value, strlen(value));
- realSize += strlen(value);
- string[realSize] = '\0';
- }
- bool CString::operator == (char* s) const
- {
- int a = strlen(s);
- if (a != realSize)
- return false;
- for (int i = 0; i < a; i++)
- {
- if (s[i] != string[i])
- return false;
- }
- return true;
- }
- void CString::operator = (char* s)
- {
- size_t a = strlen(s);
- char* buffer = new char[a + 1];
- memcpy(buffer, s, a);
- delete[] string;
- string = buffer;
- buffSize = a;
- realSize = a;
- string[realSize] = '\0';
- }
- char CString::operator [] (int i) const
- {
- return string[i];
- }
- int main()
- {
- char S[] = "raz, ";
- CString s(S);
- char a[] = "dva";
- char v = 's';
- s.PrintStr();
- s + a;
- s.PrintStr();
- s = a;
- s.PrintStr();
- if (s == a)
- std::cout << "true\n";
- else
- std::cout << "false\n";
- std::cout << s[1] << "\n";
- s.Add(v);
- s.PrintStr();
- s.AddSubStr(a, 2);
- s.PrintStr();
- s.DelSubStr(3, 5);
- s.PrintStr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment