Advertisement
Pafnytiu

Классы вектор доделать

May 21st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;
  5. class Vector
  6. {
  7. private:
  8. int x;
  9. int y;
  10. int z;
  11. public:
  12. Vector() : x(0), y(0), z(0)
  13. {}
  14. Vector(int x, int y, int z) : x(x), y(y), z(z)
  15. {}
  16. float Length()
  17. {
  18. return sqrt(float(x*x + y*y + z*z));
  19. }
  20. float ScalarProduct()
  21. {
  22. return float(x + y + z);
  23. }
  24. void Compare(Vector temp)
  25. {
  26. if (this->Length() < temp.Length())
  27. cout << "one < two" << endl;
  28.  
  29.  
  30. if (this->Length() > temp.Length())
  31. {
  32. cout << "one > two" << endl;
  33. }
  34.  
  35. if (this->Length() == temp.Length()) cout << "one = two" << endl;
  36. }
  37. void SetData(int x, int y)
  38. {
  39. this->x = x;
  40. this->y = y;
  41. this->z = z;
  42. }
  43. void ShowVector(string info)
  44. {
  45. cout << "Координаты вектора " << info << " = (" << x << " , " << y << " , " << z << " ) " << endl;
  46. }
  47. Vector operator +(Vector temp)
  48. {
  49. return Vector(x + temp.x, y + temp.y, z + temp.z);
  50. }
  51. Vector operator -(Vector temp)
  52. {
  53. return Vector(x - temp.x, y - temp.y, z - temp.z);
  54. }
  55. Vector operator * (Vector temp)
  56. {
  57. return Vector(x * temp.x, y *temp.y, z * temp.z);
  58. }
  59.  
  60. Vector VectorProduct(Vector temp)
  61. {
  62. return Vector( (y*temp.z - z*temp.y), (x*temp.z - z*temp.x) ,( x * temp.y - y *temp.x) );
  63. }
  64. };
  65. int main()
  66. {
  67. setlocale(0, "");
  68. Vector one(1, 1, 0);
  69. Vector two(2, 3, 0);
  70. one.ShowVector("one");
  71. two.ShowVector("two");
  72. cout << "Длина первого вектора = " << one.Length() << endl;
  73. cout << "Длина второго вектора = " << two.Length() << endl;
  74. Vector three = one + two;
  75. three.ShowVector("three");
  76. cout << "Длина третьего вектора = " << three.Length() << endl;
  77. Vector four = one - two;
  78. four.ShowVector("four");
  79. cout << "Длина четвёртого вектора = " << four.Length() << endl;
  80. Vector scalar = one * two;
  81. scalar.ShowVector("scalar");
  82. cout << "Скалярное произведение = " << scalar.ScalarProduct() << endl;
  83. Vector vector = one.VectorProduct(two);
  84. vector.ShowVector("vector");
  85. one.Compare(two);
  86. system("pause");
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement