Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Point_Triangle
- {
- [Serializable]
- abstract public class Essence:IComparable<Essence>
- {
- public string Name { get; set; }
- public int CompareTo(Essence item)
- {
- return this.Distance().CompareTo(item.Distance());
- }
- public Essence(string name)
- {
- this.Name = name;
- }
- abstract public double Distance();
- abstract public void Set(params int[] ar);
- }
- }
- using System;
- namespace Point_Triangle
- {
- [Serializable]
- public class PointPlane:Essence
- {
- public int X { get; set; }
- public int Y { get; set; }
- public PointPlane(int x, int y):base("Точка")
- {
- this.X = x;
- this.Y = y;
- }
- public override string ToString()
- {
- return $"({X}, {Y})";
- }
- public override double Distance()
- {
- return Math.Sqrt(X * X + Y * Y);
- }
- public override void Set(params int[] ar)
- {
- if (ar.Length == 2)
- {
- X = ar[0];
- Y = ar[1];
- }
- else
- {
- throw new Exception("Неверное количество аргументов");
- }
- }
- }
- }
- using System;
- namespace Point_Triangle
- {
- [Serializable]
- class PointSpace:PointPlane
- {
- public int Z { get; set; }
- public PointSpace(int x, int y,int z)
- : base(x,y)
- {
- this.Z = z;
- }
- public override string ToString()
- {
- return $"({X}, {Y}, {Z})";
- }
- public override double Distance()
- {
- return Math.Sqrt(X * X + Y * Y+Z*Z);
- }
- public override void Set(params int[] ar)
- {
- if (ar.Length == 3)
- {
- X = ar[0];
- Y = ar[1];
- Z = ar[2];
- }
- else
- {
- throw new Exception("Неверное количество аргументов");
- }
- }
- }
- }
- using System;
- namespace Point_Triangle
- {
- [Serializable]
- class Segment : Essence
- {
- public PointPlane Begin { get; set; }
- public PointPlane End { get; set; }
- public Segment(int x_begin, int y_begin, int x_end, int y_end)
- : base("Отрезок")
- {
- Begin = new PointPlane(x_begin, y_begin);
- End = new PointPlane(x_end, y_end);
- }
- public override string ToString()
- {
- return $"{Begin}-{End}";
- }
- public override double Distance()
- {
- return (Begin.Distance() >= End.Distance()) ? Begin.Distance() : End.Distance();
- }
- public override void Set(params int[] ar)
- {
- if (ar.Length == 4)
- {
- Begin.Set(ar[0], ar[1]);
- End.Set(ar[2], ar[3]);
- }
- else
- {
- throw new Exception("Неверное количество аргументов");
- }
- }
- public double Length
- {
- get
- {
- return Math.Sqrt(Math.Pow(Begin.X - End.X, 2) + Math.Pow(Begin.Y - End.Y, 2));
- }
- }
- }
- }
- //основная прога
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Collections.Generic;
- namespace Point_Triangle
- {
- class Program
- {
- static void Print(List<Essence> objects)
- {
- if (objects.Count == 0)
- {
- Console.WriteLine("Список объектов пуст.");
- }
- else
- {
- Console.WriteLine("Список объектов:");
- foreach (Essence item in objects)
- {
- Console.Write(item.Name+" ");
- Console.WriteLine(item);
- switch (item.Name)
- {
- case "Точка":
- Console.WriteLine("Расстояние от точки до начала координат: " + item.Distance());
- break;
- case "Отрезок":
- Console.WriteLine("Расстояние от ближайшей точки отрезка до начала координат: " +
- item.Distance());
- Console.WriteLine("Длина отрезка " + ((Segment)item).Length);
- break;
- case "Треугольник":
- Console.WriteLine("Расстояние от ближайшей точки треугольника до начала координат: " +
- item.Distance());
- Console.WriteLine("Периметр треугольника " + ((Triangle)item).Perimeter);
- break;
- }
- }
- }
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- List<Essence> objects = new List<Essence>();
- BinaryFormatter formatter = new BinaryFormatter();
- using (FileStream f = new FileStream("d:/Point-Triangle-ser/Point-Triangle/input.dat",
- FileMode.OpenOrCreate))
- {
- if (f.Length != 0)
- {
- objects = (List<Essence>)formatter.Deserialize(f);
- }
- }
- Print(objects);
- objects.Add(new PointPlane(1, 10));
- objects.Add(new PointSpace(1, 1, 1));
- objects.Add(new Segment(-1, 4, 2, 2));
- objects.Add(new Triangle(1, 1, 2, 2, 3, 3));
- Print(objects);
- objects.Sort();
- Print(objects);
- using (FileStream f = new FileStream("d:/Point-Triangle-ser/Point-Triangle/input.dat",
- FileMode.OpenOrCreate))
- {
- formatter.Serialize(f, objects);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement