loulett

HW4_strings

Feb 16th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6.  
  7. const int INITSIZE = 4;
  8.  
  9. class CString
  10. {
  11. public:
  12.     CString() : string(new char[INITSIZE]), buffSize(INITSIZE), realSize(0)
  13.     {
  14.     }
  15.  
  16.     CString(size_t buffsize);
  17.     CString(const char* str);
  18.  
  19.     CString(const std::string &str) : CString(str.c_str())
  20.     {
  21.     }
  22.  
  23.     ~CString();
  24.     void Add(char value);
  25.     void AddSubStr(char* value, int start);
  26.     void DelSubStr(int start, int finish);
  27.     void PrintStr() const;
  28.     void operator + (char* s);
  29.     bool operator == (char* s) const;
  30.     void operator = (char* s);
  31.     char operator [] (int t) const;
  32.  
  33. private:
  34.     char* string;
  35.     size_t buffSize;
  36.     size_t realSize;
  37. };
  38.  
  39.  
  40. CString::CString(size_t buffsize)
  41. : buffSize(buffsize)
  42. {
  43.     string = new char[buffsize];
  44.     realSize = 0;
  45. }
  46.  
  47. CString::CString(const char* str) : string(new char[strlen(str)+1]), buffSize(strlen(str)), realSize(buffSize)
  48. {
  49.     memcpy(string, str, buffSize);
  50.     string[realSize] = '\0';
  51. }
  52.  
  53. CString::~CString()
  54. {
  55.     delete[] string;
  56. }
  57.  
  58.  
  59. void CString::Add(char value)
  60. {
  61.     if (realSize == buffSize)
  62.     {
  63.         buffSize *= 2;
  64.         char* buffer = new char[buffSize];
  65.         memcpy(buffer, string, realSize);
  66.         delete[] string;
  67.         string = buffer;
  68.     }
  69.     string[realSize] = value;
  70.     realSize++;
  71.     string[realSize] = '\0';
  72. }
  73.  
  74.  
  75. void CString::AddSubStr(char* value, int start)
  76. {
  77.     size_t a = strlen(value);
  78.     if (buffSize < realSize + a)
  79.     {
  80.         buffSize = realSize + a;
  81.         char* buffer = new char[buffSize + 1];
  82.         memcpy(buffer, string, realSize);
  83.         delete[] string;
  84.         string = buffer;
  85.     }
  86.     char* buff = new char[realSize - start];
  87.     memmove(buff, string + start + 1, realSize - start - 1);
  88.     memmove(string + start + 1, value, a);
  89.     memmove(string + start + a + 1, buff, realSize - start - 1);
  90.     delete[] buff;
  91.     realSize += a;
  92.     string[realSize] = '\0';
  93. }
  94.  
  95. void CString::DelSubStr(int start, int finish)
  96. {
  97.     int lenght = finish - start + 1;
  98.     char* buffer = new char[realSize - lenght + 1];
  99.     memmove(buffer, string, start);
  100.     memmove(buffer + start, string + finish + 1, realSize - finish - 1);
  101.     delete[] string;
  102.     string = buffer;
  103.     realSize = realSize - lenght;
  104.     buffSize = realSize;
  105.     string[realSize] = '\0';
  106. }
  107.  
  108. void CString::PrintStr() const
  109. {
  110.     for (int i = 0; i < realSize; i++)
  111.         std::cout << string[i];
  112.     std::cout << "\n";
  113. }
  114.  
  115.  
  116. void CString::operator + (char* value)
  117. {
  118.     if (buffSize < realSize + strlen(value))
  119.     {
  120.         buffSize = realSize + strlen(value);
  121.         char* buffer = new char[buffSize + 1];
  122.         memcpy(buffer, string, realSize);
  123.         delete[] string;
  124.         string = buffer;
  125.     }
  126.     memmove(string + realSize, value, strlen(value));
  127.     realSize += strlen(value);
  128.     string[realSize] = '\0';
  129. }
  130.  
  131. bool CString::operator == (char* s) const
  132. {
  133.     int a = strlen(s);
  134.     if (a != realSize)
  135.         return false;
  136.     for (int i = 0; i < a; i++)
  137.     {
  138.         if (s[i] != string[i])
  139.             return false;
  140.     }
  141.     return true;
  142. }
  143.  
  144. void CString::operator = (char* s)
  145. {
  146.     size_t a = strlen(s);
  147.     char* buffer = new char[a + 1];
  148.     memcpy(buffer, s, a);
  149.     delete[] string;
  150.     string = buffer;
  151.     buffSize = a;
  152.     realSize = a;
  153.     string[realSize] = '\0';
  154. }
  155.  
  156. char CString::operator [] (int i) const
  157. {
  158.     return string[i];
  159. }
  160.  
  161. int main()
  162. {
  163.     char S[] = "raz, ";
  164.     CString s(S);
  165.     char a[] = "dva";
  166.     char v = 's';
  167.     s.PrintStr();
  168.     s + a;
  169.     s.PrintStr();
  170.     s = a;
  171.     s.PrintStr();
  172.     if (s == a)
  173.         std::cout << "true\n";
  174.     else
  175.         std::cout << "false\n";
  176.     std::cout << s[1] << "\n";
  177.     s.Add(v);
  178.     s.PrintStr();
  179.     s.AddSubStr(a, 2);
  180.     s.PrintStr();
  181.     s.DelSubStr(3, 5);
  182.     s.PrintStr();
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment