Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- struct Point {
- using point_t = tuple<double, double, double>;
- double x;
- double y;
- double z;
- Point() : x(0), y(0), z(0) {}
- Point(double x, double y, double z) : x(x), y(y), z(z) {}
- double length(const Point& p)const {
- return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2) + pow(p.z - z, 2));
- }
- friend istream& operator>>(istream& in, Point& p) {
- cout << "x: ";
- in >> p.x;
- cout << "y: ";
- in >> p.y;
- cout << "z: ";
- in >> p.z;
- return in;
- }
- friend ostream& operator<<(ostream& out, const Point& p) {
- return out << "{ " << p.x << ", " << p.y << ", " << p.z << " }";
- }
- };
- class Vector3d {
- public:
- Vector3d() : p_(Point{}) {}
- Vector3d(const Point p) : p_(p) {}
- void set(const Point p) {
- p_ = p;
- }
- double length()const {
- Point p;
- return p_.length(p);
- }
- private:
- Point p_;
- friend Vector3d operator+(const Vector3d& a, const Vector3d& b) {
- Point p;
- p.x = a.p_.x + b.p_.x;
- p.y = a.p_.y + b.p_.y;
- p.z = a.p_.z + b.p_.z;
- return { p };
- }
- friend Vector3d operator-(const Vector3d& a, const Vector3d& b) {
- Point p;
- p.x = a.p_.x - b.p_.x;
- p.y = a.p_.y - b.p_.y;
- p.z = a.p_.z - b.p_.z;
- return { p };
- }
- friend double operator*(const Vector3d& a, const Vector3d& b) {
- return a.p_.x * b.p_.x + a.p_.y * b.p_.y + a.p_.z * b.p_.z;
- }
- friend Vector3d operator*(const Vector3d& v, const double n) {
- Point p;
- p.x = v.p_.x * n;
- p.y = v.p_.y * n;
- p.z = v.p_.z * n;
- Vector3d t(p);
- return t;
- }
- friend Vector3d operator*(const double n, const Vector3d& v) {
- Point p;
- p.x = v.p_.x * n;
- p.y = v.p_.y * n;
- p.z = v.p_.z * n;
- Vector3d t(p);
- return t;
- }
- friend bool operator<(const Vector3d& a, const Vector3d& b) {
- return a.length() < b.length();
- }
- friend bool operator==(const Vector3d& a, const Vector3d& b) {
- return a.length() == b.length();
- }
- friend bool operator!=(const Vector3d& a, const Vector3d& b) {
- return !(a == b);
- }
- friend bool operator<=(const Vector3d& a, const Vector3d& b) {
- return a < b || a == b;
- }
- friend bool operator>(const Vector3d& a, const Vector3d& b) {
- return !(a <= b);
- }
- friend bool operator>=(const Vector3d& a, const Vector3d& b) {
- return !(a < b);
- }
- friend istream& operator>>(istream& in, Vector3d& v) {
- return in >> v.p_;
- }
- friend ostream& operator<<(ostream& out, const Vector3d& v) {
- return out << v.p_;
- }
- };
- void start_menu() {
- system("cls");
- puts("1) ввод векторов");
- puts("2) вывод векторов");
- puts("3) сумма векторов");
- puts("4) разность векторов");
- puts("5) умножение векторов");
- puts("6) сравнение векторов");
- puts("7) очистить содержимое");
- puts("0) выход из программы");
- }
- void compare(const Vector3d& a, const Vector3d& b) {
- if (a == b) {
- cout << a << " == " << b << '\n';
- cout << a << " <= " << b << '\n';
- cout << a << " => " << b << '\n';
- } else {
- cout << a << " != " << b << '\n';
- if (a < b) cout << a << " < " << b << '\n';
- else cout << a << " > " << b << '\n';
- }
- }
- int main() {
- system("chcp 1251 > nul");
- while (true) {
- start_menu();
- Vector3d a, b;
- auto flag = false;
- char point;
- while (true) {
- switch (point = _getch()) {
- case '0': return 0;
- case '1': cin >> a >> b; break;
- case '2': cout << a << '\n' << b << '\n'; break;
- case '3': cout << "Сумма: " << a + b << '\n'; break;
- case '4': cout << "Разность: " << a - b << '\n'; break;
- case '5': cout << "Произведение: " << a * b << '\n'; break;
- case '6': compare(a, b); break;
- case '7': flag = !flag; break;
- default: puts("Вы ошиблись при выборе пункта меню!\a");
- }
- if (flag) break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement