GerganaTsirkova

Command interpreter

Mar 21st, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _9.CommandInterpreter
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> input = Console.ReadLine().Split(new char[] { ' '},StringSplitOptions.RemoveEmptyEntries).ToList();
  12.             string[] command = Console.ReadLine().Split(' ').ToArray();
  13.             while (command[0] != "end")
  14.             {
  15.                
  16.                 if (command[0]=="reverse")
  17.                 {
  18.                     int start = int.Parse(command[2]);
  19.                     int count = int.Parse(command[4]);
  20.                     if(IsValidCommand(input,start,count))
  21.                     {
  22.                         List<string> portion = new List<string>();
  23.                         for (int i = start; i < start + count; i++)
  24.                         {
  25.                             portion.Add(input[i]);
  26.                         }
  27.                         portion.Reverse();
  28.                         input.RemoveRange(start, count);
  29.                         input.InsertRange(start, portion);
  30.                     }
  31.                     else
  32.                     {
  33.                         Console.WriteLine("Invalid input parameters.");
  34.                     }
  35.                 }
  36.                 else if(command[0]=="sort")
  37.                 {
  38.                     int start = int.Parse(command[2]);
  39.                     int count = int.Parse(command[4]);
  40.                     if (IsValidCommand(input, start, count))
  41.                     {
  42.                         List<string> portion = new List<string>();
  43.                         for (int i = start; i < start + count; i++)
  44.                         {
  45.                             portion.Add(input[i]);
  46.                         }
  47.                         portion.Sort();
  48.                         input.RemoveRange(start, count);
  49.                         input.InsertRange(start, portion);
  50.                     }
  51.                     else
  52.                     {
  53.                         Console.WriteLine("Invalid input parameters.");
  54.                     }
  55.                 }
  56.                 else if(command[0] == "rollLeft")
  57.                 {
  58.                     int times = int.Parse(command[1]);
  59.                     if (times < 1)
  60.                     {
  61.                         Console.WriteLine("Invalid input parameters.");
  62.                     }
  63.                     else
  64.                     {
  65.                         for (int i = 0; i < times; i++)
  66.                         {
  67.                             List<string> inner = new List<string>();
  68.                             string start = input[0];
  69.                             for (int j = 1; j < input.Count; j++)
  70.                             {
  71.                                 inner.Add(input[j]);
  72.                             }
  73.                             inner.Add(start);
  74.                             input = inner;
  75.                         }
  76.                     }
  77.                 }
  78.                 else if(command[0]== "rollRight")
  79.                 {
  80.                     int times = int.Parse(command[1]);
  81.                     if(times<1)
  82.                     {
  83.                         Console.WriteLine("Invalid input parameters.");
  84.                     }
  85.                     else
  86.                     {
  87.                         for (int i = 0; i < times; i++)
  88.                         {
  89.                             List<string> inner = new List<string>();
  90.                             string end = input[input.Count - 1];
  91.                             inner.Insert(0, end);
  92.                             for (int j = 0; j < input.Count - 1; j++)
  93.                             {
  94.                                 inner.Add(input[j]);
  95.                             }
  96.                             input = inner;
  97.                         }
  98.                     }
  99.                 }
  100.                 else
  101.                 {
  102.                     Console.WriteLine("Invalid input parameters.");
  103.                 }
  104.                 command = Console.ReadLine().Split(' ').ToArray();
  105.             }
  106.             string separator = ", ";
  107.             Console.Write("[");
  108.             Console.Write(string.Join(separator,input));
  109.             Console.WriteLine("]");
  110.         }
  111.  
  112.         static bool IsValidCommand(List<string> input, int start, int count)
  113.         {
  114.             bool result = true;
  115.             if((start<0)||(start>=input.Count-1))
  116.             {
  117.                 result = false;
  118.             }
  119.             if((count<0)||(count+start>=input.Count-1))
  120.             {
  121.                 result = false;
  122.             }
  123.             return result;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment