Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum IntervalType
- {
- Null = -1,
- RLBounded = 0,
- RBounded = 1,
- LBounded = 2,
- Boundless = 3
- }
- class Interval
- {
- private string _originalText;
- public int LeftBound { get { return ParseInterval(0); } }
- public int RightBound { get { return ParseInterval(1); } }
- public IntervalType Type { get { return ParseType(); } }
- public Interval()
- {
- _originalText = "[0, 0]";
- }
- public Interval(string intervalText)
- {
- _originalText = intervalText;
- }
- public override string ToString()
- {
- switch((int)Type)
- {
- case -1:
- throw new ArgumentException();
- case 0:
- return $"[{LeftBound}, {RightBound}]";
- case 1:
- return $"]{LeftBound}, {RightBound}]";
- case 2:
- return $"[{LeftBound}, {RightBound}[";
- case 3:
- return $"]{LeftBound}, {RightBound}[";
- default:
- throw new ArgumentException();
- }
- }
- public void Print()
- {
- for(int i = LeftBound; i <= RightBound; i++)
- {
- Console.WriteLine(i);
- }
- }
- private IntervalType ParseType()
- {
- if (_originalText[0] == '[' && _originalText[_originalText.Length - 1] == ']')
- {
- return IntervalType.RLBounded;
- }
- else if (_originalText[0] == '[' || _originalText[_originalText.Length - 1] == ']')
- {
- if (_originalText[0] == '[')
- {
- return IntervalType.RBounded;
- }
- else if (_originalText[_originalText.Length - 1] == ']')
- {
- return IntervalType.LBounded;
- }
- }
- else
- {
- return IntervalType.Boundless;
- }
- return IntervalType.Null;
- }
- private int ParseInterval(int bound)
- {
- int n1 = _originalText[0] == '[' ? int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[0]) : int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[0]) + 1;
- int n2 = _originalText[_originalText.Length - 1] == ']' ? int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[1]) : int.Parse(_originalText.Trim('[', ']', ' ').Split(',')[1]) - 1;
- int min = (n1 != n2) ? ((n1 < n2) ? n1 : n2) : n1;
- int max = (n1 != n2) ? ((n1 > n2) ? n1 : n2) : n1;
- return (bound == 0) ? min : max;
- }
- public static implicit operator Interval(string interval)
- {
- return new Interval(interval);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- bool c = true;
- while(c)
- {
- Console.Write("\nDigite um intervalo/Type an interval (Ex.: ]-2, 32]): ");
- Interval interval = Console.ReadLine();
- interval.Print();
- Console.Write("Você quer continuar?/Do you want to continue? (S/N): ");
- c = Console.ReadKey().Key == ConsoleKey.S;
- }
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment