Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1.Создать абстрактный класс Figure с методами вычисления площади и периметра, а также методом, выводящим информацию о фигуре на экран.
- 2.Создать производные классы: Rectangle (прямоугольник), Circle (круг), Triangle (треугольник) со своими методами вычисления площади и периметра.
- 3.Реализовать метод CompareTo, для сравнения объектов по площади.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Win32;
- using System.IO;
- namespace prakt_10
- {
- abstract public class Figure : IComparable
- {
- abstract public double Area(); // get area
- abstract public double Perimeter(); //get perimetr
- abstract public void Show(); //show information
- abstract public void ShowArea(); // return area only
- public int CompareTo(object obj)
- {
- Figure b = (Figure)obj;
- if (this.Area() == b.Area())
- {
- return 0;
- }
- else
- {
- if (this.Area() > b.Area())
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- }
- }
- class Trangle : Figure
- {
- private readonly string name = "Trangle";
- private int x1, y1;
- private int x2, y2;
- private int x3, y3;
- public Trangle(int x1, int y1, int x2, int y2, int x3, int y3)
- {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- this.x3 = x3;
- this.y3 = y3;
- }
- public int lengthSegment(int x2, int x1, int y2, int y1)
- {
- return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- }
- public override double Area()
- {
- int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
- int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
- int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
- double pPerimetr = segment3 + segment1 + segment2;
- return pPerimetr * Math.Sqrt((pPerimetr - segment1) * (pPerimetr - segment2) * (pPerimetr - segment3));
- }
- public override double Perimeter()
- {
- int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
- int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
- int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
- return segment2 + segment1 + segment3;
- }
- public override void Show()
- {
- Console.WriteLine("Name figure:{0}", this.name);
- Console.WriteLine("Point coordinates:");
- Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
- Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
- Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
- Console.WriteLine("Length of a segment: {0}, {1}, {2}, area: {3}, perimetr: {4}",
- this.lengthSegment(this.x2, this.x1, this.y2, this.y1),
- this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
- this.lengthSegment(this.x1, this.x3, this.y1, this.y3),
- this.Area(), this.Perimeter());
- Console.WriteLine();
- }
- public override void ShowArea()
- {
- Console.WriteLine("Area {0} = {1}",this.name, this.Area());
- }
- }
- class Rectangle : Figure
- {
- private readonly string name = "Rectangle";
- private int x1, y1;
- private int x2, y2;
- private int x3, y3;
- private int x4, y4;
- public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
- {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- this.x3 = x3;
- this.y3 = y3;
- this.x4 = x4;
- this.y4 = y4;
- }
- public int lengthSegment(int x2, int x1, int y2, int y1)
- {
- return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- }
- public override double Area()
- {
- int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
- int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
- return (segment2 * segment1);
- }
- public override double Perimeter()
- {
- int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
- int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
- return ((segment2 + segment1) * 2);
- }
- public override void Show()
- {
- Console.WriteLine("Name figure:{0}", this.name);
- Console.WriteLine("Point coordinates:");
- Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
- Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
- Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
- Console.WriteLine("Point 4: ({0};{1})", this.x4, this.y4);
- Console.WriteLine("Length of a segment: length {0}, width: {1}, area: {2}, perimetr: {3}",
- this.lengthSegment(this.x2, this.x1, this.y2, this.y1),
- this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
- this.Area(), this.Perimeter());
- Console.WriteLine();
- }
- public override void ShowArea()
- {
- Console.WriteLine("Area {0} = {1}", this.name, this.Area());
- }
- }
- class Circle : Figure
- {
- private readonly string name = "Circle";
- private int x, y;
- private double radius;
- public Circle(int x, int y, int radius)
- {
- this.x = x;
- this.y = y;
- this.radius = radius;
- }
- public override double Area()
- {
- return Math.PI * this.radius * this.radius;
- }
- public override double Perimeter()
- {
- return Math.PI * this.radius * 2;
- }
- public override void Show()
- {
- Console.WriteLine("Name figure:{0}", this.name);
- Console.WriteLine("Point coordinates:");
- Console.WriteLine("Point: ({0};{1})", this.x, this.y);
- Console.WriteLine("Area: {0}, perimetr: {1}", this.Area(), this.Perimeter());
- Console.WriteLine();
- }
- public override void ShowArea()
- {
- Console.WriteLine("Area {0} = {1}", this.name, this.Area());
- }
- }
- class Programm
- {
- static void Main(string[] args)
- {
- Console.Write("Enter n: ");
- int n = int.Parse(Console.ReadLine());
- Figure[] array = new Figure[n];
- using (StreamReader file = new StreamReader("../../data.txt"))
- {
- for (int i = 0; i < n; i++)
- {
- string[] line = file.ReadLine().Split(' ');
- switch (line[0])
- {
- case "Rectangle":
- array[i] = new Rectangle(
- int.Parse(line[1]),
- int.Parse(line[2]),
- int.Parse(line[3]),
- int.Parse(line[4]),
- int.Parse(line[5]),
- int.Parse(line[6]),
- int.Parse(line[7]),
- int.Parse(line[8]));
- break;
- case "Trangle":
- array[i] = new Trangle(
- int.Parse(line[1]),
- int.Parse(line[2]),
- int.Parse(line[3]),
- int.Parse(line[4]),
- int.Parse(line[5]),
- int.Parse(line[6]));
- break;
- case "Circle":
- array[i] = new Circle(
- int.Parse(line[1]),
- int.Parse(line[2]),
- int.Parse(line[3]));
- break;
- default:
- Console.WriteLine("Keywords not found.");
- break;
- }
- }
- for (int i = 0; i < n; i++)
- {
- array[i].ShowArea();
- }
- Console.WriteLine();
- Array.Sort(array);
- for (int i = 0; i < n; i++)
- {
- array[i].ShowArea();
- }
- Console.ReadKey();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement