Guest User

Untitled

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