Создать иерархию геометрических фигур - C#
Формулировка задачи:
Задача
- напишите иерархию геометрических фигур
- сложите несколько разных фигур в массив
- Подсчитайте и выведите на консоль, сколько в массиве кругов,
квадратов, ромбов, треугольников и пр.
- Для каждой фигуры выведите на консоль количество углов этой фигуры
Решение задачи: «Создать иерархию геометрических фигур»
textual
Листинг программы
- using System;
- namespace Space
- {
- using Type = Types.FigureType;
- struct Types
- {
- public enum FigureType { Circle, Square, Rhombus, Triangle }
- }
- class Figure
- {
- public Type Type { get; protected set; }
- public int Corners { get; protected set; }
- }
- class Circle : Figure
- {
- public Circle()
- {
- Corners = 0;
- Type = Type.Circle;
- }
- }
- class Square : Figure
- {
- public Square()
- {
- Corners = 4;
- Type = Type.Square;
- }
- }
- class Rhombus : Figure
- {
- public Rhombus()
- {
- Corners = 4;
- Type = Type.Rhombus;
- }
- }
- class Triangle : Figure
- {
- public Triangle()
- {
- Corners = 3;
- Type = Type.Triangle;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Figure[] figures = new Figure[4];
- figures[0] = new Circle();
- figures[1] = new Square();
- figures[2] = new Rhombus();
- figures[3] = new Triangle();
- int c = 0, s = 0, r = 0, t = 0;
- foreach (Figure f in figures)
- {
- switch (f.Type)
- {
- case Type.Circle: c++; break;
- case Type.Rhombus: r++; break;
- case Type.Square: s++; break;
- case Type.Triangle: t++; break;
- }
- }
- Console.WriteLine("Circles: " + c + "\tCorners: " + new Circle().Corners);
- Console.WriteLine("Squares: " + s + "\tCorners: " + new Square().Corners);
- Console.WriteLine("Rhombuses: " + r + "\tCorners: " + new Rhombus().Corners);
- Console.WriteLine("Triangles: " + t + "\tCorners: " + new Triangle().Corners);
- Console.ReadLine();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д