35657

Untitled

Jun 28th, 2024
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class String {
  8.  
  9. public:
  10.  
  11.     String() : size_(0), capacity_(15), str_(new char[capacity_]) {}
  12.  
  13.     explicit String(const int string_capacity) : size_(0), capacity_(string_capacity), str_(new char[capacity_]) {}
  14.  
  15.     String(const char* new_string) : size_(strlen(new_string)), capacity_(size_ + 1), str_(new char[capacity_]) {
  16.         strcpy(str_, new_string);
  17.     }
  18.  
  19.     String(const String& other) : size_(other.size_), capacity_(other.capacity_), str_(new char[capacity_]) {
  20.         strcpy(str_, other.str_);
  21.     }
  22.  
  23.     String(String&& other) : size_(other.size_), capacity_(other.capacity_), str_(other.str_) {
  24.         other.str_ = nullptr;
  25.         other.size_ = 0;
  26.         other.capacity_ = 0;
  27.     }
  28.  
  29.     String& operator=(const String& other) {
  30.         if (&other != this) {
  31.             size_ = other.size_;
  32.             capacity_ = other.capacity_;
  33.             delete[] str_;
  34.             str_ = new char[capacity_];
  35.             strcpy(str_, other.str_);
  36.         }
  37.         return *this;
  38.     }
  39.  
  40. String& operator=(String&& other) {
  41.         if (&other != this) {
  42.             size_ = other.size_;
  43.             capacity_ = other.capacity_;
  44.             delete[] str_;
  45.             str_ = other.str_;
  46.             other.str_ = nullptr;
  47.             other.size_ = 0;
  48.             other.capacity_ = 0;
  49.         }
  50.         return *this;
  51.     }
  52.  
  53.     String& operator=(const char* new_string) {
  54.         int new_space = strlen(new_string) + 1;
  55.         if (capacity_ < new_space) {
  56.             delete[] str_;
  57.             capacity_ = new_space;
  58.             str_ = new char[capacity_];
  59.         }
  60.         strcpy(str_, new_string);
  61.         size_ = capacity_ - 1;
  62.         return *this;
  63.     }
  64.  
  65.     char& operator[](const int index) {
  66.         if (index < 0 || index >= size_) {
  67.             cout << "Некорректный индекс" << endl;
  68.         }
  69.         return str_[index];
  70.     }
  71.  
  72.     const char& operator[](const int index) const {
  73.         if (index < 0 || index >= size_) {
  74.             cout << "Некорректный индекс" << endl;
  75.         }
  76.         return str_[index];
  77.     }
  78.  
  79.     bool operator==(const String& other) const {
  80.         return strcmp(str_, other.str_) == 0;
  81.     }
  82.  
  83.     bool operator!=(const String& other) const {
  84.         return strcmp(str_, other.str_) != 0;
  85.     }
  86.  
  87.     int size() const {
  88.         return size_;
  89.     }
  90.  
  91.     int capacity() const {
  92.         return capacity_;
  93.     }
  94.  
  95.     ~String() {
  96.         delete[] str_;
  97.     }
  98.  
  99.     friend ostream& operator<<(ostream& output, const String& str);
  100.  
  101. private:
  102.     int size_;
  103.     int capacity_;
  104.     char* str_;
  105. };
  106.  
  107.  
  108. ostream& operator<<(ostream& output, const String& str) {
  109.     output << str.str_;
  110.     return output;
  111. }
  112.  
  113. istream& operator>>(istream& input, String& str) {
  114.     int capacity = 15;
  115.     int i = 0;
  116.     char* new_str = new char[capacity];
  117.     char ch;
  118.  
  119.     while (ch = input.get()) {
  120.         if (ch == ' ' || ch == '\n') {
  121.             break;
  122.         }
  123.         if (i == capacity - 1) {
  124.             char* temp = new char[capacity *= 2];
  125.             strcpy(temp, new_str);
  126.             delete[] new_str;
  127.             new_str = temp;
  128.         }
  129.         new_str[i++] = ch;
  130.     }
  131.     new_str[i] = '\0';
  132.     str = new_str;
  133.     delete[] new_str;
  134.     return input;
  135. }
  136.  
  137. int main() {
  138.     setlocale(LC_ALL, "ru");
  139.  
  140.     String str1;
  141.  
  142.     String str2(30);
  143.  
  144.     String str3("Привет");
  145.  
  146.     cout << str3 << endl;
  147.  
  148.     str3= "Новая строка";
  149.  
  150.     cout << str3 << endl;
  151.  
  152.     str1 = "Еще одна новая строка";
  153.  
  154.     cout << str1 << endl;
  155.  
  156.     String str4(str3);
  157.  
  158.     cout << str4 << endl;
  159.  
  160.     str4 = str1;
  161.  
  162.     cout << str1 << endl;
  163.  
  164.     cout << str4 << endl;
  165.  
  166.     str4[5] = 'н';
  167.  
  168.     cout << str4 << endl;
  169.  
  170.     cout << (str3 == str4) << endl;
  171.  
  172.     cout << (str3 != str4) << endl;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment