HICONT

Strings.h

Mar 31st, 2022 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #pragma once
  2. #include<string>
  3. using namespace std;
  4.  
  5. class MyString
  6. {
  7. private:
  8.     char* mem;
  9.     int count;
  10.     int size;
  11. public:
  12.     MyString(string str = "") {
  13.         if (str.length() > 128)
  14.         {
  15.             size = str.length();
  16.         }
  17.         else {
  18.             size = 128;
  19.         }
  20.         size = str.length();
  21.         count = str.length();
  22.         mem = new char[size];
  23.         for (int i = 0; i < count; i++) {
  24.             mem[i] = str[i];
  25.         }
  26.     };
  27.     MyString(char* src, int k) {
  28.         if (k > 128)
  29.         {
  30.             size = k;
  31.     }
  32.         else {
  33.             size = 128;
  34.         }
  35.         mem = new char[size];
  36.         count = k;
  37.         for (int i = 0; i < count; i++) {
  38.             mem[i] = src[i];
  39.         }
  40.     };
  41.     ~MyString() { delete[]mem; };
  42.     MyString(const MyString& tmp) {
  43.         count = tmp.count;
  44.         size = tmp.size;
  45.         mem = new char[size];
  46.         for (int i = 0; i < count; i++) {
  47.             mem[i] = tmp.mem[i];
  48.         }
  49.     };
  50.  
  51.     MyString& operator=(MyString tmp) {
  52.         if (size != tmp.size) {
  53.             if (size != 0) {
  54.                 delete[]mem;
  55.             }
  56.             size = tmp.size;
  57.             mem = new char[size];
  58.         }
  59.         count = tmp.count;
  60.         for (int i = 0; i < count; i++) {
  61.             mem[i] = tmp.mem[i];
  62.         }
  63.         return *this;
  64.     };
  65.  
  66.     int GetCount() {
  67.         return this->count;
  68.     }
  69.    
  70.     char& operator[](int k) {
  71.         if (k >= 0 && k < count) {
  72.             return mem[k];
  73.         }
  74.         char a = NULL;
  75.         return a;
  76.     }
  77.    
  78.     friend ostream& operator<<(ostream& out, MyString tmp)
  79.     {
  80.         int count;
  81.         for (int i = 0; i < count; i++)
  82.         {
  83.             out << tmp.mem[i];
  84.             return out;
  85.         }
  86.     }
  87.     friend istream& operator>>(istream& in, MyString& tmp)
  88.     {
  89.         char* r;
  90.         r = new char[512];
  91.         tmp.count = 0;
  92.         do
  93.         {
  94.             in >> r[tmp.count];
  95.             if (tmp.count != 13)
  96.             {
  97.                 tmp.count++;
  98.             }
  99.         } while (tmp.count != 13);
  100.         tmp = MyString(r, tmp.count);
  101.         return in;
  102.     }
  103.     int operator == (MyString tmp)
  104.     {
  105.         int result = 0; //сравнимые строки одинаковы
  106.         if (count != tmp.count)
  107.         {
  108.             int result = -1;
  109.         }
  110.         else
  111.         {
  112.             for(int i = 0; i < count && result == 0; i++)
  113.             {
  114.                 if (mem[i] != tmp.mem[i])
  115.                 {
  116.                     result = -1;
  117.                 }
  118.             }
  119.         }
  120.         return result;
  121.     }
  122.     int operator > (MyString tmp)
  123.     {
  124.         int result = 0;
  125.         for (int i = 0; i < count && i < tmp.count; i++)
  126.         {
  127.             if (mem[i] > tmp.mem[i])
  128.             {
  129.                 result = -1;
  130.             }
  131.             if (count > tmp.count)
  132.             {
  133.                 result = -1;
  134.             }
  135.         }
  136.         return result;
  137.     }
  138.     int operator < (MyString tmp)
  139.     {
  140.         int result = 0;
  141.         for (int i = 0; i < count && i < tmp.count; i++)
  142.         {
  143.             if (mem[i] < tmp.mem[i])
  144.             {
  145.                 result = -1;
  146.             }
  147.             if (count < tmp.count)
  148.             {
  149.                 result = -1;
  150.             }
  151.         }
  152.         return result;
  153.     }
  154.     MyString(int num)
  155.     {
  156.         size = 128;
  157.         count = 0;
  158.         char tmp;
  159.         mem = new char[size];
  160.         /*char nums[] = {'0','1','2','3','4','5','6','7','8','9'};*/
  161.         while (num > 0)
  162.         {
  163.             mem[count++] = num / 10 + 48;
  164.             num /= 10;
  165.         }
  166.         for (int i = 0; i < count || 2; i++)
  167.         {
  168.             tmp = mem[i];
  169.             mem[i] = mem[count - 1 - i];
  170.             mem[count - 1 - i] = tmp;
  171.         }
  172.     }
  173. };
Add Comment
Please, Sign In to add comment