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


#include <iostream>
using namespace std;

class VECTOR2
{
protected:
    double x;
    double y;

public:
    VECTOR2()
    {
        x = 0;
        y = 0;
    }

    VECTOR2(double x, double y)
    {
        this->x = x;
        this->y = y;
    }

    virtual void display() const
    {
        cout << "(" << x << "; " << y << ")" << endl;
    }

    VECTOR2 multiplyByNumber(double k) const
    {
        return VECTOR2(x * k, y * k);
    }

    VECTOR2 divideByNumber(double k) const
    {
        if (k == 0)
        {
            cout << "Ошибка: деление на 0" << endl;
            return *this;
        }
        return VECTOR2(x / k, y / k);
    }

    VECTOR2 operator+(const VECTOR2& other) const
    {
        return VECTOR2(x + other.x, y + other.y);
    }

    VECTOR2 operator-(const VECTOR2& other) const
    {
        return VECTOR2(x - other.x, y - other.y);
    }

    VECTOR2& operator=(const VECTOR2& other)
    {
        if (this != &other)
        {
            x = other.x;
            y = other.y;
        }
        return *this;
    }

    double operator*(const VECTOR2& other) const
    {
        return x * other.x + y * other.y;
    }

    bool operator==(const VECTOR2& other) const
    {
        return x == other.x && y == other.y;
    }

    VECTOR2 calcVector(double k, const VECTOR2& a, double m, const VECTOR2& b) const
    {
        return VECTOR2(k * a.x + m * b.x, k * a.y + m * b.y);
    }
};

class VECTOR3 : public VECTOR2
{
protected:
    double z;

public:
    VECTOR3() : VECTOR2()
    {
        z = 0;
    }

    VECTOR3(double x, double y, double z) : VECTOR2(x, y)
    {
        this->z = z;
    }

    void display() const override
    {
        cout << "(" << x << "; " << y << "; " << z << ")" << endl;
    }

    VECTOR3 multiplyByNumber(double k) const
    {
        return VECTOR3(x * k, y * k, z * k);
    }

    VECTOR3 divideByNumber(double k) const
    {
        if (k == 0)
        {
            cout << "Ошибка: деление на 0" << endl;
            return *this;
        }
        return VECTOR3(x / k, y / k, z / k);
    }

    VECTOR3 operator+(const VECTOR3& other) const
    {
        return VECTOR3(x + other.x, y + other.y, z + other.z);
    }

    VECTOR3 operator-(const VECTOR3& other) const
    {
        return VECTOR3(x - other.x, y - other.y, z - other.z);
    }

    VECTOR3& operator=(const VECTOR3& other)
    {
        if (this != &other)
        {
            x = other.x;
            y = other.y;
            z = other.z;
        }
        return *this;
    }

    double operator*(const VECTOR3& other) const
    {
        return x * other.x + y * other.y + z * other.z;
    }

    bool operator==(const VECTOR3& other) const
    {
        return x == other.x && y == other.y && z == other.z;
    }

    VECTOR3 calcVector(double k, const VECTOR3& a, double m, const VECTOR3& b) const
    {
        return VECTOR3(
            k * a.x + m * b.x,
            k * a.y + m * b.y,
            k * a.z + m * b.z
        );
    }
};

int main()
{
    setlocale(LC_ALL, "Russian");

    VECTOR2 a(2, 3), b(4, 1), c;
    cout << "VECTOR2" << endl;
    cout << "a = ";
    a.display();
    cout << "b = ";
    b.display();

    c = a + b;
    cout << "a + b = ";
    c.display();

    c = a - b;
    cout << "a - b = ";
    c.display();

    cout << "Скалярное произведение a * b = " << a * b << endl;

    if (a == b)
        cout << "a и b равны" << endl;
    else
        cout << "a и b не равны" << endl;

    c = a.multiplyByNumber(2);
    cout << "a * 2 = ";
    c.display();

    c = b.divideByNumber(2);
    cout << "b / 2 = ";
    c.display();

    c = c.calcVector(3, a, 2, b);
    cout << "c = 3*a + 2*b = ";
    c.display();

    cout << endl;

    VECTOR3 d(1, 2, 3), e(4, 5, 6), f;
    cout << "VECTOR3" << endl;
    cout << "d = ";
    d.display();
    cout << "e = ";
    e.display();

    f = d + e;
    cout << "d + e = ";
    f.display();

    f = d - e;
    cout << "d - e = ";
    f.display();

    cout << "Скалярное произведение d * e = " << d * e << endl;

    if (d == e)
cout << "d и e равны" << endl;
    else
        cout << "d и e не равны" << endl;

    f = d.multiplyByNumber(2);
    cout << "d * 2 = ";
    f.display();

    f = e.divideByNumber(2);
    cout << "e / 2 = ";
    f.display();

    f = f.calcVector(2, d, 3, e);
    cout << "f = 2*d + 3*e = ";
    f.display();

    return 0;
}