Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба3 / Laba 3 (1)
.cpp#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;
class Vector5D {
private:
double coords[5];
public:
Vector5D(double a = 0, double b = 0, double c = 0, double d = 0, double e = 0) {
coords[0] = a; coords[1] = b; coords[2] = c; coords[3] = d; coords[4] = e;
}
// Сложение векторов
Vector5D operator+(const Vector5D& other) const {
return Vector5D(
coords[0] + other.coords[0],
coords[1] + other.coords[1],
coords[2] + other.coords[2],
coords[3] + other.coords[3],
coords[4] + other.coords[4]
);
}
// Вычитание векторов
Vector5D operator-(const Vector5D& other) const {
return Vector5D(
coords[0] - other.coords[0],
coords[1] - other.coords[1],
coords[2] - other.coords[2],
coords[3] - other.coords[3],
coords[4] - other.coords[4]
);
}
// Скалярное произведение
double operator*(const Vector5D& other) const {
double result = 0;
for (int i = 0; i < 5; i++) {
result += coords[i] * other.coords[i];
}
return result;
}
// Векторное произведение (операция %)
Vector5D operator%(const Vector5D& other) const {
return Vector5D(
coords[1] * other.coords[2] - coords[2] * other.coords[1],
coords[2] * other.coords[0] - coords[0] * other.coords[2],
coords[0] * other.coords[1] - coords[1] * other.coords[0],
0, 0 // В 5D векторное произведение определено только для первых 3 координат
);
}
// Умножение на скаляр
Vector5D operator*(double scalar) const {
return Vector5D(
coords[0] * scalar,
coords[1] * scalar,
coords[2] * scalar,
coords[3] * scalar,
coords[4] * scalar
);
}
friend Vector5D operator*(double scalar, const Vector5D& vec);
void print() const {
cout << "(";
for (int i = 0; i < 5; i++) {
cout << coords[i];
if (i < 4) cout << ", ";
}
cout << ")" << endl;
}
double& operator[](int index) { return coords[index]; }
const double& operator[](int index) const { return coords[index]; }
};
Vector5D operator*(double scalar, const Vector5D& vec) {
return vec * scalar;
}
int main() {
SetConsoleOutputCP(65001);
cout << "=== Задача 3.1 - Вектор в 5D пространстве ===" << endl;
Vector5D v1(1, 2, 3, 4, 5);
Vector5D v2(5, 4, 3, 2, 1);
cout << "Вектор v1: "; v1.print();
cout << "Вектор v2: "; v2.print();
Vector5D sum = v1 + v2;
cout << "Сумма v1 + v2: "; sum.print();
Vector5D diff = v1 - v2;
cout << "Разность v1 - v2: "; diff.print();
double dot = v1 * v2;
cout << "Скалярное произведение v1 * v2: " << dot << endl;
Vector5D cross = v1 % v2;
cout << "Векторное произведение v1 % v2: "; cross.print();
Vector5D scaled = v1 * 2.5;
cout << "v1 * 2.5: "; scaled.print();
return 0;
}
Соседние файлы в папке Лаба3
