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;
- }
- }
- TString& operator =(const TString& other);
- friend std::ostream& operator << (std::ostream& os, const TString& other);
- friend std::istream& operator >> (std::istream& in, TString& other);
- bool operator ==(const TString& other);
- TString& operator ++();
- TString& operator --();
- bool operator >(const TString& other);
- TString operator+(const TString& other);
- TString& operator+=(const TString& other);
- TString& operator !();
- };
- TString& TString::operator =(const TString& other){
- str = other.str;
- length = other.length;
- return *this;
- }
- std::ostream& operator << (std::ostream& os, const TString& other) {
- loop(i, 0, other.length){
- os << other.str[i];
- }
- return os;
- }
- std::istream& operator >> (std::istream& in, TString& other) {
- string s;
- getline(in, s);
- other.setString(s.c_str());
- return in;
- }
- bool TString::operator ==(const TString& other){
- if(compareLength(other) != 0){
- return 0;
- }
- if(strcmp(str, other.str) != 0){
- return 0;
- }
- return 1;
- }
- TString& TString::operator ++(){
- appendChar(' ');
- return *this;
- }
- bool TString::operator >(const TString& other){
- return (str > other.str);
- }
- TString& TString::operator --(){
- string s = this->str;
- // cout << s << "\n";
- s.pop_back();
- this->setString(s.c_str());
- return *this;
- }
- TString TString::operator+(const TString& other){
- // cout << other.str;
- TString ts;
- string temp1 = other.str;
- string temp2 = str; // idk why (*other).str doesn't work
- ts.setString((temp2 + temp1).c_str());
- return ts;
- }
- TString& TString::operator+=(const TString& other){
- this->appendString(other);
- return *this;
- }
- TString& TString::operator!(){
- for(int i = 0; 2*i < length; i++){
- swap(str[i], str[length-i-1]);
- }
- return *this;
- }
- int main() {
- // Объявление объектов
- // cin.ignore(); // clear the input buffer
- string s = "abc";
- // getline(cin, s);
- TString str1(s.c_str());
- --str1;
- TString str2;
- cout << "Enter second:\n";
- cin >> str2;
- // str1 = str2;
- cout << str2 << "\n";
- // return 1;
- //
- cout << "check equals: str1 and str2 : " << (str2==str1 ? "Yes" : "No") << "\n";
- --str1;
- ++str2;
- cout << "increment and decrement :" << str1 << " " << str2 << "\n";
- //
- cout << "check str1 > str2 ? : " << (str1 > str2 ? "Yes" : "No" ) << "\n";
- //
- cout << "str1 plus str2 = " << str1+str2 << "\n";
- //
- str1+= str2;
- //
- cout << "and equals but += : " << str1 << '\n';
- //
- //
- str1 = str2;
- cout << "str1 = str2: " << str1 << "\n";
- //
- cout << "So and reverse str2(!str2): ";
- cout << !str2 << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment