Advertisement
Guest User

SequenceOfCommands

a guest
Oct 3rd, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. public class SequenceOfCommands
  8. {
  9.     public static void Main()
  10.     {
  11.         int sizeOfArray = int.Parse(Console.ReadLine());
  12.  
  13.         long[] array = Console.ReadLine()
  14.             .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  15.             .Select(long.Parse)
  16.             .ToArray();
  17.  
  18.         string command = Console.ReadLine();
  19.  
  20.         while (!command.Equals("stop"))
  21.         {
  22.             string[] args = command
  23.                 .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  24.                 .ToArray();
  25.            
  26.             PerformAction(args, array);
  27.  
  28.             Console.WriteLine(string.Join(" ", array));
  29.  
  30.             command = Console.ReadLine();
  31.         }
  32.     }
  33.  
  34.     static void PerformAction(string[] args, long[] array)
  35.     {
  36.         string action = args[0];
  37.  
  38.         if (args.Length > 1)
  39.         {
  40.             int index = int.Parse(args[1]) - 1;
  41.             int value = int.Parse(args[2]);
  42.             switch (action)
  43.             {
  44.                 case "add":
  45.                     array[index] += value;
  46.                     break;
  47.                 case "subtract":
  48.                     array[index] -= value;
  49.                     break;
  50.                 case "multiply":
  51.                     array[index] *= value;
  52.                     break;
  53.             }
  54.         }
  55.         else if (args[0] == "lshift")
  56.         {
  57.             ArrayShiftLeft(array);
  58.         }
  59.         else if (args[0] == "rshift")
  60.         {
  61.             ArrayShiftRight(array);
  62.         }
  63.     }
  64.  
  65.     private static void ArrayShiftRight(long[] array)
  66.     {
  67.         long lastNumber = array[array.Length - 1];
  68.         for (int i = array.Length - 1; i >= 1; i--)
  69.         {
  70.             array[i] = array[i - 1];
  71.         }
  72.         array[0] = lastNumber;
  73.     }
  74.  
  75.     private static void ArrayShiftLeft(long[] array)
  76.     {
  77.         long firstNumber = array[0];
  78.         for (int i = 0; i < array.Length - 1; i++)
  79.         {
  80.             array[i] = array[i + 1];
  81.         }
  82.         array[array.Length - 1] = firstNumber;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement