tanya_zheleva

Commands

Jan 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ExamPreparation
  5. {
  6.     class Startup
  7.     {
  8.         private const char ArgumentsDelimiter = ' ';
  9.  
  10.         static void Main()
  11.         {
  12.             int sizeOfArray = int.Parse(Console.ReadLine());
  13.  
  14.             long[] array = Console.ReadLine()
  15.                 .Split(ArgumentsDelimiter)
  16.                 .Select(long.Parse)
  17.                 .ToArray();
  18.  
  19.             string command = Console.ReadLine();
  20.  
  21.             while (!command.Equals("stop"))
  22.             {
  23.                 string[] tokens = command.Split(ArgumentsDelimiter);
  24.  
  25.                 int index = 0, value = 0;
  26.                 if (tokens.Length > 1)
  27.                 {
  28.                     index = int.Parse(tokens[1]);
  29.                     value = int.Parse(tokens[2]);
  30.                     index -= 1;
  31.                 }
  32.  
  33.                 switch (tokens[0])
  34.                 {
  35.                     case "multiply":
  36.                         array[index] *= value;
  37.                         break;
  38.                     case "add":
  39.                         array[index] += value;
  40.                         break;
  41.                     case "subtract":
  42.                         array[index] -= value;
  43.                         break;
  44.                     case "lshift":
  45.                         ShiftLeft(ref array);
  46.                         break;
  47.                     case "rshift":
  48.                         ShiftRight(ref array);
  49.                         break;
  50.                 }
  51.  
  52.                 Print(array);
  53.                 command = Console.ReadLine();
  54.             }
  55.         }
  56.  
  57.         private static void ShiftRight(ref long[] array)
  58.         {
  59.             var firstHalf = array.Reverse().Take(1);
  60.             var secondHalf = array.Reverse().Skip(1).Reverse();
  61.  
  62.             array = firstHalf.Concat(secondHalf).ToArray();
  63.         }
  64.  
  65.         private static void ShiftLeft(ref long[] array)
  66.         {
  67.             var firsHalf = array.Skip(1);
  68.             var secondHalf = array.Take(1);
  69.  
  70.             array = firsHalf.Concat(secondHalf).ToArray();
  71.         }
  72.  
  73.         private static void Print(long[] array)
  74.         {
  75.             for (int i = 0; i < array.Length; i++)
  76.             {
  77.                 Console.Write(array[i] + " ");
  78.             }
  79.  
  80.             Console.WriteLine();
  81.         }
  82.     }
  83. }
Add Comment
Please, Sign In to add comment