Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // VectorClass
  4. //
  5. // Created by Олег on 20.03.17.
  6. // Copyright © 2017 Олег. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <math.h>
  11. using namespace std;
  12.  
  13. class VectorClass
  14. {
  15. private:
  16. float x,y,length;
  17. int* vector;
  18.  
  19. public:
  20. VectorClass()
  21. {
  22. this->set(rand()% (100 - 1 + 1) + 1,rand()% (100 - 1 + 1) + 1);
  23. }
  24. VectorClass(VectorClass const &VectorClass2)
  25. {
  26. printf("\nСкопированный вектор - ");
  27. this->set(VectorClass2.x, VectorClass2.y);
  28. }
  29. VectorClass(float x,float y)
  30. {
  31. this->set(x, y);
  32. }
  33.  
  34. //Перегружаем операторы
  35. friend ostream& operator<<(ostream& os, const VectorClass &v)
  36. {
  37. os << "Vector:[" << v.x << ":" << v.y << "] ";
  38. return os;
  39. }
  40. friend istream& operator>> ( istream& is, VectorClass &v)
  41. {
  42. printf("Введите X: ");
  43. is >> v.x;
  44. printf("Введите Y: ");
  45. is >> v.y;
  46. v.VectorToArray();
  47. return is;
  48. }
  49.  
  50. bool operator==(const VectorClass &VectorClass2)
  51. {
  52. if(this->x == VectorClass2.x && this->y == VectorClass2.y)
  53. return true;
  54. else
  55. return false;
  56. }
  57. int operator*(const VectorClass &VectorClass2)
  58. {
  59. if(IsNullVector(VectorClass2))
  60. return echo("nv");
  61.  
  62. return (this->x * VectorClass2.x) + (this->y * VectorClass2.y);
  63. }
  64. int operator*(int k)
  65. {
  66. if(IsNullVector() && k > 0)
  67. return echo("nv");
  68.  
  69. return (this->x * k) + (this->y * k);
  70. }
  71. int operator[](int i)
  72. {
  73. return this->vector[i];
  74. }
  75. int operator+(const VectorClass &VectorClass2)
  76. {
  77. return (this->x + VectorClass2.x) * (this->y + VectorClass2.y);
  78. }
  79. int operator-(const VectorClass &VectorClass2)
  80. {
  81. return (this->x - VectorClass2.x) * (this->y - VectorClass2.y);
  82. }
  83. bool echo(string type)
  84. {
  85. if(type == "nv")
  86. printf("ОШИБКА! Один из векторов нулевой!");
  87. return true;
  88. }
  89. void set(float x = 0, float y = 0)
  90. {
  91. this->x = x;
  92. this->y = y;
  93.  
  94. this->init();
  95. }
  96. void init()
  97. {
  98. cout << *this << "СОЗДАН" << endl;
  99. this->length = pow(pow(this->x,2) + pow(this->y,2),0.5);
  100. this->VectorToArray();
  101. }
  102. void VectorToArray()
  103. {
  104. // if(*this->vector)
  105. // delete [] this->vector;
  106.  
  107. for(int x = 0; x < this->y-1; x++)
  108. this->vector[x] = this->x+x;
  109. }
  110. bool IsNullVector(const VectorClass &Vector = VectorClass())
  111. {
  112. if(this->length <= 0 || Vector.length <= 0)
  113. return true;
  114. else
  115. return false;
  116. }
  117. };
  118. int main() {
  119.  
  120. cout << "[#V1] = "; VectorClass V1;
  121. cout << "[#V1] "; cin >> V1;
  122. cout << "[#V1]"; cout << V1;
  123.  
  124. cout << V1[1];
  125.  
  126.  
  127. // int xy[2];
  128. //
  129. // //v1
  130. // printf("\nВведите x для вектора V1:");
  131. // cin >> xy[0];
  132. // printf("Введите y для вектора V1:");
  133. // cin >> xy[1];
  134. // VectorClass Vector1(xy[0],xy[1]);
  135. //
  136. // //V2
  137. // printf("\nВведите x для вектора V2:");
  138. // cin >> xy[0];
  139. // printf("Введите y для вектора V2:");
  140. // cin >> xy[1];
  141. // VectorClass Vector2(xy[0],xy[1]);
  142. //
  143. // //V3
  144. // VectorClass Vector3(Vector1);
  145. //
  146. // printf("\nСумма V1 + V2 = %d; \nСкалярной произведение V1 * V3 = %d; \nV1 * 2 = %d\n",
  147. // Vector2+Vector1,Vector1*Vector3,Vector1*2);
  148. //
  149. // 1
  150. // if(Vector3 == Vector1)
  151. // printf("\nV3 и V1 равны\n");
  152.  
  153.  
  154.  
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement