Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace LinkedList
  6. {
  7.     static class EntryPoint
  8.     {
  9.         static readonly Regex ValidRegex = new Regex("^(0|-?[1-9][0-9]*|[A-Za-z][0-9A-Z_a-z]*)$", RegexOptions.Compiled);
  10.  
  11.         static bool IsValidString(string str) => ValidRegex.IsMatch(str);
  12.  
  13.         static bool InsertBefore(string val, Item<string> oth)
  14.         {
  15.             if (BigInteger.TryParse(val, out var x) && BigInteger.TryParse(oth.value, out var y))
  16.             {
  17.                 return x <= y;
  18.             }
  19.             else
  20.             {
  21.                 return val.CompareTo(oth.value) < 1;
  22.             }
  23.         }
  24.  
  25.         static bool ValueEquals(Item<string> item, string val) => item.value == val;
  26.  
  27.         static void Main()
  28.         {
  29.             Item<string> start = null;
  30.  
  31.             var begin = true;
  32.             string input;
  33.  
  34.             while (true)
  35.             {
  36.                 if (!begin)
  37.                 {
  38.                     Console.WriteLine();
  39.                 }
  40.                 else
  41.                 {
  42.                     begin = false;
  43.                 }
  44.  
  45.                 Console.WriteLine("Awaiting input...");
  46.                 input = Console.ReadLine();
  47.  
  48.                 if (input.Length == 0)
  49.                 {
  50.                     Helpers.RemoveAll(ref start);
  51.                     Console.WriteLine("\nProgram terminated!");
  52.                     return;
  53.                 }
  54.                 else if (input.StartsWith('~'))
  55.                 {
  56.                     if (input.Length == 1)
  57.                     {
  58.                         Console.WriteLine("\nDeleting list...");
  59.                         Helpers.RemoveAll(ref start);
  60.                     }
  61.                     else
  62.                     {
  63.                         input = input.Substring(1);
  64.                         if (IsValidString(input))
  65.                         {
  66.                             Console.WriteLine("\nRemoving item...");
  67.                             Helpers.RemoveItem(ref start, input, ValueEquals);
  68.                         }
  69.                         else
  70.                         {
  71.                             Console.WriteLine("\nCould not parse input!");
  72.                         }
  73.                     }
  74.                 }
  75.                 else if (input.Equals("l"))
  76.                 {
  77.                     Console.WriteLine("\nList print...");
  78.                     Helpers.PrintList(start);
  79.                 }
  80.                 else if (input.Equals("i"))
  81.                 {
  82.                     Console.WriteLine("\nIterator print...");
  83.                     Helpers.PrintIterator(start);
  84.                 }
  85.                 else if (input.Equals("a"))
  86.                 {
  87.                     Console.WriteLine("\nArray print...");
  88.                     Helpers.PrintArray(start);
  89.                 }
  90.                 else if (input.Equals("r"))
  91.                 {
  92.                     Console.WriteLine("\nRecursive print...");
  93.                     Helpers.PrintRecursive(start);
  94.                 }
  95.                 else if (IsValidString(input))
  96.                 {
  97.                     Console.WriteLine("\nInserting item...");
  98.                     Helpers.InsertItem(ref start, input, InsertBefore);
  99.                 }
  100.                 else
  101.                 {
  102.                     Console.WriteLine("\nCould not parse input!");
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement