Advertisement
herhor67

Vector3D

Jun 6th, 2019
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include <string>
  4. #include <new>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. class v3d {
  11. public:
  12.     double* arr;
  13.  
  14.     v3d() {
  15.         cout << "Tworzymy... ";
  16.         arr = new double[3];
  17.         cout << this << " gotowe." << endl;
  18.     }
  19.  
  20.     ~v3d() {
  21.         cout << "Usuwamy... " << this;
  22.         delete[] arr;
  23.         cout << " gotowe." << endl;
  24.     }
  25.  
  26.     v3d(const v3d& other)
  27.     {
  28.         arr = new double[3];
  29.         arr[0] = other.arr[0];
  30.         arr[1] = other.arr[1];
  31.         arr[2] = other.arr[2];
  32.     }
  33.  
  34.  
  35.  
  36.     v3d operator + (const v3d& other)
  37.     {
  38.         v3d temp;
  39.         temp.arr[0] = arr[0] + other.arr[0];
  40.         temp.arr[1] = arr[1] + other.arr[1];
  41.         temp.arr[2] = arr[2] + other.arr[2];
  42.         return temp;
  43.     }
  44.     v3d& operator += (const v3d& other)
  45.     {
  46.         arr[0] += other.arr[0];
  47.         arr[1] += other.arr[1];
  48.         arr[2] += other.arr[2];
  49.         return *this;
  50.     }
  51.  
  52.     v3d operator - (const v3d& other)
  53.     {
  54.         v3d temp;
  55.         temp.arr[0] = arr[0] - other.arr[0];
  56.         temp.arr[1] = arr[1] - other.arr[1];
  57.         temp.arr[2] = arr[2] - other.arr[2];
  58.         return temp;
  59.     }
  60.     v3d& operator -= (const v3d& other)
  61.     {
  62.         arr[0] -= other.arr[0];
  63.         arr[1] -= other.arr[1];
  64.         arr[2] -= other.arr[2];
  65.         return *this;
  66.     }
  67.  
  68.     v3d operator * (const v3d& other)
  69.     {
  70.         v3d temp;
  71.         temp.arr[0] = arr[1] * other.arr[2] - arr[2] * other.arr[1];
  72.         temp.arr[1] = arr[2] * other.arr[0] - arr[0] - other.arr[2];
  73.         temp.arr[2] = arr[0] * other.arr[1] - arr[1] - other.arr[0];
  74.         return temp;
  75.     }
  76.  
  77.  
  78.  
  79.     v3d& operator = (const v3d& other)
  80.     {
  81.         if (this != &other)
  82.         {
  83.             arr[0] = other.arr[0];
  84.             arr[1] = other.arr[1];
  85.             arr[2] = other.arr[2];
  86.         }
  87.         return *this;
  88.     }
  89.  
  90.     double len() {
  91.         return pow(pow(arr[0], 2) + pow(arr[1], 2) + pow(arr[2], 2), 0.5);
  92.     }
  93.  
  94.     string to_str() {
  95.         return "[" + to_string(arr[0]) + ", " + to_string(arr[1]) + ", " + to_string(arr[2]) + "]";
  96.     }
  97. };
  98.  
  99.  
  100. int main()
  101. {
  102.     v3d a, b, sum;
  103.  
  104.     a.arr[0] = 12;
  105.     a.arr[1] = -56;
  106.     a.arr[2] = 5e-3;
  107.  
  108.     b.arr[0] = 7 - 3e2;
  109.     b.arr[1] = 24.789;
  110.     b.arr[2] = 1;
  111.  
  112.     sum = a*b;
  113.  
  114.     cout << sum.to_str() << endl << sum.len() << endl;
  115.     system("pause");
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement