Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Variant5
- {
- class Program
- {
- static Random rnd = new Random();
- static void Main()
- {
- do
- {
- int n;
- do
- {
- Console.WriteLine("Введите целую неотрицательную длинну массива");
- } while (!int.TryParse(Console.ReadLine(), out n) || (n <= 0));
- List<Vector> vectors = CreateList(n);
- Vector q = CreateQ();
- List<Vector> collVectors = GetCollList(vectors, q);
- if (collVectors.Count == 0)
- {
- Console.WriteLine("Коллиниарные векторы не обнаружены");
- }
- else
- {
- PrintList(vectors);
- Console.WriteLine();
- PrintList(collVectors);
- }
- Console.WriteLine("Введите ESC, чтобы закончить...");
- } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
- }
- static List<Vector> CreateList(int n)
- {
- List<Vector> vectors = new List<Vector>();
- for (int i = 0; i < n; i++)
- {
- int x1 = rnd.Next(0, 11);
- int y1 = rnd.Next(0, 11);
- int x2 = rnd.Next(0, 11);
- int y2 = rnd.Next(0, 11);
- Vector vector = new Vector(x1, y1, x2, y2);
- vectors.Add(vector);
- }
- return vectors;
- }
- static Vector CreateQ()
- {
- int x;
- int y;
- do
- {
- Console.WriteLine("Задайте ненулевой вектор");
- do
- {
- Console.WriteLine("Введите координату вектора Х");
- } while (!int.TryParse(Console.ReadLine(), out x));
- do
- {
- Console.WriteLine("Введите координату вектора Y");
- } while (!int.TryParse(Console.ReadLine(), out y));
- } while ((x == 0) && (y == 0));
- Vector q = new Vector(x, y);
- return q;
- }
- static List<Vector> GetCollList(List<Vector> vectors, Vector q)
- {
- List<Vector> collVectors = new List<Vector>();
- foreach (Vector vector in vectors)
- {
- if (vector.IsCollinearity(q))
- {
- collVectors.Add(vector);
- }
- }
- return collVectors;
- }
- static void PrintList(List<Vector> vectors)
- {
- foreach (Vector vector in vectors)
- {
- Console.WriteLine(vector);
- }
- }
- }
- class Vector
- {
- private int x1;
- private int y1;
- private int x2;
- private int y2;
- private int x;
- private int y;
- public Vector(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- public Vector(int x1, int y1, int x2, int y2)
- {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- x = x2 - x1;
- y = y2 - y1;
- }
- public int X => x;
- public int Y => y;
- public double VectorLength
- {
- get
- {
- return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(x, 2));
- }
- }
- public bool IsCollinearity(Vector vector)
- {
- if (x != 0 && y != 0)
- {
- return (this.x * vector.Y == this.y * vector.X);
- }
- return false;
- }
- public override string ToString()
- {
- return $"X = {x}\t Y = {y}\t Length = {this.VectorLength}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement