hpnq

перегрузка класса

Sep 18th, 2024
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.19 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.     TString& operator =(const TString& other);
  164.  
  165.     friend std::ostream& operator << (std::ostream& os, const TString& other);
  166.     friend std::istream& operator >> (std::istream& in, TString& other);
  167.  
  168.     bool operator ==(const TString& other);
  169.  
  170.     TString& operator ++();
  171.     TString& operator --();
  172.  
  173.     bool operator >(const TString& other);
  174.     TString operator+(const TString& other);
  175.  
  176.     TString& operator+=(const TString& other);
  177.     TString& operator !();
  178.  
  179. };
  180.  
  181. TString& TString::operator =(const TString& other){
  182.  
  183.     str = other.str;
  184.     length = other.length;
  185.     return *this;
  186. }
  187.  
  188. std::ostream& operator << (std::ostream& os, const TString& other) {
  189.     loop(i, 0, other.length){
  190.         os << other.str[i];
  191.     }
  192.     return os;
  193. }
  194.  
  195. std::istream& operator >> (std::istream& in, TString& other) {
  196.     string s;
  197.  
  198.     getline(in, s);
  199.  
  200.     other.setString(s.c_str());
  201.  
  202.     return in;
  203. }
  204.  
  205. bool TString::operator ==(const TString& other){
  206.     if(compareLength(other) != 0){
  207.         return 0;
  208.     }
  209.     if(strcmp(str, other.str) != 0){
  210.         return 0;
  211.     }
  212.     return 1;
  213. }
  214.  
  215. TString& TString::operator ++(){
  216.     appendChar(' ');
  217.     return *this;
  218. }
  219.  
  220. bool TString::operator >(const TString& other){
  221.     return (str > other.str);
  222. }
  223.  
  224. TString& TString::operator --(){
  225.     string s = this->str;
  226. //    cout << s << "\n";
  227.     s.pop_back();
  228.     this->setString(s.c_str());
  229.     return *this;
  230. }
  231.  
  232.  
  233. TString TString::operator+(const TString& other){
  234. //    cout << other.str;
  235.     TString ts;
  236.     string temp1 = other.str;
  237.     string temp2 = str; // idk why (*other).str doesn't work
  238.     ts.setString((temp2 + temp1).c_str());
  239.     return ts;
  240. }
  241.  
  242.  
  243. TString& TString::operator+=(const TString& other){
  244.     this->appendString(other);
  245.     return *this;
  246. }
  247.  
  248. TString& TString::operator!(){
  249.     for(int i = 0; 2*i < length; i++){
  250.         swap(str[i], str[length-i-1]);
  251.     }
  252.     return *this;
  253.  
  254. }
  255.  
  256. int main() {
  257.     // Объявление объектов
  258. //    cin.ignore();  // clear the input buffer
  259.     string s = "abc";
  260. //    getline(cin, s);
  261.     TString str1(s.c_str());
  262.     --str1;
  263.  
  264.     TString str2;
  265.     cout << "Enter second:\n";
  266.  
  267.     cin >> str2;
  268. //    str1 = str2;
  269.  
  270.  
  271.     cout << str2 << "\n";
  272. //    return 1;
  273. //
  274.     cout << "check equals: str1 and str2 : " << (str2==str1 ? "Yes" : "No") << "\n";
  275.     --str1;
  276.     ++str2;
  277.     cout << "increment and decrement :" << str1 << " " << str2 << "\n";
  278. //
  279.     cout << "check str1 > str2 ? : " << (str1 > str2 ? "Yes" : "No" ) << "\n";
  280. //
  281.     cout << "str1 plus str2 = " << str1+str2 << "\n";
  282. //
  283.     str1+= str2;
  284. //
  285.     cout << "and equals but += : " << str1 << '\n';
  286. //
  287. //
  288.     str1 = str2;
  289.     cout << "str1 = str2: " << str1 << "\n";
  290. //
  291.     cout << "So and reverse str2(!str2): ";
  292.     cout << !str2 << '\n';
  293.  
  294.  
  295.     return 0;
  296. }
  297.  
Advertisement
Add Comment
Please, Sign In to add comment