Danny_Berova

03.CommandInterpreter

Aug 14th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1.  
  2. namespace _01.Some_Exam
  3. {
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             string inputLine = Console.ReadLine();
  13.             List<string> input = inputLine.Split().ToList();
  14.  
  15.             string commands = Console.ReadLine();
  16.  
  17.             while (commands != "end")
  18.             {
  19.                 var tokens = commands.Split().ToArray();
  20.  
  21.                 switch (tokens[0])
  22.                 {
  23.                     case "reverse":
  24.                         int startReverse = int.Parse(tokens[2]);
  25.                         int countReverse = int.Parse(tokens[4]);
  26.  
  27.                         if (startReverse >= 0 &&
  28.                              startReverse < input.Count &&
  29.                              countReverse >= 0 &&
  30.                              startReverse + countReverse <= input.Count)
  31.                         {
  32.                             input.Reverse(startReverse, countReverse);
  33.                         }
  34.                         else
  35.                         {
  36.                             Console.WriteLine("Invalid input parameters.");
  37.                         }
  38.                         break;
  39.  
  40.                     case "sort":
  41.                         int startSort = int.Parse(tokens[2]);
  42.                         int countSort = int.Parse(tokens[4]);
  43.  
  44.                         if (startSort >= 0 &&
  45.                             startSort < input.Count &&
  46.                             countSort >= 0 &&
  47.                             startSort + countSort <= input.Count)
  48.                         {
  49.                             input.Sort(startSort, countSort, StringComparer.InvariantCulture);
  50.                         }
  51.                         else
  52.                         {
  53.                             Console.WriteLine("Invalid input parameters.");
  54.                         }
  55.                         break;
  56.  
  57.                     case "rollLeft":
  58.                         int countRollLeft = int.Parse(tokens[1]);
  59.  
  60.                         if (countRollLeft >= 0)
  61.                         {
  62.                             for (int i = 0; i < countRollLeft % input.Count; i++)
  63.                             {
  64.                                 string firstElement = input[0];
  65.  
  66.                                 for (int j = 0; j < input.Count - 1; j++)
  67.                                 {
  68.                                     input[j] = input[j + 1];
  69.                                 }
  70.  
  71.                                 input[input.Count - 1] = firstElement;
  72.                             }
  73.                         }
  74.                         else
  75.                         {
  76.                             Console.WriteLine("Invalid input parameters.");
  77.                         }
  78.  
  79.                         break;
  80.  
  81.                     case "rollRight":
  82.                         int countRollRight = int.Parse(tokens[1]);
  83.  
  84.                         if (countRollRight >= 0)
  85.                         {
  86.                             for (int i = 0; i < countRollRight % input.Count; i++)
  87.                             {
  88.                                 string lastElement = input[input.Count - 1];
  89.  
  90.                                 for (int j = input.Count - 1; j > 0; j--)
  91.                                 {
  92.                                     input[j] = input[j - 1];
  93.                                 }
  94.  
  95.                                 input[0] = lastElement;
  96.                             }
  97.                         }
  98.                         else
  99.                         {
  100.                             Console.WriteLine("Invalid input parameters.");
  101.                         }
  102.                         break;
  103.  
  104.                 }
  105.                 commands = Console.ReadLine();
  106.             }
  107.  
  108.             Console.WriteLine("[" + (string.Join(", ", input)) + "]");
  109.  
  110.         }
  111.     }
  112. }
Add Comment
Please, Sign In to add comment