Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HW
- {
- abstract class Shape : IComparable<Shape>
- {
- private string name;
- public string Name { get { return name; } }
- public Shape(string name)
- {
- this.name = name;
- }
- public abstract double Area();
- public abstract double Perimeter();
- public int CompareTo(Shape s)
- {
- Shape shape = s as Shape;
- if (shape != null)
- return this.Area().CompareTo(shape.Area());
- else
- throw new Exception("���������� �������� ��� �������");
- }
- }
- class Circle : Shape
- {
- private double radius;
- public double Radius { get { return radius; } }
- public Circle(string name, double radius) : base(name)
- {
- this.radius = radius;
- }
- public override double Area()
- {
- return Math.PI * (this.radius * this.radius);
- }
- public override double Perimeter()
- {
- return this.radius * (2 * Math.PI);
- }
- }
- class Square : Shape
- {
- private double a;
- public double A { get { return a; } }
- public Square(string name, double a) : base(name)
- {
- this.a = a;
- }
- public override double Area()
- {
- return a * this.a;
- }
- public override double Perimeter()
- {
- return 4 * this.a;
- }
- }
- class Program
- {
- static void Main()
- {
- List<Shape> shapes = new List<Shape>();
- for (int i = 0; i < 4; i++)
- {
- Console.WriteLine("What shape should it be? \n" +
- "S - Square : C - Circle\n" +
- "If you want to stop entering shapes press e");
- string tempShape = Console.ReadLine();
- if (tempShape == "s" || tempShape == "S")
- {
- Console.Write("Enter length : ");
- double tempShapePar = Convert.ToDouble(Console.ReadLine());
- shapes.Add(new Square($"Square {i}", tempShapePar));
- }
- else if (tempShape == "c" || tempShape == "C")
- {
- Console.Write("Enter radius : ");
- double tempShapePar = Convert.ToDouble(Console.ReadLine());
- shapes.Add(new Circle($"Circle {i}", tempShapePar));
- }
- else if (tempShape == "e")
- {
- break;
- }
- else
- {
- i--;
- Console.WriteLine("You have entered wrong char. Try again");
- }
- }
- Console.WriteLine("Do you want to find largest perimiter?\n" +
- "Y - yes : N - no");
- string TempPer = Console.ReadLine(); ;
- bool ans = false;
- while (!ans)
- {
- if ((TempPer == "y" || TempPer == "Y") && shapes.Count > 0)
- {
- ans = true;
- double tempPerimeter = 0;
- int tempCounterTrue = 0;
- int tempCounter = 0;
- foreach (var i in shapes)
- {
- if (tempPerimeter < i.Perimeter())
- {
- tempPerimeter = i.Perimeter();
- tempCounterTrue = tempCounter;
- }
- tempCounter++;
- }
- Console.WriteLine(shapes[tempCounterTrue].Name + " has the biggest perimeter");
- }
- else if ((TempPer == "y" || TempPer == "Y") && shapes.Count <= 0)
- {
- Console.WriteLine("Not enough items in list");
- ans = true;
- }
- else if (TempPer == "n" || TempPer == "N")
- {
- ans = true;
- }
- else
- {
- Console.WriteLine("Wrong answer. Try again.");
- }
- }
- Console.WriteLine("Do you want to sort list and print it?\n" +
- "Y - yes : N - no");
- bool istempSort = false;
- string SortAns = Console.ReadLine();
- while (!istempSort)
- if ((SortAns == "y" || SortAns == "Y") && shapes.Count > 0)
- {
- istempSort = true;
- shapes.Sort();
- foreach (var i in shapes)
- {
- Console.WriteLine($"{i.Name}, Area = {i.Area()}, Perimeter = {i.Perimeter()}");
- }
- }
- else if ((SortAns == "y" || SortAns == "Y") && shapes.Count <= 0)
- {
- Console.WriteLine("Not enough items in list");
- istempSort = true;
- }
- else if (SortAns == "n" || SortAns == "N")
- {
- istempSort = true;
- Console.WriteLine("Not enough items in list");
- }
- else
- {
- Console.WriteLine("Wrong answer. Try again.");
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement