35657

Untitled

Jun 4th, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class String {
  10.  
  11. public:
  12.  
  13.     String() : size(0), capacity(15), str(new char[15]) {
  14.         total_number_strings++;
  15.     }
  16.  
  17.     explicit String(const int string_capacity) : size(0), capacity(string_capacity), str(new char[string_capacity]) {
  18.         total_number_strings++;
  19.     }
  20.  
  21.     explicit String(const char new_str[]) {
  22.         capacity = strlen(new_str) + 1;
  23.         str = new char[capacity];
  24.         strcpy(str, new_str);
  25.         size = capacity - 1;
  26.         total_number_strings++;
  27.     }
  28.  
  29.     String(const String& other) : size(other.size), capacity(other.capacity), str(new char[capacity]) {
  30.         strcpy(str, other.str);
  31.         total_number_strings++;
  32.     }
  33.  
  34.     String& operator=(const String& other) {
  35.         if (this != &other) {
  36.             size = other.size;
  37.             capacity = other.capacity;
  38.             delete[] str;
  39.             str = new char[capacity];
  40.             strcpy(str, other.str);
  41.         }
  42.         return *this;
  43.     }
  44.  
  45.     String& operator=(const char new_str[]) {
  46.         capacity = strlen(new_str) + 1;
  47.         delete[] str;
  48.         str = new char[capacity];
  49.         strcpy(str, new_str);
  50.         size = capacity - 1;
  51.         return *this;
  52.     }
  53.  
  54.     char& operator[] (const int index) {
  55.         if (index < 0 || index >= size) {
  56.             cout << "Invalid index" << endl;
  57.             abort;
  58.         }
  59.         return str[index];
  60.     }
  61.  
  62.     char& operator[] (const int index) const {
  63.         if (index < 0 || index >= size) {
  64.             cout << "Invalid index" << endl;
  65.             abort;
  66.         }
  67.         return str[index];
  68.     }
  69.  
  70.     bool operator==(const String& right) const {
  71.         if (size != right.size) {
  72.             return false;
  73.         }
  74.         for (int i = 0; i < size; i++) {
  75.             if (str[i] != right.str[i]) {
  76.                 return false;
  77.             }
  78.         }
  79.         return true;
  80.     }
  81.  
  82.     bool operator!=(const String& right) const {
  83.         if (size != right.size) {
  84.             return true;
  85.         }
  86.         for (int i = 0; i < size; i++) {
  87.             if (str[i] != right.str[i]) {
  88.                 return true;
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.  
  94.     int Size() const {
  95.         return size;
  96.     }
  97.  
  98.     int Capacity() const {
  99.         return capacity;
  100.     }
  101.  
  102.     static int GetTotalNumberStrings() {
  103.         return total_number_strings;
  104.     }
  105.  
  106.     ~String() {
  107.         delete[] str;
  108.         total_number_strings--;
  109.     }
  110.  
  111. private:
  112.     int size;
  113.     int capacity;
  114.     char* str;
  115.     static int total_number_strings;
  116. };
  117.  
  118. ostream& operator<<(ostream& output, const String& string) {
  119.     for (int i = 0; i < string.Size(); i++) {
  120.         output << string[i];
  121.     }
  122.     return output;
  123. }
  124.  
  125. istream& operator>>(istream& input, String& string) {
  126.     int capacity = 15;
  127.     int i = 0;
  128.     char* str = new char[capacity];
  129.     char ch;
  130.     while (true) {
  131.         ch = input.get();
  132.         if (ch == ' ' || ch == '\n') {
  133.             break;
  134.         }
  135.         if (i == capacity - 1) {
  136.             char* temp = new char[capacity *= 2];
  137.             for (int j = 0; j < i; j++) {
  138.                 temp[j] = str[j];
  139.             }
  140.             delete[] str;
  141.             str = temp;
  142.         }
  143.         str[i] = ch;
  144.         i++;
  145.     }
  146.     if (i > 0) {
  147.         str[i] = '\0';
  148.         string = str;
  149.     }
  150.     return input;
  151. }
  152.  
  153. int String::total_number_strings = 0;
  154.  
  155.  
  156. int main() {
  157.     String my_string;
  158.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  159.     my_string = "Hello";
  160.     cout << my_string << endl;
  161.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  162.     my_string = "Hello my dear friend";
  163.     cout << my_string << endl;
  164.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  165.  
  166.     my_string[2] = 'm';
  167.  
  168.     cout << my_string << endl;
  169.     cout << my_string[1] << endl;
  170.     String my_string2("1234");
  171.     cout << my_string2 << endl;
  172.     cout << "Size: " << my_string2.Size() << " Capacity: " << my_string2.Capacity() << endl;
  173.     String my_string3(my_string);
  174.     cout << my_string3 << endl;
  175.     cout << String::GetTotalNumberStrings() << endl;
  176.  
  177.     const String my_string4 = my_string2;
  178.  
  179.     cout << my_string4[1] << endl;
  180.  
  181.     cout << my_string4 << endl;
  182.  
  183.     cout << boolalpha;
  184.  
  185.     cout << (my_string == my_string4) << endl;
  186.     cout << (my_string != my_string4) << endl;
  187.  
  188.     String my_string5;
  189.  
  190.     cin >> my_string5;
  191.  
  192.     cout << my_string5;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment