Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- //speed coding handle
- #define mp make_pair
- #define cve(tpy) for (auto i : tpy) {for(auto j : i){cout << j << " "; }cout << "\n";} ;
- #define f first
- #define s second
- #define loop(i, x, n) for (int i = x; i < n; i++)
- #define joop(x, n) for (ll j = x; j < n; j++)
- #define lp(n) for (ll i = 0; i < n; i++)
- #define err cout << "ERROR" << endl;
- #define all(x) x.begin(), x.end()
- #define pb push_back
- #define sz(x) x.size()
- #define rndm rng()
- // types
- #define pii pair<int, int>
- #define pll pair<ll, ll>
- #define vvi vector<vector<int>>
- #define vvll vector<vector<ll>>
- typedef long long ll;
- typedef long double ld;
- // types of data
- #define inf 1000000000
- #define infll 1000000000000000000
- #define INF ll(1e9)
- #define md 998244353
- #define mod 1000000009
- //#define K 239017
- #define DEBUG 1
- using namespace std;
- mt19937_64 rng(113113);
- uniform_int_distribution<ll> drist;
- //const int INF = numeric_limits<int>::max();
- class TString {
- private:
- char* str;
- int length;
- public:
- // Конструктор без параметров
- TString() : str(nullptr), length(0) {}
- // Конструктор с параметрами
- TString(const char* s) {
- length = strlen(s);
- str = new char[length + 1];
- strcpy(str, s);
- }
- // Конструктор копирования
- TString(const TString& other) {
- length = other.length;
- str = new char[length + 1];
- strcpy(str, other.str);
- }
- // Деструктор
- ~TString() {
- delete[] str;
- }
- // Вывод строки
- void print(){
- if (str) {
- cout << str << endl;
- }
- }
- // Вывод строки и ее длины на экран
- void printWithLength(){
- if (str) {
- cout << str << " (Length: " << length << ")" << endl;
- }
- }
- // Увеличение длины строки на заданное значение
- void increaseLength(int increment) {
- if (increment > 0) {
- char* newStr = new char[length + increment + 1];
- if (str) {
- strcpy(newStr, str);
- } else {
- newStr[0] = '\0';
- }
- delete[] str;
- str = newStr;
- length += increment;
- str[length] = '\0';
- }
- }
- // Прибавление к строке заданного символа
- void appendChar(char c) {
- increaseLength(1);
- str[length - 1] = c;
- str[length] = '\0';
- }
- // Изменение строки на заданное значение
- void setString(const char* s) {
- delete[] str;
- length = strlen(s);
- str = new char[length + 1];
- strcpy(str, s);
- }
- // Получение подстроки из данной строки
- TString substring(int start, int count) const {
- if (start < 0 || start >= length || count <= 0 || start + count > length) {
- return TString(""); // Return an empty string for invalid ranges
- }
- char* subStr = new char[count + 1];
- strncpy(subStr, str + start, count);
- subStr[count] = '\0';
- TString result(subStr);
- delete[] subStr;
- return result;
- }
- // Метод выдает полученную строку
- const char* getString() const {
- return str;
- }
- // Прибавление к строке заданной строки (конкатенация)
- void appendString(const TString& other) {
- if (other.str) {
- char* newStr = new char[length + other.length + 1];
- if (str) {
- strcpy(newStr, str);
- }
- if (other.str) {
- strcpy(newStr + length, other.str);
- }
- delete[] str;
- str = newStr;
- length += other.length;
- str[length] = '\0';
- }
- }
- // Сравнение длины данной строки с длиной другой строки
- int compareLength(const TString& other) const {
- if (length < other.length) {
- return -1;
- } else if (length > other.length) {
- return 1;
- } else {
- return 0;
- }
- }
- };
- int main() {
- // Объявление объектов
- cout << "Enter first:\n";
- // cin.ignore(); // clear the input buffer
- string s;
- getline(cin, s);
- TString str1(s.c_str());
- cout << "Enter second:\n";
- // cin.ignore(); // clear the input buffer
- getline(cin, s);
- TString str2(s.c_str());
- cout << "Enter third:\n";
- // cin.ignore(); // clear the input buffer
- getline(cin, s);
- TString str3(s.c_str());
- // Демонстрация
- cout << "str1: ";
- str1.print();
- cout << "str2: ";
- str2.print();
- cout << "str3: ";
- str3.print();
- cout << "\nAppend str1 to str2: ";
- str2.appendString(str1);
- str2.printWithLength();
- cout << "\nstr2 with length: ";
- str2.printWithLength();
- cout << "\nAppend '!' to str2: ";
- str2.appendChar('!');
- str2.printWithLength();
- cout << "\nIncrease length of str2 by 5: ";
- str2.increaseLength(5);
- str2.printWithLength();
- cout << "\nSet str1 to 'World': ";
- str1.setString("World");
- str1.printWithLength();
- cout << "\nSubstring of str2 (start 0, count 5): ";
- TString subStr = str2.substring(0, 5);
- subStr.printWithLength();
- // cout << "\nAppend str1 to str2: ";
- // str2.appendString(str1);
- // str2.printWithLength();
- cout << "\nCompare lengths of str2 and str3: ";
- int comparison = str2.compareLength(str3);
- if (comparison < 0) {
- cout << "str2 is shorter than str3" << endl;
- } else if (comparison > 0) {
- cout << "str2 is longer than str3" << endl;
- } else {
- cout << "str2 is equal in length to str3" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement