Advertisement
hpnq

Классы ts

May 27th, 2024
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. //speed coding handle
  3.  
  4. #define mp make_pair
  5. #define cve(tpy) for (auto i : tpy) {for(auto j : i){cout << j << " ";  }cout << "\n";} ;
  6. #define f first
  7. #define s second
  8. #define loop(i, x, n) for (int i = x; i < n; i++)
  9. #define joop(x, n) for (ll j = x; j < n; j++)
  10. #define lp(n) for (ll i = 0; i < n; i++)
  11. #define err cout << "ERROR" << endl;
  12. #define all(x) x.begin(), x.end()
  13. #define pb push_back
  14. #define sz(x) x.size()
  15. #define rndm rng()
  16.  
  17. // types
  18. #define pii pair<int, int>
  19. #define pll pair<ll, ll>
  20. #define vvi vector<vector<int>>
  21. #define vvll vector<vector<ll>>
  22. typedef long long ll;
  23. typedef long double ld;
  24.  
  25. // types of data
  26. #define inf 1000000000
  27. #define infll 1000000000000000000
  28. #define INF ll(1e9)
  29.  
  30. #define md 998244353
  31. #define mod 1000000009
  32. //#define K 239017
  33.  
  34. #define DEBUG 1
  35. using namespace std;
  36. mt19937_64 rng(113113);
  37. uniform_int_distribution<ll> drist;
  38. //const int INF = numeric_limits<int>::max();
  39.  
  40. class TString {
  41. private:
  42.     char* str;
  43.     int length;
  44.  
  45. public:
  46.     // Конструктор без параметров
  47.     TString() : str(nullptr), length(0) {}
  48.  
  49.     // Конструктор с параметрами
  50.     TString(const char* s) {
  51.         length = strlen(s);
  52.         str = new char[length + 1];
  53.         strcpy(str, s);
  54.     }
  55.  
  56.     // Конструктор копирования
  57.     TString(const TString& other) {
  58.         length = other.length;
  59.         str = new char[length + 1];
  60.         strcpy(str, other.str);
  61.     }
  62.  
  63.     // Деструктор
  64.     ~TString() {
  65.         delete[] str;
  66.     }
  67.  
  68.     // Вывод строки
  69.     void print(){
  70.         if (str) {
  71.             cout << str << endl;
  72.         }
  73.     }
  74.  
  75.     // Вывод строки и ее длины на экран
  76.     void printWithLength(){
  77.         if (str) {
  78.  
  79.             cout << str << " (Length: " << length << ")" << endl;
  80.         }
  81.     }
  82.  
  83.     // Увеличение длины строки на заданное значение
  84.     void increaseLength(int increment) {
  85.         if (increment > 0) {
  86.             char* newStr = new char[length + increment + 1];
  87.             if (str) {
  88.                 strcpy(newStr, str);
  89.             } else {
  90.                 newStr[0] = '\0';
  91.             }
  92.             delete[] str;
  93.             str = newStr;
  94.             length += increment;
  95.             str[length] = '\0';
  96.         }
  97.     }
  98.  
  99.     // Прибавление к строке заданного символа
  100.     void appendChar(char c) {
  101.         increaseLength(1);
  102.         str[length - 1] = c;
  103.         str[length] = '\0';
  104.     }
  105.  
  106.     // Изменение строки на заданное значение
  107.     void setString(const char* s) {
  108.         delete[] str;
  109.         length = strlen(s);
  110.         str = new char[length + 1];
  111.         strcpy(str, s);
  112.     }
  113.  
  114.     // Получение подстроки из данной строки
  115.     TString substring(int start, int count) const {
  116.         if (start < 0 || start >= length || count <= 0 || start + count > length) {
  117.             return TString(""); // Return an empty string for invalid ranges
  118.         }
  119.  
  120.         char* subStr = new char[count + 1];
  121.         strncpy(subStr, str + start, count);
  122.         subStr[count] = '\0';
  123.  
  124.         TString result(subStr);
  125.         delete[] subStr;
  126.         return result;
  127.     }
  128.  
  129.     // Метод выдает полученную строку
  130.     const char* getString() const {
  131.         return str;
  132.     }
  133.  
  134.     // Прибавление к строке заданной строки (конкатенация)
  135.     void appendString(const TString& other) {
  136.         if (other.str) {
  137.             char* newStr = new char[length + other.length + 1];
  138.             if (str) {
  139.                 strcpy(newStr, str);
  140.             }
  141.             if (other.str) {
  142.                 strcpy(newStr + length, other.str);
  143.             }
  144.             delete[] str;
  145.             str = newStr;
  146.             length += other.length;
  147.             str[length] = '\0';
  148.         }
  149.     }
  150.  
  151.     // Сравнение длины данной строки с длиной другой строки
  152.     int compareLength(const TString& other) const {
  153.         if (length < other.length) {
  154.             return -1;
  155.         } else if (length > other.length) {
  156.             return 1;
  157.         } else {
  158.             return 0;
  159.         }
  160.     }
  161. };
  162.  
  163.  
  164.  
  165. int main() {
  166.     // Объявление объектов
  167.     cout << "Enter first:\n";
  168. //    cin.ignore();  // clear the input buffer
  169.     string s;
  170.     getline(cin, s);
  171.     TString str1(s.c_str());
  172.  
  173.     cout << "Enter second:\n";
  174. //    cin.ignore();  // clear the input buffer
  175.     getline(cin, s);
  176.     TString str2(s.c_str());
  177.  
  178.     cout << "Enter third:\n";
  179. //    cin.ignore();  // clear the input buffer
  180.     getline(cin, s);
  181.     TString str3(s.c_str());
  182.  
  183.     // Демонстрация
  184.     cout << "str1: ";
  185.     str1.print();
  186.     cout << "str2: ";
  187.     str2.print();
  188.     cout << "str3: ";
  189.     str3.print();
  190.  
  191.  
  192.     cout << "\nAppend str1 to str2: ";
  193.     str2.appendString(str1);
  194.     str2.printWithLength();
  195.  
  196.     cout << "\nstr2 with length: ";
  197.     str2.printWithLength();
  198.  
  199.     cout << "\nAppend '!' to str2: ";
  200.     str2.appendChar('!');
  201.     str2.printWithLength();
  202.  
  203.     cout << "\nIncrease length of str2 by 5: ";
  204.     str2.increaseLength(5);
  205.     str2.printWithLength();
  206.  
  207.  
  208.  
  209.     cout << "\nSet str1 to 'World': ";
  210.     str1.setString("World");
  211.     str1.printWithLength();
  212.  
  213.     cout << "\nSubstring of str2 (start 0, count 5): ";
  214.     TString subStr = str2.substring(0, 5);
  215.     subStr.printWithLength();
  216.  
  217. //    cout << "\nAppend str1 to str2: ";
  218. //    str2.appendString(str1);
  219. //    str2.printWithLength();
  220.  
  221.     cout << "\nCompare lengths of str2 and str3: ";
  222.     int comparison = str2.compareLength(str3);
  223.     if (comparison < 0) {
  224.         cout << "str2 is shorter than str3" << endl;
  225.     } else if (comparison > 0) {
  226.         cout << "str2 is longer than str3" << endl;
  227.     } else {
  228.         cout << "str2 is equal in length to str3" << endl;
  229.     }
  230.  
  231.     return 0;
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement