Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.IO;
- using System.Collections.Generic;
- class Program
- {
- struct SPoint : IComparable<SPoint>
- {
- public double x, y;
- public SPoint(double x, double y)
- {
- this.x = x;
- this.y = y;
- }
- public void Print()
- {
- Console.WriteLine("({0};{1})", x, y);
- }
- public static double Distance(SPoint PointA, SPoint PointB)
- {
- return Math.Round(Math.Abs(Math.Sqrt(Math.Pow(PointB.x - PointA.x, 2) + Math.Pow(PointB.y - PointA.y, 2))),3);
- }
- public int CompareTo(SPoint Other)
- {
- if (this.x == Other.x)
- {
- return 0;
- }
- else if (this.x > Other.x)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- }
- static void Main(string[] args)
- {
- using (StreamReader ReadFile = new StreamReader("../../Data.txt", Encoding.GetEncoding(1251)))
- {
- double Max = 0;
- double Dist=0;
- string str;
- string[] Parts;
- List<SPoint> ListPoints = new List<SPoint>();
- var a = ReadFile.EndOfStream;
- char[] div = { ';', ':', ' ' };
- while (!ReadFile.EndOfStream)
- {
- str = ReadFile.ReadLine();
- Parts = str.Split(div, StringSplitOptions.RemoveEmptyEntries);
- try // try cath для обработки исключений если вдруг что то пойдет по пизде, если хочешь убери не поломает если не будешь косячить в коде, или оставь и объясни если спросит
- {
- if (Parts.Length == 2)
- {
- SPoint Point = new SPoint( Convert.ToDouble(Parts[0]), Convert.ToDouble(Parts[1]));
- ListPoints.Add(Point);
- }
- else
- {
- Console.WriteLine("Не точка: {0}", str);
- }
- }
- catch
- {
- //выводим в какой нибудь файл с логами
- //Console.WriteLine("Неверный формат данных");
- }
- }
- ListPoints.Sort();
- foreach (SPoint item in ListPoints)
- {
- item.Print();
- }
- Console.WriteLine("Отсортированная изначальная последовательность точек по X");
- Console.WriteLine();
- try
- {
- List<SPoint> Final = new List<SPoint>();
- SPoint temp = ListPoints[0];
- for (int i = 0; i < ListPoints.Count; i++) //начинаем перебирать все точки внешний цикл берет одну точку
- {
- for (int j = 0; j < ListPoints.Count; j++)//а тут мерить дистанции до всех других точек
- {
- if (i!=j )//не считая саму себя
- {
- Dist += SPoint.Distance(ListPoints[i], ListPoints[j]);
- }
- }
- if (Max == Dist) //если Попались точки с одинаковыми расстояниями что очень редко может случиться
- {
- Max = Dist;
- temp = ListPoints[i];
- Final.Add(temp);
- }
- if (Max < Dist)//Если Дистанция максимальная меньше той которая на данный момент измерена то ее задаем как макс и заносим с список
- {
- Final.Clear();
- Max = Dist;
- temp = ListPoints[i];
- Final.Add(temp);
- }
- Dist = 0;
- }
- Console.WriteLine("Точки конечные");
- foreach (SPoint item in Final)
- {
- item.Print();
- }
- }
- catch
- {
- Console.WriteLine("Файле нет точек, либо он пуст");
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement