fbinnzhivko

17. Sequence of Commands

Dec 25th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1.     using System;
  2.     using System.Linq;
  3.  
  4.     public class ArraysMain
  5.     {
  6.         private const char ArgumentsDelimiter = ' ';
  7.  
  8.         public static void Main()
  9.         {
  10.             int sizeOfArray = int.Parse(Console.ReadLine());
  11.  
  12.             long[] array = Console.ReadLine().Split(ArgumentsDelimiter).Select(long.Parse).ToArray();
  13.  
  14.             string[] command = Console.ReadLine().Split(' ').ToArray();
  15.  
  16.             while (command[0] != "stop")
  17.             {
  18.  
  19.                 int[] args = new int[2];
  20.  
  21.                 if (command[0] == "add" ||
  22.                     command[0] == "subtract" ||
  23.                     command[0] == "multiply")
  24.                 {
  25.  
  26.                     args[0] = int.Parse(command[1]);
  27.                     args[1] = int.Parse(command[2]);
  28.                     PerformAction(array, command[0], args);
  29.                 }
  30.                 else
  31.                 {
  32.                     PerformAction(array, command[0], args);
  33.                 }
  34.                 PrintArray(array);
  35.  
  36.                 Console.WriteLine();
  37.  
  38.                 command = Console.ReadLine().Split(' ').ToArray();
  39.             }
  40.         }
  41.  
  42.         static void PerformAction(long[] array, string action, int[] args)
  43.         {
  44.  
  45.             int pos = args[0];
  46.             int value = args[1];
  47.  
  48.             switch (action)
  49.             {
  50.                 case "multiply":
  51.                     array[pos - 1] *= value;
  52.                     break;
  53.                 case "add":
  54.                     array[pos - 1] += value;
  55.                     break;
  56.                 case "subtract":
  57.                     array[pos - 1] -= value;
  58.                     break;
  59.                 case "lshift":
  60.                     ArrayShiftLeft(array);
  61.                     break;
  62.                 case "rshift":
  63.                     ArrayShiftRight(array);
  64.                     break;
  65.  
  66.             }
  67.         }
  68.  
  69.         private static void ArrayShiftRight(long[] array)
  70.         {
  71.             long num = array[array.Length - 1];
  72.             for (int i = array.Length - 1; i >= 1; i--)
  73.             {
  74.                 array[i] = array[i - 1];
  75.             }
  76.             array[0] = num;
  77.         }
  78.  
  79.         private static void ArrayShiftLeft(long[] array)
  80.         {
  81.             long num = array[0];
  82.             for (int i = 0; i < array.Length - 1; i++)
  83.             {
  84.                 array[i] = array[i + 1];
  85.             }
  86.             array[array.Length - 1] = num;
  87.         }
  88.  
  89.         private static void PrintArray(long[] array)
  90.         {
  91.             for (int i = 0; i < array.Length; i++)
  92.             {
  93.                 Console.Write(array[i] + " ");
  94.             }
  95.  
  96.         }
  97.     }
Add Comment
Please, Sign In to add comment