Advertisement
Guest User

Command Interpreter 95/100

a guest
Sep 4th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class CommandInterpreter
  5. {
  6.     static void Main()
  7.     {
  8.         var input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  9.         var commandInput = Console.ReadLine();
  10.         var start = 0;
  11.         var count = 0;
  12.  
  13.         while (commandInput != "end")
  14.         {
  15.             var commandSplit = commandInput.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  16.             var command = commandSplit[0];
  17.  
  18.             if (commandSplit.Length == 5) //Проверява кой тип команда е въведена, за да приравни променливите.
  19.             {
  20.                 start = int.Parse(commandSplit[2]);
  21.                 count = int.Parse(commandSplit[4]);
  22.  
  23.                 if (count > input.Length - start)
  24.                 {
  25.                     Console.WriteLine("Invalid input parameters.");
  26.                     goto COMMAND;
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 count = int.Parse(commandSplit[1]);
  32.                 if (count > input.Length) count %= input.Length; //Oстават само меродавни отмествания, а тези които превъртат масива(count кратно на input.Length) се пренебрегват.
  33.             }
  34.  
  35.             if ((start < 0) || (start > input.Length - 1) || count < 0)
  36.             {
  37.                 Console.WriteLine("Invalid input parameters.");
  38.                 goto COMMAND;
  39.             }
  40.  
  41.             if (command == "reverse")
  42.             {
  43.                 Array.Reverse(input, start, count);
  44.             }
  45.             else if (command == "sort")
  46.             {
  47.                 Array.Sort(input, start, count);
  48.             }
  49.             else if (command == "rollRight") //Изпълнява се чрез копиране на части от input в друг масив(temp).
  50.             {
  51.                 var temp = new string[input.Length];
  52.                 Array.Copy(input, 0, temp, count, temp.Length - count);
  53.                 Array.Copy(input, input.Length - count, temp, 0, count);
  54.                 input = temp;
  55.             }
  56.             else //rollLeft - Изпълнява се чрез копиране на части от input в друг масив(temp).
  57.             {
  58.                 var temp = new string[input.Length];
  59.                 Array.Copy(input, count, temp, 0, temp.Length - count);
  60.                 Array.Copy(input, 0, temp, temp.Length - count, count);
  61.                 input = temp;
  62.             }
  63.  
  64.             COMMAND: commandInput = Console.ReadLine(); //Тук слиза понякога и от 26-ред
  65.         }
  66.  
  67.         Console.WriteLine("[" + String.Join(", ", input) + "]");
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement