Advertisement
Guest User

Untitled

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