Guest User

Untitled

a guest
Feb 1st, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. #include <string>
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. #include <math.h>
  8. #include <vector>
  9. #include <utility>
  10.  
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. using namespace std;
  15.  
  16. struct trng
  17. {
  18. int v1;
  19. int v2;
  20. int v3;
  21. double sq;
  22. };
  23.  
  24. class SquareTrngl
  25. {
  26. public:
  27. SquareTrngl() {}
  28. pair<double,double> p;
  29.  
  30. struct trng Vertx;
  31.  
  32. vector < pair <double,double> > PointsTrngl;
  33. vector < struct trng > SqTrg;
  34.  
  35. /////////////////////
  36. void InputData()
  37. {
  38. srand (time(NULL));
  39. cout << "Введите количество точек = "; cin >> Pts;
  40.  
  41. if (!PointsTrngl.empty()) PointsTrngl.clear();
  42.  
  43. int t= Pts; int x, y;
  44. while (t)
  45. {
  46. x= rand() % 100 - 50;
  47. p.first= (double) x; p.first/=10.0;
  48.  
  49. y= rand() % 100 - 50;
  50. p.second= (double) y; p.second/=10.0;
  51.  
  52. PointsTrngl.push_back(p);
  53. t--;
  54. }
  55. }
  56. ///////////////
  57. void OutCoord()
  58. {
  59. cout << setw(10) << "Х коорд" << setw(10) << "Y коорд" << endl;
  60. cout << setw(50) << setfill('=') << "=" << endl;
  61. cout << setfill(' ');
  62.  
  63. int t= 0;
  64. while(t<PointsTrngl.size())
  65. {
  66. p= PointsTrngl[t];
  67. cout << setw(10) << setprecision(2) << p.first;
  68. cout << setw(10) << setprecision(2) << p.second;
  69. cout << endl;
  70. t++;
  71. }
  72. }
  73. //////////////
  74. void MakeTrg()
  75. {
  76. int v1, v2, v3;
  77.  
  78. for (v1=0; v1<(Pts-1); v1++)
  79. {
  80. for (v2= v1+1; v2<Pts; v2++)
  81. {
  82. for (v3= v2+1; v3<Pts; v3++)
  83. {
  84. Vertx.v1= v1;
  85. Vertx.v2= v2;
  86. Vertx.v3= v3;
  87. Vertx.sq= 0.0;
  88. SqTrg.push_back(Vertx);
  89. }
  90. }
  91. }
  92. }
  93. void OutTrgSq()
  94. {
  95. cout << setw(10) << "Вершина 1";
  96. cout << setw(10) << "Вершина 2";
  97. cout << setw(10) << "Вершина 3";
  98. cout << setw(10) << "Площадь" << endl;
  99. cout << setw(50) << setfill('=') << "=" << endl;
  100. cout << setfill(' ');
  101.  
  102. int t= 0;
  103. while(t<SqTrg.size())
  104. {
  105. Vertx= SqTrg[t];
  106. cout << setw(10) << Vertx.v1;
  107. cout << setw(10) << Vertx.v2;
  108. cout << setw(10) << Vertx.v3;
  109. cout << setw(10) << Vertx.sq;
  110. cout << endl;
  111. t++;
  112. }
  113. }
  114. /////////////////
  115. void SquareUnit()
  116. {
  117. double pp, ss; double x1,x2,x3,y1,y2,y3; double a,b,c;
  118.  
  119. int t= 0;
  120.  
  121. while (t<SqTrg.size())
  122. {
  123. Vertx= SqTrg[t];
  124. p= PointsTrngl[Vertx.v1];
  125. x1= p.first; y1= p.second;
  126.  
  127. p= PointsTrngl[Vertx.v2];
  128. x2= p.first; y2= p.second;
  129.  
  130. p= PointsTrngl[Vertx.v3];
  131. x3= p.first; y3= p.second;
  132.  
  133. a= (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); a= sqrt(a);
  134. b= (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2); b= sqrt(b);
  135. c= (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3); c= sqrt(c);
  136.  
  137. pp= (a+b+c)/2.0;
  138. ss= sqrt( pp*(pp-a)*(pp-b)*(pp-c) );
  139.  
  140. Vertx.sq= ss;
  141. SqTrg[t]= Vertx;
  142.  
  143. t++;
  144. }
  145. }
  146. ///////////////////////
  147. void IsTrngl()
  148. {
  149. int t= 0; double minS;
  150.  
  151. //Находим первый невырожденный треугольник с ненулевой площадью
  152.  
  153. while(t<SqTrg.size())
  154. {
  155. Vertx= SqTrg[t];
  156. if (Vertx.sq!=0) { minS= Vertx.sq; break; }
  157. t++;
  158. }
  159.  
  160. // Сортируем по минимальной площади
  161.  
  162. while(t<SqTrg.size())
  163. {
  164. Vertx= SqTrg[t];
  165. if (Vertx.sq < minS && Vertx.sq!=0) { minS= Vertx.sq; mnSq= t; }
  166. t++;
  167. }
  168.  
  169. cout << endl << "Треугольник с минимальной площадью имеет парметры" << endl;
  170.  
  171. Vertx= SqTrg[mnSq];
  172.  
  173. p= PointsTrngl[Vertx.v1];
  174. cout << setw(10) << "Вершина 1";
  175. cout << setw(10) << "X1= " << setw(10) << p.first << setw(10) << "Y1= " << p.second << endl;
  176.  
  177. p= PointsTrngl[Vertx.v2];
  178. cout << setw(10) << "Вершина 2";
  179. cout << setw(10) << "X2= " << setw(10) << p.first << setw(10) << "Y2= " << p.second << endl;
  180.  
  181. p= PointsTrngl[Vertx.v3];
  182. cout << setw(10) << "Вершина 3";
  183. cout << setw(10) << "X3= " << setw(10) << p.first << setw(10) << "Y3= " << p.second << endl;
  184.  
  185. cout << setw(10) << "Площадь= " << setw(10) << Vertx.sq << endl;
  186.  
  187. cout << setw(50) << setfill('=') << "=" << endl;
  188. cout << setfill(' ');
  189. }
  190. ///////////////////////
  191. ~SquareTrngl() {}
  192.  
  193. protected:
  194. int Pts;
  195. double xPt;
  196. double yPt;
  197. int mnSq;
  198. private:
  199. };
  200.  
  201. int main(int argc, char **argv)
  202. {
  203. system("chcp 1251 > nul"); // Руссификация сообщений
  204. setlocale(LC_ALL, "Russian");
  205.  
  206. SquareTrngl st; st.InputData(); st.OutCoord();
  207.  
  208. st.MakeTrg(); st.SquareUnit(); st.OutTrgSq();
  209.  
  210. st.IsTrngl();
  211.  
  212. cout << endl << endl;
  213. system("pause"); // system("pause > nul");
  214. return 0;
  215. }
  216.  
  217.  
Advertisement
Add Comment
Please, Sign In to add comment