Advertisement
damesova

Figures Drawing

Oct 23rd, 2022
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | Source Code | 0 0
  1. //ИЗЧЕРТАВАНЕ НА ПРАВОЪГЪЛНИК И ТРИЪГЪЛНИК СЪС ЗВЕЗДИЧКА
  2.  
  3. //ИНТЕРФЕЙС DRAWABLE:
  4. public interface Drawable
  5.     {
  6.         void Draw();
  7.     }
  8.  
  9. //КЛАС RECTANGLE:
  10. public class Rectangle : Drawable
  11.     {
  12.         private int Width;
  13.         private int Height;
  14.  
  15.         public Rectangle(int Width, int Height)
  16.         {
  17.             this.Width = Width;
  18.             this.Height = Height;
  19.         }
  20.         public void Draw()
  21.         {
  22.             DrawLine(this.Width, '*', '*');
  23.             for (int i = 1; i < this.Height - 1; ++i)
  24.                 DrawLine(this.Width, '*', ' ');
  25.             DrawLine(this.Width, '*', '*');
  26.         }
  27.  
  28.         private void DrawLine(int width, char end, char mid)
  29.         {
  30.             Console.Write(end);
  31.             for (int i = 1; i < width - 1; ++i)
  32.                 Console.Write(mid);
  33.             Console.WriteLine(end);
  34.         }
  35.     }
  36.  
  37. //КЛАС TRIANGLE:
  38. public class Triangle : Drawable
  39.     {
  40.        
  41.         private int Side;
  42.  
  43.         public Triangle(int Side)
  44.         {
  45.             this.Side = Side;
  46.         }
  47.  
  48.         public void Draw()
  49.         {
  50.             for (var row = 1; row <= this.Side; row++)
  51.             {
  52.                 Console.Write("*");
  53.                 for (var col = 1; col < row; col++)
  54.                 {
  55.                     Console.Write(" *");
  56.                 }
  57.                 Console.WriteLine();
  58.             }
  59.  
  60.         }
  61.     }
  62.  
  63. //ГЛАВНА ПРОГРАМА:
  64. public class Program
  65.     {
  66.         static void Main(string[] args)
  67.         {
  68.             Drawable rect = new Rectangle(5,10);
  69.             Console.WriteLine("Drawing rectangle like this:");
  70.             rect.Draw();
  71.  
  72.             Drawable tr = new Triangle(5);
  73.             Console.WriteLine("Drawing triangle like this:");
  74.             tr.Draw();
  75.         }
  76.     }
  77.  
  78.  
  79. //РЕЗУЛТАТ:
  80. Drawing rectangle like this:
  81. *****
  82. *   *
  83. *   *
  84. *   *
  85. *   *
  86. *   *
  87. *   *
  88. *   *
  89. *   *
  90. *****
  91. Drawing triangle like this:
  92. *
  93. * *
  94. * * *
  95. * * * *
  96. * * * * *
Tags: C#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement