Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
1
Добавлен:
01.11.2025
Размер:
3.94 Кб
Скачать
#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;

class MyString {
private:
    char* str;
    int length;
    
    void allocate(int len) {
        length = len;
        str = new char[length + 1];
    }
    
public:
    // Конструкторы
    MyString(const char* s = "") {
        length = strlen(s);
        str = new char[length + 1];
        strcpy(str, s);
    }
    
    MyString(const MyString& other) {
        length = other.length;
        str = new char[length + 1];
        strcpy(str, other.str);
    }
    
    ~MyString() {
        delete[] str;
    }
    
    // Оператор присваивания
    MyString& operator=(const MyString& other) {
        if (this != &other) {
            delete[] str;
            length = other.length;
            str = new char[length + 1];
            strcpy(str, other.str);
        }
        return *this;
    }
    
    // Сравнение строк (==)
    bool operator==(const MyString& other) const {
        return strcmp(str, other.str) == 0;
    }
    
    // Удаление символа (-)
    MyString operator-(char ch) const {
        int newLength = 0;
        for (int i = 0; i < length; i++) {
            if (str[i] != ch) newLength++;
        }
        
        MyString result;
        result.allocate(newLength);
        
        int j = 0;
        for (int i = 0; i < length; i++) {
            if (str[i] != ch) {
                result.str[j++] = str[i];
            }
        }
        result.str[newLength] = '\0';
        return result;
    }
    
    // Переворот строки (~)
    MyString operator~() const {
        MyString result;
        result.allocate(length);
        
        for (int i = 0; i < length; i++) {
            result.str[i] = str[length - 1 - i];
        }
        result.str[length] = '\0';
        return result;
    }
    
    // Удаление символов из первой строки, встречающихся во второй
    MyString strset(const MyString& other) const {
        bool toRemove[256] = {false};
        for (int i = 0; i < other.length; i++) {
            toRemove[(unsigned char)other.str[i]] = true;
        }
        
        int newLength = 0;
        for (int i = 0; i < length; i++) {
            if (!toRemove[(unsigned char)str[i]]) newLength++;
        }
        
        MyString result;
        result.allocate(newLength);
        
        int j = 0;
        for (int i = 0; i < length; i++) {
            if (!toRemove[(unsigned char)str[i]]) {
                result.str[j++] = str[i];
            }
        }
        result.str[newLength] = '\0';
        return result;
    }
    
    void print() const {
        cout << str << " (длина: " << length << ")" << endl;
    }
    
    friend ostream& operator<<(ostream& os, const MyString& s);
};

ostream& operator<<(ostream& os, const MyString& s) {
    os << s.str;
    return os;
}

int main() {
    SetConsoleOutputCP(65001);
    
    cout << "=== Задача 3.2 - Строка символов ===" << endl;
    
    MyString s1("Hello World!");
    MyString s2("Hello");
    MyString s3("Hello World!");
    
    cout << "s1: " << s1 << endl;
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;
    
    cout << "s1 == s2: " << (s1 == s2 ? "true" : "false") << endl;
    cout << "s1 == s3: " << (s1 == s3 ? "true" : "false") << endl;
    
    MyString withoutL = s1 - 'l';
    cout << "s1 - 'l': " << withoutL << endl;
    
    MyString reversed = ~s1;
    cout << "~s1 (перевернутая): " << reversed << endl;
    
    MyString s4("abc");
    MyString s5("bd");
    MyString result = s4.strset(s5);
    cout << "strset(\"abc\", \"bd\"): " << result << endl;
    
    return 0;
}
Соседние файлы в папке Лаба3