Stacklysm

Math Interval Test

Nov 30th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. enum IntervalType
  2.     {
  3.         Null = -1,
  4.         RLBounded = 0,
  5.         RBounded = 1,
  6.         LBounded = 2,
  7.         Boundless = 3
  8.     }
  9.  
  10.     class Interval
  11.     {
  12.         private string _originalText;
  13.  
  14.         public int LeftBound { get { return ParseInterval(0); } }
  15.         public int RightBound { get { return ParseInterval(1); } }
  16.         public IntervalType Type { get { return ParseType(); } }
  17.  
  18.         public Interval()
  19.         {
  20.             _originalText = "[0, 0]";
  21.         }
  22.  
  23.         public Interval(string intervalText)
  24.         {
  25.             _originalText = intervalText;
  26.         }
  27.  
  28.         public override string ToString()
  29.         {
  30.             switch((int)Type)
  31.             {
  32.                 case -1:
  33.                     throw new ArgumentException();
  34.  
  35.                 case 0:
  36.                     return $"[{LeftBound}, {RightBound}]";
  37.  
  38.                 case 1:
  39.                     return $"]{LeftBound}, {RightBound}]";
  40.  
  41.                 case 2:
  42.                     return $"[{LeftBound}, {RightBound}[";
  43.  
  44.                 case 3:
  45.                     return $"]{LeftBound}, {RightBound}[";
  46.  
  47.                 default:
  48.                     throw new ArgumentException();
  49.             }
  50.         }
  51.  
  52.         public void Print()
  53.         {
  54.             for(int i = LeftBound; i <= RightBound; i++)
  55.             {
  56.                 Console.WriteLine(i);
  57.             }
  58.         }
  59.  
  60.         private IntervalType ParseType()
  61.         {
  62.             if (_originalText[0] == '[' && _originalText[_originalText.Length - 1] == ']')
  63.             {
  64.                 return IntervalType.RLBounded;
  65.             }
  66.             else if (_originalText[0] == '[' || _originalText[_originalText.Length - 1] == ']')
  67.             {
  68.                 if (_originalText[0] == '[')
  69.                 {
  70.                     return IntervalType.RBounded;
  71.                 }
  72.                 else if (_originalText[_originalText.Length - 1] == ']')
  73.                 {
  74.                     return IntervalType.LBounded;
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 return IntervalType.Boundless;
  80.             }
  81.  
  82.             return IntervalType.Null;
  83.         }
  84.  
  85.         private int ParseInterval(int bound)
  86.         {
  87.             int n1 = _originalText[0] == '[' ? int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[0]) : int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[0]) + 1;
  88.             int n2 = _originalText[_originalText.Length - 1] == ']' ? int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[1]) : int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[1]) - 1;
  89.            
  90.             int min = (n1 != n2) ? ((n1 < n2) ? n1 : n2) : n1;
  91.             int max = (n1 != n2) ? ((n1 > n2) ? n1 : n2) : n1;
  92.  
  93.             return (bound == 0) ? min : max;
  94.         }
  95.  
  96.  
  97.         public static implicit operator Interval(string interval)
  98.         {
  99.             return new Interval(interval);
  100.         }
  101.     }
  102.  
  103.     class Program
  104.     {
  105.         static void Main(string[] args)
  106.         {
  107.             bool c = true;
  108.  
  109.             while(c)
  110.             {
  111.                 Console.Write("\nDigite um intervalo/Type an interval (Ex.: ]-2, 32]): ");
  112.                 Interval interval = Console.ReadLine();
  113.  
  114.                 interval.Print();
  115.                
  116.                 Console.Write("Você quer continuar?/Do you want to continue? (S/N): ");
  117.                 c = Console.ReadKey().Key == ConsoleKey.S;
  118.             }
  119.  
  120.             Console.ReadLine();
  121.         }
  122.     }
Advertisement
Add Comment
Please, Sign In to add comment