Advertisement
NickAndNick

Vector3d

Nov 7th, 2020
2,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4. struct Point {
  5.     using point_t = tuple<double, double, double>;
  6.     double x;
  7.     double y;
  8.     double z;
  9.     Point() : x(0), y(0), z(0) {}
  10.     Point(double x, double y, double z) : x(x), y(y), z(z) {}
  11.     double length(const Point& p)const {
  12.         return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2) + pow(p.z - z, 2));
  13.     }
  14.     friend istream& operator>>(istream& in, Point& p) {
  15.         cout << "x: ";
  16.         in >> p.x;
  17.         cout << "y: ";
  18.         in >> p.y;
  19.         cout << "z: ";
  20.         in >> p.z;
  21.         return in;
  22.     }
  23.     friend ostream& operator<<(ostream& out, const Point& p) {
  24.         return out << "{ " << p.x << ", " << p.y << ", " << p.z << " }";
  25.     }
  26. };
  27. class Vector3d {
  28. public:
  29.     Vector3d() : p_(Point{}) {}
  30.     Vector3d(const Point p) : p_(p) {}
  31.     void set(const Point p) {
  32.         p_ = p;
  33.     }
  34.     double length()const {
  35.         Point p;
  36.         return p_.length(p);
  37.     }
  38. private:
  39.     Point p_;
  40.     friend Vector3d operator+(const Vector3d& a, const Vector3d& b) {
  41.         Point p;
  42.         p.x = a.p_.x + b.p_.x;
  43.         p.y = a.p_.y + b.p_.y;
  44.         p.z = a.p_.z + b.p_.z;
  45.         return { p };
  46.     }
  47.     friend Vector3d operator-(const Vector3d& a, const Vector3d& b) {
  48.         Point p;
  49.         p.x = a.p_.x - b.p_.x;
  50.         p.y = a.p_.y - b.p_.y;
  51.         p.z = a.p_.z - b.p_.z;
  52.         return { p };
  53.     }
  54.     friend double operator*(const Vector3d& a, const Vector3d& b) {
  55.         return a.p_.x * b.p_.x + a.p_.y * b.p_.y + a.p_.z * b.p_.z;
  56.     }
  57.     friend Vector3d operator*(const Vector3d& v, const double n) {
  58.         Point p;
  59.         p.x = v.p_.x * n;
  60.         p.y = v.p_.y * n;
  61.         p.z = v.p_.z * n;
  62.         Vector3d t(p);
  63.         return t;
  64.     }
  65.     friend Vector3d operator*(const double n, const Vector3d& v) {
  66.         Point p;
  67.         p.x = v.p_.x * n;
  68.         p.y = v.p_.y * n;
  69.         p.z = v.p_.z * n;
  70.         Vector3d t(p);
  71.         return t;
  72.     }
  73.     friend bool operator<(const Vector3d& a, const Vector3d& b) {
  74.         return a.length() < b.length();
  75.     }
  76.     friend bool operator==(const Vector3d& a, const Vector3d& b) {
  77.         return a.length() == b.length();
  78.     }
  79.     friend bool operator!=(const Vector3d& a, const Vector3d& b) {
  80.         return !(a == b);
  81.     }
  82.     friend bool operator<=(const Vector3d& a, const Vector3d& b) {
  83.         return a < b || a == b;
  84.     }
  85.     friend bool operator>(const Vector3d& a, const Vector3d& b) {
  86.         return !(a <= b);
  87.     }
  88.     friend bool operator>=(const Vector3d& a, const Vector3d& b) {
  89.         return !(a < b);
  90.     }
  91.     friend istream& operator>>(istream& in, Vector3d& v) {
  92.         return in >> v.p_;
  93.     }
  94.     friend ostream& operator<<(ostream& out, const Vector3d& v) {
  95.         return out << v.p_;
  96.     }
  97. };
  98. void start_menu() {
  99.     system("cls");
  100.     puts("1) ввод векторов");
  101.     puts("2) вывод векторов");
  102.     puts("3) сумма векторов");
  103.     puts("4) разность векторов");
  104.     puts("5) умножение векторов");
  105.     puts("6) сравнение векторов");
  106.     puts("7) очистить содержимое");
  107.     puts("0) выход из программы");
  108. }
  109. void compare(const Vector3d& a, const Vector3d& b) {
  110.     if (a == b) {
  111.         cout << a << " == " << b << '\n';
  112.         cout << a << " <= " << b << '\n';
  113.         cout << a << " => " << b << '\n';
  114.     } else {
  115.         cout << a << " != " << b << '\n';
  116.         if (a < b) cout << a << " < " << b << '\n';
  117.         else cout << a << " > " << b << '\n';
  118.     }
  119. }
  120. int main() {
  121.     system("chcp 1251 > nul");
  122.     while (true) {
  123.         start_menu();
  124.         Vector3d a, b;
  125.         auto flag = false;
  126.         char point;
  127.         while (true) {
  128.             switch (point = _getch()) {
  129.             case '0':  return 0;
  130.             case '1': cin >> a >> b; break;
  131.             case '2': cout << a << '\n' << b << '\n'; break;
  132.             case '3': cout << "Сумма: " << a + b << '\n'; break;
  133.             case '4': cout << "Разность: " << a - b << '\n'; break;
  134.             case '5': cout << "Произведение: " << a * b << '\n'; break;
  135.             case '6': compare(a, b); break;
  136.             case '7': flag = !flag; break;
  137.             default: puts("Вы ошиблись при выборе пункта меню!\a");
  138.             }
  139.             if (flag) break;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement