Seal_of_approval

Pr10ex3

Nov 13th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. Program.cs
  2. ______________
  3. using System;
  4.  
  5. namespace test
  6. {
  7.     class Program
  8.     {
  9.         static void Main ()
  10.         {
  11.             Rectangle firstRectangle = new Rectangle (-1,2);
  12.             firstRectangle.Show ();
  13.             Console.WriteLine ();
  14.  
  15.             firstRectangle.A = 4;
  16.             firstRectangle.B = 4;
  17.  
  18.             firstRectangle.Show ();
  19.             if (firstRectangle)
  20.                 Console.WriteLine ("Is Square");
  21.            
  22.             int x = firstRectangle.GetPerimetr();
  23.             int y = firstRectangle.GetSquare();
  24.             Console.WriteLine ("Perimetr = {0}; Square = {1} ",
  25.                 x, y);
  26.            
  27.             Console.WriteLine ();
  28.  
  29.             firstRectangle [0] = 9;
  30.             firstRectangle [1] = 9;
  31.             firstRectangle [2] = 0;
  32.             firstRectangle++;
  33.             firstRectangle.Show ();
  34.  
  35.             firstRectangle *= 4;
  36.             firstRectangle.Show ();
  37.  
  38.             int s = firstRectangle.isSquare;
  39.         }
  40.     }
  41. }
  42.  
  43. ____________________
  44. Rectangle.cs
  45. ____________________
  46. using System;
  47.  
  48. namespace test
  49. {
  50.     public class Rectangle
  51.     {
  52.         private int a;
  53.         private int b;
  54.  
  55.         //конструкторы
  56.         public Rectangle ()
  57.         {
  58.         }
  59.  
  60.         public Rectangle (int a, int b)
  61.         {
  62.             if (a > 0 && b > 0)
  63.             {
  64.                 this.a = a;
  65.                 this.b = b;
  66.             }
  67.             else
  68.                 Console.WriteLine ("Incorrect a or b");
  69.         }
  70.  
  71.         //методы: вывод на экран, подсчет периметра и площади
  72.         public void Show()
  73.         {
  74.             Console.WriteLine ("Rectangle with side a = {0} and side b = {1}", a, b);
  75.         }
  76.  
  77.         public int GetPerimetr()
  78.         {
  79.             return 2*(a+b);
  80.         }
  81.  
  82.         public int GetSquare()
  83.         {
  84.             return a*b;
  85.         }
  86.  
  87.         //свойства
  88.         public int A
  89.         {
  90.             get
  91.             {
  92.                 return a;
  93.             }
  94.             set
  95.             {
  96.                 if (value > 0)
  97.                     a = value;
  98.                 else
  99.                 {
  100.                     a = 0;
  101.                     Console.WriteLine ("Incorrect a");
  102.                 }
  103.             }
  104.         }
  105.  
  106.         public int B
  107.         {
  108.             get
  109.             {
  110.                 return b;
  111.             }
  112.             set
  113.             {
  114.                 if (value > 0)
  115.                     b = value;
  116.                 else
  117.                 {
  118.                     b = 0;
  119.                     Console.WriteLine ("Incorret b");
  120.                 }
  121.             }
  122.         }
  123.  
  124.         public int isSquare
  125.         {
  126.             get
  127.             {
  128.                 if (a == b)
  129.                 {
  130.                     Console.WriteLine("Rectangle is Square");
  131.                     return 0;
  132.                 }
  133.                 else return 0;
  134.             }
  135.         }
  136.  
  137.         //индексатор
  138.         public int this [int i]
  139.         {
  140.             get
  141.             {
  142.                 if (i == 0)
  143.                     return a;
  144.                 else if (i == 1)
  145.                     return b;
  146.                 else
  147.                 {
  148.                     Console.WriteLine ("Error: index");
  149.                     return 0;
  150.                 }
  151.             }
  152.             set
  153.             {
  154.                 if (i == 0 && value > 0)
  155.                     a = value;
  156.                 else if (i == 1 && value > 0)
  157.                     b = value;
  158.                 else
  159.                     Console.WriteLine ("Error: index or value");
  160.             }
  161.         }
  162.  
  163.         //перегрузка операций
  164.         public static Rectangle operator ++ (Rectangle x)
  165.         {
  166.             x.A += 1;
  167.             x.B += 1;
  168.             return x;
  169.         }
  170.  
  171.         public static Rectangle operator -- (Rectangle x)
  172.         {
  173.             x.A -= 1;
  174.             x.B -= 1;
  175.             return x;
  176.         }
  177.  
  178.         public static Rectangle operator * (Rectangle x, int y)
  179.         {
  180.             x.A *= y;
  181.             x.B *= y;
  182.             return x;
  183.         }
  184.  
  185.         public static bool operator true (Rectangle x)
  186.         {
  187.             if (x.a == x.b)
  188.                 return true;
  189.             else
  190.                 return false;
  191.         }
  192.  
  193.         public static bool operator false (Rectangle x)
  194.         {
  195.             if (x.a == x.b)
  196.                 return true;
  197.             else
  198.                 return false;
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment