Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. class Program
  6. {
  7. struct SPoint : IComparable<SPoint>
  8. {
  9. public double x, y;
  10.  
  11. public SPoint(double x, double y)
  12. {
  13. this.x = x;
  14. this.y = y;
  15. }
  16.  
  17. public void Print()
  18. {
  19. Console.WriteLine("({0};{1})", x, y);
  20. }
  21.  
  22. public static double Distance(SPoint PointA, SPoint PointB)
  23. {
  24. return Math.Round(Math.Abs(Math.Sqrt(Math.Pow(PointB.x - PointA.x, 2) + Math.Pow(PointB.y - PointA.y, 2))),3);
  25. }
  26.  
  27. public int CompareTo(SPoint Other)
  28.  
  29. {
  30. if (this.x == Other.x)
  31. {
  32. return 0;
  33. }
  34. else if (this.x > Other.x)
  35. {
  36. return 1;
  37. }
  38. else
  39. {
  40. return -1;
  41. }
  42. }
  43. }
  44.  
  45. static void Main(string[] args)
  46. {
  47. using (StreamReader ReadFile = new StreamReader("../../Data.txt", Encoding.GetEncoding(1251)))
  48. {
  49. double Max = 0;
  50. double Dist=0;
  51.  
  52. string str;
  53. string[] Parts;
  54. List<SPoint> ListPoints = new List<SPoint>();
  55.  
  56. var a = ReadFile.EndOfStream;
  57. char[] div = { ';', ':', ' ' };
  58. while (!ReadFile.EndOfStream)
  59. {
  60. str = ReadFile.ReadLine();
  61. Parts = str.Split(div, StringSplitOptions.RemoveEmptyEntries);
  62. try // try cath для обработки исключений если вдруг что то пойдет по пизде, если хочешь убери не поломает если не будешь косячить в коде, или оставь и объясни если спросит
  63. {
  64. if (Parts.Length == 2)
  65. {
  66. SPoint Point = new SPoint( Convert.ToDouble(Parts[0]), Convert.ToDouble(Parts[1]));
  67. ListPoints.Add(Point);
  68. }
  69. else
  70. {
  71. Console.WriteLine("Не точка: {0}", str);
  72.  
  73. }
  74. }
  75. catch
  76. {
  77. //выводим в какой нибудь файл с логами
  78. //Console.WriteLine("Неверный формат данных");
  79.  
  80. }
  81. }
  82. ListPoints.Sort();
  83. foreach (SPoint item in ListPoints)
  84. {
  85. item.Print();
  86. }
  87.  
  88. Console.WriteLine("Отсортированная изначальная последовательность точек по X");
  89. Console.WriteLine();
  90. try
  91. {
  92. List<SPoint> Final = new List<SPoint>();
  93. SPoint temp = ListPoints[0];
  94. for (int i = 0; i < ListPoints.Count; i++) //начинаем перебирать все точки внешний цикл берет одну точку
  95. {
  96. for (int j = 0; j < ListPoints.Count; j++)//а тут мерить дистанции до всех других точек
  97. {
  98. if (i!=j )//не считая саму себя
  99. {
  100. Dist += SPoint.Distance(ListPoints[i], ListPoints[j]);
  101. }
  102. }
  103. if (Max == Dist) //если Попались точки с одинаковыми расстояниями что очень редко может случиться
  104. {
  105. Max = Dist;
  106. temp = ListPoints[i];
  107. Final.Add(temp);
  108. }
  109. if (Max < Dist)//Если Дистанция максимальная меньше той которая на данный момент измерена то ее задаем как макс и заносим с список
  110. {
  111. Final.Clear();
  112. Max = Dist;
  113. temp = ListPoints[i];
  114. Final.Add(temp);
  115. }
  116.  
  117. Dist = 0;
  118. }
  119.  
  120. Console.WriteLine("Точки конечные");
  121. foreach (SPoint item in Final)
  122. {
  123. item.Print();
  124. }
  125.  
  126. }
  127. catch
  128. {
  129.  
  130. Console.WriteLine("Файле нет точек, либо он пуст");
  131. }
  132. Console.ReadLine();
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement