Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdarg.h>
  4.  
  5. using namespace std;
  6.  
  7. class Vector;
  8. bool kollin2 (Vector &j, Vector &m);
  9.  
  10. class Vector
  11. {
  12. private:
  13. int amount; //Количество координат
  14. double coordinates [100]; //Координаты
  15.  
  16. public:
  17.  
  18. void SetAmount (int n) //Методы для координат и необходимого количества
  19. {
  20. amount = n;
  21. }
  22. void SetCoordinates (double value, int i)
  23. {
  24. coordinates [i] = value;
  25. }
  26.  
  27. double& operator[](int i) {
  28. return coordinates[i];
  29. }
  30.  
  31. int GetAmount () const
  32. {
  33. return amount;
  34. }
  35. int GetCoordinates (int i) const //Координаты
  36. {
  37. return coordinates [i];
  38. }
  39. const Vector operator+ (const Vector& v)
  40. {
  41. int i;
  42.  
  43. Vector tmp=v;
  44.  
  45. for (i=0; i<this->amount; i++)
  46.  
  47. tmp.coordinates [i] = coordinates[i]+v.GetCoordinates(i);
  48. return tmp;
  49. }
  50.  
  51. const Vector operator- (const Vector& v)
  52. {
  53. int i;
  54.  
  55. Vector tmp=v;
  56.  
  57. for (i=0; i<this->amount; i++)
  58.  
  59. tmp.coordinates [i] = coordinates[i]-v.GetCoordinates(i);
  60. return tmp;
  61. }
  62.  
  63. friend Vector operator- (const Vector& v) {
  64. Vector tmp = v;
  65.  
  66. for (int i=0; i<v.amount; i++)
  67. tmp.coordinates [i] = -v.coordinates[i];
  68.  
  69. return tmp;
  70.  
  71. }
  72.  
  73. friend Vector operator+ (const Vector& v) {
  74. Vector tmp = v;
  75.  
  76. for (int i=0; i<v.amount; i++)
  77. tmp.coordinates [i] = +v.coordinates[i];
  78.  
  79. return tmp;
  80.  
  81. }
  82.  
  83. bool operator== (const Vector& v)
  84. {
  85. int flag = 0;
  86. for (int i=0; i<this->amount; i++){
  87.  
  88. if(this->coordinates[i] == v.coordinates[i]) {}
  89. else
  90. flag = 1;
  91. }
  92. if (flag != 0)
  93. return false;
  94.  
  95. return true;
  96. }
  97.  
  98. bool operator!= (const Vector& v)
  99. {
  100. int flag = 0;
  101. for (int i=0; i<amount; i++){
  102.  
  103. if(coordinates[i] != v.coordinates[i]) {
  104. flag = 1;
  105. }
  106. }
  107. if (flag == 0)
  108. return false;
  109.  
  110. return true;
  111. }
  112.  
  113. double operator* (const Vector& v)
  114. {
  115. int i;
  116.  
  117. Vector tmp=v;
  118. double m=0;
  119.  
  120. for (i=0; i<this->amount; i++){
  121.  
  122. tmp.coordinates [i] = coordinates[i]*v.GetCoordinates(i);
  123. m=m+tmp.coordinates[i];};
  124. return m;
  125. }
  126.  
  127. const Vector operator/ (const Vector& v)
  128. {
  129.  
  130. Vector tmp=v;
  131. double prom=1;
  132. for (int i=0; i<this->amount; i++){
  133. if(coordinates[i]!=0&&v.GetCoordinates(i)!=0) prom=tmp.coordinates[i];}
  134. for (int i=0; i<this->amount; i++){
  135. if(coordinates[i]==0&&v.GetCoordinates(i)==0) tmp.coordinates[i]=prom;
  136. else {tmp.coordinates [i] = coordinates[i]/v.GetCoordinates(i);
  137. prom=tmp.coordinates[i];}}
  138. return tmp;
  139. }
  140.  
  141. bool dioph_eq(double a, double b, double c) {
  142. return true;
  143. }
  144.  
  145. bool ifCollinear (const Vector& v) {
  146. Vector tmp = *this/v;
  147. int cdm=tmp.GetAmount()-1;
  148. for (int i=0; i<cdm; i++){
  149. if (tmp.GetCoordinates(i)!=tmp.GetCoordinates(i+1)) return false;
  150. }
  151. return true;
  152. }
  153.  
  154. int GCD(int a, int b) {
  155. while(a != b) {
  156. if(a > b)
  157. a -= b;
  158. else
  159. a += b;
  160. }
  161. return a;
  162. }
  163.  
  164. bool ifCoplanar(Vector& v2, Vector& v3) {
  165.  
  166. int flag, c, gcdValue;
  167. flag = 0;
  168. for (int i=0; i<amount; i++) {
  169. gcdValue = GCD(v2.coordinates[i], v3.coordinates[i]);
  170. c = coordinates[i];
  171. if (gcdValue % c != 0)
  172. flag = 1;
  173. }
  174. if (flag == 0)
  175. return true;
  176.  
  177. return false;
  178. }
  179.  
  180. };
  181.  
  182. ostream& operator <<(ostream& out , Vector& v) {
  183. for(int i = 0; i < v.GetAmount(); i++) {
  184. out << v[i] << " ";
  185. }
  186. return out;
  187.  
  188. }
  189.  
  190. istream& operator >>(istream& in, Vector& v) {
  191. for(int i = 0; i < v.GetAmount(); i++) {
  192. in >> v[i];
  193. }
  194. return in;
  195. }
  196.  
  197.  
  198.  
  199. bool kollinsys (int n, ...){
  200. va_list args;
  201. va_start(args, n);
  202.  
  203. Vector v = va_arg(args, Vector);
  204. for(int i = 0; i < n-1; i++) {
  205. Vector u = va_arg(args, Vector);
  206. if(!u.ifCollinear(v)) return false;
  207. }
  208.  
  209. va_end(args);
  210. return true;
  211. }
  212.  
  213. int main ()
  214. {
  215. setlocale (LC_ALL,"rus");
  216. Vector Vector1;
  217. Vector Vector2;
  218. Vector Vector3;
  219. Vector Vector4;
  220. Vector Vector5;
  221.  
  222. int n;
  223. int i;
  224. double value;
  225.  
  226. cout << "Введите количество координат векторов:\n";
  227. cin >> n;
  228. Vector1.SetAmount (n);
  229. cout << "Введите координаты вектора 1:\n";
  230. cin >> Vector1;
  231. Vector2.SetAmount (n);
  232. cout << "Введите координаты вектора 2:\n";
  233. cin >> Vector2;
  234. Vector3.SetAmount (n);
  235. cout << "Введите координаты вектора 3:\n";
  236. cin >> Vector3;
  237.  
  238. Vector sumVector;
  239. Vector resVector;
  240. sumVector=Vector1+Vector2;
  241. resVector=Vector1-Vector2;
  242. cout << "\nВектор 1:\n" << Vector1 << "\n";
  243. cout << "Вектор 2:\n" << Vector2 << "\n";
  244. cout << "Вектор 3:\n" << Vector3 << "\n\n";
  245.  
  246. cout<<"Вычитаем из вектора 1 вектор 2 = "<<resVector<<"\n";
  247. cout<<"Складываем вектор 1 с вектором 2 = "<<sumVector<<"\n";
  248.  
  249. double m=Vector1*Vector2;
  250. cout<< "Скалярное произведение векторов 1 и 2: " <<m;
  251.  
  252. Vector4 = -Vector1;
  253. cout<< "\nОтрицательный вектор 1: " << Vector4 << endl;
  254.  
  255. bool a, b;
  256. a=Vector2==Vector1;
  257. b=Vector2!=Vector1;
  258. if (a==true && b==false)
  259. cout<<"Векторы 1 и 2 равны\n";
  260. else
  261. cout<<"Векторы 1 и 2 неравны\n";
  262.  
  263. cout << "\nВектор 1:\n" << Vector1 << "\n";
  264. cout << "Вектор 2:\n" << Vector2 << "\n";
  265.  
  266.  
  267. bool coll1=Vector1.ifCollinear(Vector2);
  268. bool kollinbg=kollinsys(3,Vector1,Vector2,Vector3);
  269. if (coll1 == true)
  270. cout<<"Векторы 1 и 2 коллинеарны\n";
  271. else
  272. cout<<"Векторы 1 и 2 неколлинеарны\n";
  273.  
  274.  
  275. bool cop = Vector1.ifCoplanar(Vector2, Vector3);
  276. if (cop == true)
  277. cout<< "Векторы 1, 2 и 3 компланарны\n";
  278. else
  279. cout<< "Векторы 1, 2 и 3 некомпланарны\n";
  280.  
  281. cout<<"Проверка на коллинеарность системы из векторов 1, 2, 3 = "<<kollinbg<<endl;
  282.  
  283. return 0;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement