Advertisement
ostapdontstop

saslan

May 7th, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Figure
  5. {
  6.     abstract class Figure
  7.     {
  8.         public abstract double Area();
  9.  
  10.         public abstract double Perimeter();
  11.  
  12.         public abstract void ShowInfo();
  13.     }
  14.  
  15.     class Rectangle : Figure
  16.     {
  17.         double a, b;
  18.  
  19.         public Rectangle(double a, double b)
  20.         {
  21.             this.a = a;
  22.             this.b = b;
  23.         }
  24.  
  25.         public override double Area()
  26.         {
  27.             return a * b;
  28.         }
  29.  
  30.         public override double Perimeter()
  31.         {
  32.             return (a + b) * 2;
  33.         }
  34.  
  35.         public override void ShowInfo()
  36.         {
  37.             Console.WriteLine("Прямоугольник со сторонами {0} и {1}", a, b);
  38.         }
  39.     }
  40.  
  41.     class Triangle : Figure
  42.     {
  43.         double a, b, c;
  44.  
  45.         public Triangle(double a, double b, double c)
  46.         {
  47.             this.a = a;
  48.             this.c = c;
  49.             this.b = b;
  50.         }
  51.  
  52.         public override double Perimeter()
  53.         {
  54.             return a + b + c;
  55.         }
  56.  
  57.  
  58.         public override double Area()
  59.         {
  60.             double p = Perimeter() / 2;
  61.             return Math.Sqrt(p * (p - a) * (p - b) * (p - c));
  62.         }
  63.  
  64.         public override void ShowInfo()
  65.         {
  66.             Console.WriteLine("Треугольник со сторонами {0}, {1}, {2}", a, b, c);
  67.         }
  68.     }
  69.  
  70.     class Circle : Figure
  71.     {
  72.         const double pi = 3.14;
  73.  
  74.         double r;
  75.  
  76.         public Circle(double r)
  77.         {
  78.             this.r = r;
  79.         }
  80.  
  81.         public override double Area()
  82.         {
  83.             return pi * r * r;
  84.         }
  85.  
  86.         public override double Perimeter()
  87.         {
  88.             return 2 * pi * r;
  89.         }
  90.  
  91.         public override void ShowInfo()
  92.         {
  93.             Console.WriteLine("Круг c радиусом {0}.", r);
  94.         }
  95.     }
  96.  
  97.     class Program
  98.     {
  99.         static void Main()
  100.         {
  101.             Figure[] arFigs = {
  102.                 new Rectangle(2,4),
  103.                 new Triangle(2,3,4),
  104.                 new Circle(5)
  105.             };
  106.  
  107.             for (int i = 0; i < arFigs.Length; i++)
  108.             {
  109.                 arFigs[i].ShowInfo();
  110.                 Console.Write("Периметр: {0}; Площадь: {1}\n\n", arFigs[i].Perimeter(), arFigs[i].Area());
  111.             }
  112.         }
  113.  
  114.  
  115.     }
  116.  
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement