Advertisement
Aborigenius

CommandInterpreter

Sep 3rd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Task2
  8. {
  9.     class CommandInterpreter
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] array = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  14.             List<string> list = array.ToList();
  15.             string input = Console.ReadLine();
  16.  
  17.             while (input != "end")
  18.             {
  19.                 string[] inputTokens = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 if (inputTokens[0] == "reverse")
  22.                 {
  23.                     int from = int.Parse(inputTokens[2]);
  24.                     int count = int.Parse(inputTokens[4]);
  25.  
  26.                     if (from < 0 || from >= array.Length || (from + count) > array.Length || count < 0)
  27.                     {
  28.                         Console.WriteLine("Invalid input parameters.");
  29.                         goto Enter;
  30.                     }
  31.                     else
  32.                     {
  33.                         list.Reverse(from, count);
  34.                         array = list.ToArray();
  35.                     }
  36.  
  37.                 }
  38.                 if (inputTokens[0] == "sort")
  39.                 {
  40.                     int from = int.Parse(inputTokens[2]);
  41.                     int count = int.Parse(inputTokens[4]);
  42.                     if (from < 0 || from >= array.Length || (from + count) > array.Length || count < 0)
  43.                     {
  44.                         Console.WriteLine("Invalid input parameters.");
  45.                         goto Enter;
  46.                     }
  47.                     else
  48.                     {
  49.                         Array.Sort(array, from * 1, count * 1);
  50.                         list = array.ToList();
  51.                     }
  52.  
  53.                 }
  54.                 if (inputTokens[0] == "rollLeft")
  55.                 {
  56.                     int cnt = int.Parse(inputTokens[1]);
  57.                     if (cnt > 0)
  58.                     {
  59.                         list = list.Skip(cnt).Concat(list.Take(cnt)).ToList();
  60.                         array = list.ToArray();
  61.                     }
  62.                     else
  63.                     {
  64.                         Console.WriteLine("Invalid input parameters.");
  65.                         goto Enter;
  66.                     }
  67.  
  68.                 }
  69.                 if (inputTokens[0] == "rollRight")
  70.                 {
  71.                     int cnt = int.Parse(inputTokens[1]);
  72.                     if (cnt >= 0)
  73.                     {
  74.                         for (int i = 0; i < cnt % list.Count; i++)
  75.                         {
  76.                             string lastElement = list[list.Count - 1];
  77.  
  78.                             for (int j = list.Count - 1; j > 0; j--)
  79.                             {
  80.                                 list[j] = list[j - 1];
  81.                             }
  82.  
  83.                             list[0] = lastElement;
  84.                         }
  85.                     }
  86.                     else
  87.                     {
  88.                         Console.WriteLine("Invalid input parameters.");
  89.                         goto Enter;
  90.                     }
  91.  
  92.  
  93.                 }
  94.                 Enter:
  95.                 input = Console.ReadLine();
  96.             }
  97.  
  98.             Console.Write($"[{string.Join(", ", list)}]");
  99.  
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement