Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. class Vector {
  5. double components[4] = {}; // массив из 4 элементов ct, x, y, z
  6.  
  7. public:
  8. Vector() {}
  9. Vector(double ct, double x, double y, double z) {
  10. components[0] = ct;
  11. components[1] = x;
  12. components[2] = y;
  13. components[3] = z;
  14. }
  15. Vector operator+(const Vector &other) { // к текущему ветору прибавляется еще один
  16. Vector result; // создание нового вектора - результата.
  17. for (int i = 0; i < 4; i++) {
  18. result.components[i] = components[i] +
  19. other.components[i]; // поэлементное сложение текущего векторая и того что справа от него
  20. }
  21. return result;
  22. }
  23.  
  24. Vector operator-(const Vector &other) { // то же самое только с минусом
  25. Vector result; // создание нового вектора - результата.
  26. for (int i = 0; i < 4; i++) {
  27. result.components[i] = components[i] -
  28. other.components[i]; // поэлементное сложение текущего векторая и того что справа от него
  29. }
  30. return result;
  31. }
  32.  
  33. double interval(const Vector &other) { // это используется так: double x = v1.interval(v2);
  34. Vector diff = *this - other; // вычитаем из текущего вектора тот который подали в аргументе
  35. return pow(diff.components[0], 2) -
  36. pow(diff.components[1], 2) -
  37. pow(diff.components[2], 2) -
  38. pow(diff.components[3], 2);
  39. }
  40. };
  41.  
  42. int main() {
  43. Vector v1(1,2,3,4);
  44. Vector v2(2,2,2,2);
  45. std::cout << v1.interval(v2) << std::endl;
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement