Advertisement
Trawka011

Untitled

Jan 19th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class String {
  6. private:
  7.     char* str;
  8.     int len;
  9. public:
  10.     String(int len = 0) {
  11.         this->len = len;
  12.         str = new char[len];
  13.     }
  14.     String(const String& other) {
  15.         len = other.len;
  16.         str = new char[other.len];
  17.         for (int i = 0; i < len; i++) {
  18.             str[i] = other.str[i];
  19.         }
  20.     }
  21.     String(const char* str) {
  22.         int counter = 0;
  23.         while (str[counter] != 0) counter++;
  24.         len = counter;
  25.         this->str = new char[len];
  26.         for (int i = 0; i < len; i++) {
  27.             this->str[i] = str[i];
  28.         }
  29.     }
  30.     char& operator[](int index) {
  31.         return str[index];
  32.     }
  33.     char operator[](int index) const {
  34.         return str[index];
  35.     }
  36.     ~String() {
  37.         delete[] str;
  38.     }
  39.     String& operator=(String& other) {
  40.         delete[] str;
  41.         len = other.len;
  42.         str = new char[other.len];
  43.         for (int i = 0; i < len; i++) {
  44.             str[i] = other.str[i];
  45.         }
  46.         return *this;
  47.     }
  48.     String& operator+(String& other) {
  49.         char* str_new = new char[len];
  50.         for (int i = 0; i < len; i++)
  51.         {
  52.             str_new[i] = str[i];
  53.         }
  54.         str = new char[len + other.len];
  55.         for (int i = 0; i < len; i++) {
  56.             str[i] = str_new[i];
  57.         }
  58.         for (int i = 0; i < other.len; i++) {
  59.             str[i+len] = other[i];
  60.         }
  61.         len += other.size();
  62.         return *this;
  63.     }
  64.     String& operator*(int x) {
  65.         char* str_new = new char[len * x];
  66.         for (int i = 0; i < len * x; i++)
  67.         {
  68.             str_new[i] = str[i % len];
  69.         }
  70.         str = new char[len * x];
  71.         str = str_new;
  72.         len *= x;
  73.         return *this;
  74.     }
  75.     int size() const {
  76.         return len;
  77.     }
  78.     friend istream& operator>>(istream& in, String& a);
  79.     friend ostream& operator<<(ostream& on, String& a);
  80. };
  81. istream& operator>>(istream& in, String& a) {
  82.     int buffer_size = 1000000;
  83.     char* buffer = new char[buffer_size];
  84.     in.getline(buffer, buffer_size);
  85.     a.str = buffer;
  86.     a.len = strlen(buffer);
  87.     return in;
  88. }
  89.  
  90. ostream& operator<<(ostream& on, String& a) {
  91.     for (int i = 0; i < a.size(); i++) {
  92.         on << a[i];
  93.     }
  94.     on << endl;
  95.     return on;
  96. }
  97.  
  98. int main() {
  99.     String z;
  100.     int tmp;
  101.     cin >> z;
  102.     cin >> tmp;
  103.     cout << z * tmp;
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement