Загрузка данных


#include <iostream>
using namespace std;

class Rectangle {
private:
    double left;   // левая граница
    double bottom; // нижняя граница
    double right;  // правая граница
    double top;    // верхняя граница

public:
    // Конструктор
    Rectangle(double l, double b, double r, double t) {
        if (l < r && b < t) {
            left = l;
            bottom = b;
            right = r;
            top = t;
        } else {
            cout << "Oshibka! Sozdan edinichnyj kvadrat." << endl;
            left = 0;
            bottom = 0;
            right = 1;
            top = 1;
        }
    }

    // Конструктор по умолчанию
    Rectangle() {
        left = 0;
        bottom = 0;
        right = 1;
        top = 1;
    }

    // Перемещение
    void move(double dx, double dy) {
        left += dx;
        right += dx;
        bottom += dy;
        top += dy;
    }

    // Изменение размеров
    void resize(double width, double height) {
        if (width > 0 && height > 0) {
            right = left + width;
            top = bottom + height;
        } else {
            cout << "Oshibka: razmery dolzhny byt polozhitelnymi!" << endl;
        }
    }

    // Наименьший прямоугольник, содержащий два
    Rectangle enclosing(Rectangle r) {
        double newLeft, newBottom, newRight, newTop;
        
        if (left < r.left) newLeft = left;
        else newLeft = r.left;
        
        if (bottom < r.bottom) newBottom = bottom;
        else newBottom = r.bottom;
        
        if (right > r.right) newRight = right;
        else newRight = r.right;
        
        if (top > r.top) newTop = top;
        else newTop = r.top;
        
        return Rectangle(newLeft, newBottom, newRight, newTop);
    }

    // Пересечение двух прямоугольников
    Rectangle intersection(Rectangle r) {
        double newLeft, newBottom, newRight, newTop;
        
        if (left > r.left) newLeft = left;
        else newLeft = r.left;
        
        if (bottom > r.bottom) newBottom = bottom;
        else newBottom = r.bottom;
        
        if (right < r.right) newRight = right;
        else newRight = r.right;
        
        if (top < r.top) newTop = top;
        else newTop = r.top;
        
        if (newLeft < newRight && newBottom < newTop) {
            return Rectangle(newLeft, newBottom, newRight, newTop);
        } else {
            cout << "Pryamougolniki ne peresekayutsya!" << endl;
            return Rectangle(0, 0, 0, 0);
        }
    }

    // Вывод
    void print() {
        cout << "(" << left << ", " << bottom << ") - (" << right << ", " << top << ")";
        cout << " shirina=" << (right - left) << " vysota=" << (top - bottom) << endl;
    }
};

int main() {
    setlocale(LC_ALL, "Russian");
    
    int n;
    cout << "Skolko pryamougolnikov vy hotite sozdat? ";
    cin >> n;
    
    // Создаем массив прямоугольников
    Rectangle* rectangles = new Rectangle[n];
    
    // Ввод данных для каждого прямоугольника
    for(int i = 0; i < n; i++) {
        double x1, y1, x2, y2;
        cout << "\nPryamougolnik " << i + 1 << ":" << endl;
        cout << "Levyj nizhnij ugol (x y): ";
        cin >> x1 >> y1;
        cout << "Pravyj verhnij ugol (x y): ";
        cin >> x2 >> y2;
        rectangles[i] = Rectangle(x1, y1, x2, y2);
    }
    
    // Показываем все прямоугольники
    cout << "\n=== VSE PRYAMOUGOLNIKI ===" << endl;
    for(int i = 0; i < n; i++) {
        cout << "Pryamougolnik " << i + 1 << ": ";
        rectangles[i].print();
    }
    
    int choice;
    do {
        cout << "\n======= MENU =======" << endl;
        cout << "1. Peremestit pryamougolnik" << endl;
        cout << "2. Izmenit razmery pryamougolnika" << endl;
        cout << "3. Naiti naimenwij pryamougolnik, soderzhashij dva" << endl;
        cout << "4. Naiti peresechenie dvuh pryamougolnikov" << endl;
        cout << "5. Pokazat vse pryamougolniki" << endl;
        cout << "0. Vyhod" << endl;
        cout << "Vash vybor: ";
        cin >> choice;
        
        if(choice == 1) {
            int num;
            double dx, dy;
            cout << "Nomer pryamougolnika (1-" << n << "): ";
            cin >> num;
            if(num >= 1 && num <= n) {
                cout << "Vvedite smeshenie (dx dy): ";
                cin >> dx >> dy;
                rectangles[num-1].move(dx, dy);
                cout << "Pryamougolnik " << num << " peremeshen!" << endl;
                rectangles[num-1].print();
            } else {
                cout << "Nevernyj nomer!" << endl;
            }
        }
        else if(choice == 2) {
            int num;
            double w, h;
            cout << "Nomer pryamougolnika (1-" << n << "): ";
            cin >> num;
            if(num >= 1 && num <= n) {
                cout << "Vvedite novuju shirinu i vysotu: ";
                cin >> w >> h;
                rectangles[num-1].resize(w, h);
                cout << "Razmery izmeneny!" << endl;
                rectangles[num-1].print();
            } else {
                cout << "Nevernyj nomer!" << endl;
            }
        }
        else if(choice == 3) {
            int num1, num2;
            cout << "Vvedite nomera dvuh pryamougolnikov (1-" << n << "): ";
            cin >> num1 >> num2;
            if(num1 >= 1 && num1 <= n && num2 >= 1 && num2 <= n) {
                cout << "\nNaimenwij pryamougolnik, soderzhashij oba:" << endl;
                Rectangle result = rectangles[num1-1].enclosing(rectangles[num2-1]);
                result.print();
            } else {
                cout << "Nevernye nomera!" << endl;
            }
        }
        else if(choice == 4) {
            int num1, num2;
            cout << "Vvedite nomera dvuh pryamougolnikov (1-" << n << "): ";
            cin >> num1 >> num2;
            if(num1 >= 1 && num1 <= n && num2 >= 1 && num2 <= n) {
                cout << "\nPeresechenie pryamougolnikov:" << endl;
                Rectangle result = rectangles[num1-1].intersection(rectangles[num2-1]);
                result.print();
            } else {
                cout << "Nevernye nomera!" << endl;
            }
        }
        else if(choice == 5) {
            cout << "\n=== VSE PRYAMOUGOLNIKI ===" << endl;
            for(int i = 0; i < n; i++) {
                cout << "Pryamougolnik " << i + 1 << ": ";
                rectangles[i].print();
            }
        }
        else if(choice == 0) {
            cout << "Do svidaniya!" << endl;
        }
        else {
            cout << "Nevernyj vybor! Poprobujte snova." << endl;
        }
    } while(choice != 0);
    
    delete[] rectangles;
    
    return 0;
}