Advertisement
kirya522

Untitled

Feb 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.74 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using CoolerLibrary;
  4.  
  5. namespace Lab10
  6. {
  7.     public class Triangle
  8.     {
  9.         private double _a;
  10.         private double _b;
  11.         private double _c;
  12.         static int _count = 0;
  13.  
  14.         public double A
  15.         {
  16.             set
  17.             {
  18.                 try
  19.                 {
  20.                     if (value <= 0)
  21.                     {
  22.                         Console.WriteLine("Length must be more than 0");
  23.                     }
  24.                     else
  25.                     {
  26.                         _a = value;
  27.                     }
  28.                 }
  29.                 catch
  30.                 {
  31.                     Console.WriteLine("can't setup the side");
  32.                 }
  33.             }
  34.             get { return _a; }
  35.         }
  36.         public double B
  37.         {
  38.             set
  39.             {
  40.                 try
  41.                 {
  42.                     if (value <= 0)
  43.                     {
  44.                         throw new Exception("Length must be more than 0");
  45.                     }
  46.                     else
  47.                     {
  48.                         _b = value;
  49.                     }
  50.                 }
  51.                 catch
  52.                 {
  53.                     Console.WriteLine("can't setup the side");
  54.                 }
  55.             }
  56.             get { return _b; }
  57.         }
  58.         public double C
  59.         {
  60.             set
  61.             {
  62.                 try
  63.                 {
  64.                     if (value <= 0)
  65.                     {
  66.                         throw new Exception("Length must be more than 0");
  67.                     }
  68.                     else
  69.                     {
  70.                         _c = value;
  71.                     }
  72.                 }
  73.                 catch
  74.                 {
  75.                     Console.WriteLine("can't setup the side");
  76.                 }
  77.             }
  78.             get { return _c; }
  79.         }
  80.  
  81.         public Triangle(double a, double b, double c)
  82.         {
  83.             this._a = a;
  84.             this._b = b;
  85.             this._c = c;
  86.             _count++;
  87.         }
  88.  
  89.         public Triangle(Random rnd)
  90.         {
  91.             //Random rnd= new Random();
  92.             this.A = rnd.Next(1, 30);
  93.             this.B = rnd.Next(1, 30);
  94.             this.C = rnd.Next(1, 30);
  95.             _count++;
  96.         }
  97.  
  98.         public static int Count
  99.         {
  100.             get { return _count; }
  101.         }
  102.  
  103.         public override string ToString()
  104.         {
  105.             string s = System.String.Format("a={0,2}, b={1,2}, c={2,2}",_a,_b,_c);
  106.             return s;
  107.         }
  108.  
  109.         public double Square()
  110.         {
  111.             if ((_a + _b > _c) && (_b + _c > _a) && (_a + _c > _b))
  112.             {
  113.                 double p = (_a + _b + _c) / 2.0;
  114.                 return Math.Sqrt(p * (p - _a) * (p - _b) * (p - _c));
  115.             }
  116.             else
  117.             {
  118.                 return -1;
  119.             }
  120.         }
  121.  
  122.         public static Triangle operator ++(Triangle triangle)
  123.         {
  124.             triangle._a++;
  125.             triangle._b++;
  126.             triangle._c++;
  127.             return triangle;
  128.         }
  129.         public static Triangle operator --(Triangle triangle)
  130.         {
  131.             try
  132.             {
  133.                 triangle._a--;
  134.                 triangle._b--;
  135.                 triangle._c--;
  136.                 if (triangle._a * triangle._b * triangle._c == 0 || triangle._a * triangle._b * triangle._c < 0)
  137.                 {
  138.                     throw new Exception("Side can't be smaller 0");
  139.                 }
  140.                 else
  141.                 {
  142.                     return triangle;
  143.                 }
  144.             }
  145.             catch
  146.             {
  147.                 Console.WriteLine("EXCEPTION: Side can't be smaller 0, no decrease");
  148.                 return triangle++;
  149.             }
  150.            
  151.         }
  152.         public static bool operator >(Triangle tr1, Triangle tr2)
  153.         {
  154.             bool flag = false;
  155.  
  156.             if (tr1.Square() > tr2.Square() && tr1.Square() > 0)
  157.             {
  158.                 flag = true;
  159.             }
  160.             else
  161.             {
  162.                 try
  163.                 {
  164.                     if (tr2.Square() > 0)
  165.                     {
  166.                         flag = false;
  167.                     }
  168.                     else
  169.                     {
  170.                         throw new SystemException("This value can't be matched");
  171.                     }
  172.                 }
  173.                 catch
  174.                 {
  175.                     Console.WriteLine("EXCEPTION:This value can't be matched");
  176.                 }
  177.             }
  178.             return flag;
  179.         }
  180.         public static bool operator <(Triangle tr1, Triangle tr2)
  181.         {
  182.             bool flag = false;
  183.  
  184.             if (tr1.Square() > tr2.Square() && tr1.Square() > 0)
  185.             {
  186.                 flag = false;
  187.             }
  188.             else
  189.             {
  190.                 try
  191.                 {
  192.                     if (tr2.Square() > 0)
  193.                     {
  194.                         flag = true;
  195.                     }
  196.                     else
  197.                     {
  198.                         throw new SystemException("This value can't be matched");
  199.                     }
  200.                 }
  201.                 catch
  202.                 {
  203.                     Console.WriteLine("This value can't be matched");
  204.                 }
  205.             }
  206.             return flag;
  207.         }
  208.  
  209.         public static implicit operator double(Triangle tr)
  210.         {
  211.             return tr.Square();
  212.         }
  213.  
  214.         public static explicit operator bool(Triangle tr)
  215.         {
  216.             if (tr.Square() > 0)
  217.                 return true;
  218.             return false;
  219.         }
  220.        
  221.     }
  222.  
  223.    
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement