Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба8 / Laba 10 (1)
.cpp#include <iostream>
#include <cmath>
#include <locale>
using namespace std;
class Quaternion {
private:
double w, x, y, z;
public:
Quaternion(double w = 0, double x = 0, double y = 0, double z = 0)
: w(w), x(x), y(y), z(z) {}
// Сложение
Quaternion operator+(const Quaternion& other) const {
return Quaternion(w + other.w, x + other.x, y + other.y, z + other.z);
}
// Умножение
Quaternion operator*(const Quaternion& other) const {
return Quaternion(
w * other.w - x * other.x - y * other.y - z * other.z,
w * other.x + x * other.w + y * other.z - z * other.y,
w * other.y - x * other.z + y * other.w + z * other.x,
w * other.z + x * other.y - y * other.x + z * other.w
);
}
// Сопряженный кватернион
Quaternion conjugate() const {
return Quaternion(w, -x, -y, -z);
}
// Норма
double norm() const {
return sqrt(w * w + x * x + y * y + z * z);
}
// Обратный кватернион
Quaternion inverse() const {
double n = norm();
if (n == 0) return *this;
return conjugate() * (1.0 / (n * n));
}
// Вывод
void print() const {
cout << w << " + " << x << "i + " << y << "j + " << z << "k" << endl;
}
// Умножение на скаляр
Quaternion operator*(double scalar) const {
return Quaternion(w * scalar, x * scalar, y * scalar, z * scalar);
}
};
int main() {
setlocale(LC_ALL, "ru_RU.UTF-8");
Quaternion q1(1, 2, 3, 4);
Quaternion q2(2, 3, 4, 5);
cout << "Кватернион 1: "; q1.print();
cout << "Кватернион 2: "; q2.print();
Quaternion sum = q1 + q2;
cout << "Сумма: "; sum.print();
Quaternion product = q1 * q2;
cout << "Произведение: "; product.print();
cout << "Норма q1: " << q1.norm() << endl;
return 0;
}
Соседние файлы в папке Лаба8
