Сортировка по параметрам и поиск по значению в массиве - C#
Формулировка задачи:
1) сортировка: отсортировать фигуры по типам сначала квадраты потом треугольники, круги
2) поиск: определить есть ли фигура с площадью > 10
Код:
using System;
using System.Linq;
class TwoDShape
{
double pri_size;
public TwoDShape()
{
Size = 0.0;
name = "null";
}
public TwoDShape(double x, string n)
{
Size = x;
name = n;
}
public TwoDShape(TwoDShape ob)
{
Size = ob.Size;
name = ob.name;
}
public double Size
{
get { return pri_size; }
set { pri_size = value < 0 ? -value : value; }
}
public string name { get; set; }
public virtual double Area()
{
Console.WriteLine("Метод Area() должен быть переопределен");
return 0.0;
}
}
class Triangle : TwoDShape
{
string Style;
public Triangle()
{
Style = "null";
}
public Triangle(string s, double x) :
base(x, "Треугольник")
{
Style = s;
}
public Triangle(double x) : base(x, "Треугольник")
{
Style = "Равносторонний";
}
public override double Area()
{
return (Math.Sqrt(3) / 4) * (Size * Size);
}
public void ShowStyle()
{
Console.WriteLine("Треугольник " + Style);
}
}
class Rectangle : TwoDShape
{
public Rectangle(double x) :
base(x, "Квадрат")
{ }
public Rectangle(Rectangle ob) : base(ob) { }
public override double Area()
{
return Size * Size;
}
}
class Circle : TwoDShape
{
public Circle(double x) :
base(x, "Круг")
{ }
public Circle(Circle ob) : base(ob) { }
public override double Area()
{
return Math.PI * (Size * Size);
}
}
class DynShapes
{
static void Main()
{
TwoDShape[] shapes = new TwoDShape[10];
shapes[0] = new Triangle(2.0);
shapes[1] = new Rectangle(10.0);
shapes[2] = new Circle(3.0);
shapes[3] = new Rectangle(4.0);
shapes[4] = new Triangle(7.0);
shapes[5] = new Circle(5.0);
shapes[6] = new Rectangle(6.0);
shapes[7] = new Triangle(1.0);
shapes[8] = new Circle(8.0);
shapes[9] = new Circle(10.0);
Console.WriteLine("\tПлощадь\t\0|Параметр|Обьект\n"); //вывод общий
Console.WriteLine(new string('=', 50));
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0,17}|{1,8}|{2,0}", shapes[i].Area(), shapes[i].Size, shapes[i].name);
Console.WriteLine();
}
Array.Sort(shapes, new Comparison<TwoDShape>(CompareShapes));
Console.WriteLine("параметры по возрастанию.\n\tПлощадь\t\0|Параметр|Обьект\n "); //вывод сортированных параметров
Console.WriteLine(new string('=', 50));
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0,17}|{1,8}|{2,0}", shapes[i].Area(), shapes[i].Size, shapes[i].name);
Console.WriteLine();
}
Array.Sort(shapes, new Comparison<TwoDShape>(CompareArea)); //вывод сортированной площади (убыв)
Console.WriteLine("убывание площади\n\tПлощадь\t\0|Параметр|Обьект\n ");
Console.WriteLine(new string('=', 50));
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0,17}|{1,8}|{2,0}", shapes[i].Area(), shapes[i].Size, shapes[i].name);
Console.WriteLine();
}
}
private static int CompareShapes(TwoDShape x, TwoDShape y) //сортированных параметров
{
return x.Size.CompareTo(y.Size);
}
private static int CompareArea(TwoDShape z, TwoDShape w) //сортированной площади (убыв)
{
return w.Area().CompareTo(z.Area());
}
}
спасибо покорно за помощь с поиском разобрался, помогите с сортировкой , действительно не понимаю
Решение задачи: «Сортировка по параметрам и поиск по значению в массиве»
textual
Листинг программы
private static int TypesComparison(TwoDShape x, TwoDShape y)
{
var xCircle = x as Circle;
var xTriangle = x as Triangle;
var xRectangle = x as Rectangle;
var yCircle = y as Circle;
var yTriangle = y as Triangle;
var yRectangle = y as Rectangle;
if (xCircle != null)
{
if (yCircle != null)
return 0;
return -1;
}
if (xTriangle != null)
{
if (yTriangle != null)
return 0;
if (yRectangle != null)
return -1;
return 1;
}
if (xRectangle != null)
{
if (yRectangle != null)
return 0;
return 1;
}
throw new Exception();
}