Tvor0zhok

СиАКОД 14.1

Mar 30th, 2022 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     struct SPoint : IComparable<SPoint> // описание структуры точки в 3-мерном пр-ве
  7.     {
  8.         public int x, y, z;  // поля-данные: координаты точки (x, y, z)
  9.         public SPoint(int x, int y, int z)  // конструктор
  10.         {
  11.             this.x = x;
  12.             this.y = y;
  13.             this.z = z;
  14.         }
  15.  
  16.         public void Show()  // вывод данных о точке на экран
  17.         {
  18.             Console.WriteLine("({0}, {1}, {2})\t\t{3:f2}", x, y, z, Distance());
  19.         }
  20.  
  21.         public double Distance() // расстояние от начала координат до точки
  22.         {
  23.             return Math.Sqrt(x * x + y * y + z * z);
  24.         }
  25.  
  26.         public int CompareTo(SPoint obj)
  27.         {
  28.             return Distance().CompareTo(obj.Distance());
  29.         }
  30.     }
  31.  
  32.     class Program
  33.     {
  34.         static void Print(SPoint[] points) // Выводим данные о точках в виде таблицы
  35.         {
  36.             Console.WriteLine("{0, -24}{1, -30}", "Координаты точки", "Расстояние от начала координат");
  37.  
  38.             foreach (SPoint p in points) p.Show();
  39.         }
  40.  
  41.         static SPoint[] Input(string fileName) // Читаем данные из файла
  42.         {
  43.             using (StreamReader file = new StreamReader(fileName))
  44.             {
  45.                 int n = int.Parse(file.ReadLine());
  46.                 SPoint[] points = new SPoint[n];
  47.  
  48.                 for (int i = 0; i < n; ++i)
  49.                 {
  50.                     string[] coordinates = file.ReadLine().Split(' ');
  51.                     points[i] = new SPoint(int.Parse(coordinates[0]),
  52.                                             int.Parse(coordinates[1]),
  53.                                             int.Parse(coordinates[2]));
  54.                 }
  55.  
  56.                 return points;
  57.             }
  58.         }
  59.  
  60.         static void Main()
  61.         {
  62.             SPoint[] points = Input("C:/Users/Acer/Desktop/СиАКОД/Задание 14/input1.txt");
  63.             Print(points);
  64.  
  65.             SPoint ans = points[0]; // точка-ответ на задачу
  66.  
  67.             foreach (SPoint p in points)
  68.                 if (ans.CompareTo(p) < 0) ans = p;
  69.  
  70.             Console.WriteLine("\nНаиболее удаленная точка:");
  71.             ans.Show();
  72.         }
  73.     };
  74. }
  75.  
  76. /* input1.txt
  77. 10
  78. 60 14 -8
  79. -89 -1 65
  80. -32 -27 24
  81. 59 87 -55
  82. 98 -39 2
  83. -44 -24 -70
  84. 41 -3 -12
  85. 17 -69 -61
  86. 20 36 -80
  87. 97 27 -53
  88. */
Add Comment
Please, Sign In to add comment