Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Variant5
  5. {
  6.     class Program
  7.     {
  8.         static Random rnd = new Random();
  9.         static void Main()
  10.         {
  11.             do
  12.             {
  13.                 int n;
  14.                 do
  15.                 {
  16.                     Console.WriteLine("Введите целую неотрицательную длинну массива");
  17.                 } while (!int.TryParse(Console.ReadLine(), out n) || (n <= 0));
  18.  
  19.                 List<Vector> vectors = CreateList(n);
  20.  
  21.                 Vector q = CreateQ();
  22.  
  23.                 List<Vector> collVectors = GetCollList(vectors, q);
  24.                 if (collVectors.Count == 0)
  25.                 {
  26.                     Console.WriteLine("Коллиниарные векторы не обнаружены");
  27.                 }
  28.                 else
  29.                 {
  30.                     PrintList(vectors);
  31.                     Console.WriteLine();
  32.                     PrintList(collVectors);
  33.                 }
  34.  
  35.                 Console.WriteLine("Введите ESC, чтобы закончить...");
  36.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  37.         }
  38.  
  39.         static List<Vector> CreateList(int n)
  40.         {
  41.             List<Vector> vectors = new List<Vector>();
  42.             for (int i = 0; i < n; i++)
  43.             {
  44.                 int x1 = rnd.Next(0, 11);
  45.                 int y1 = rnd.Next(0, 11);
  46.                 int x2 = rnd.Next(0, 11);
  47.                 int y2 = rnd.Next(0, 11);
  48.                 Vector vector = new Vector(x1, y1, x2, y2);
  49.                 vectors.Add(vector);
  50.             }
  51.             return vectors;
  52.         }
  53.  
  54.         static Vector CreateQ()
  55.         {
  56.             int x;
  57.             int y;
  58.             do
  59.             {
  60.                 Console.WriteLine("Задайте ненулевой вектор");
  61.                 do
  62.                 {
  63.                     Console.WriteLine("Введите координату вектора Х");
  64.                 } while (!int.TryParse(Console.ReadLine(), out x));
  65.                 do
  66.                 {
  67.                     Console.WriteLine("Введите координату вектора Y");
  68.                 } while (!int.TryParse(Console.ReadLine(), out y));
  69.             } while ((x == 0) && (y == 0));
  70.  
  71.             Vector q = new Vector(x, y);
  72.             return q;
  73.         }
  74.  
  75.         static List<Vector> GetCollList(List<Vector> vectors, Vector q)
  76.         {
  77.             List<Vector> collVectors = new List<Vector>();
  78.  
  79.             foreach (Vector vector in vectors)
  80.             {
  81.                 if (vector.IsCollinearity(q))
  82.                 {
  83.                     collVectors.Add(vector);
  84.                 }
  85.             }
  86.  
  87.             return collVectors;
  88.         }
  89.  
  90.         static void PrintList(List<Vector> vectors)
  91.         {
  92.             foreach (Vector vector in vectors)
  93.             {
  94.                 Console.WriteLine(vector);
  95.             }
  96.         }
  97.     }
  98.  
  99.     class Vector
  100.     {
  101.         private int x1;
  102.         private int y1;
  103.         private int x2;
  104.         private int y2;
  105.         private int x;
  106.         private int y;
  107.  
  108.         public Vector(int x, int y)
  109.         {
  110.             this.x = x;
  111.             this.y = y;
  112.         }
  113.  
  114.         public Vector(int x1, int y1, int x2, int y2)
  115.         {
  116.             this.x1 = x1;
  117.             this.y1 = y1;
  118.             this.x2 = x2;
  119.             this.y2 = y2;
  120.  
  121.             x = x2 - x1;
  122.             y = y2 - y1;
  123.         }
  124.  
  125.         public int X => x;
  126.  
  127.         public int Y => y;
  128.  
  129.         public double VectorLength
  130.         {
  131.             get
  132.             {
  133.                 return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(x, 2));
  134.             }
  135.         }
  136.  
  137.         public bool IsCollinearity(Vector vector)
  138.         {
  139.             if (x != 0 && y != 0)
  140.             {
  141.                 return (this.x * vector.Y == this.y * vector.X);
  142.             }
  143.             return false;
  144.         }
  145.  
  146.         public override string ToString()
  147.         {
  148.             return $"X = {x}\t Y = {y}\t Length = {this.VectorLength}";
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement