Advertisement
braveheart1989

Sequence of Commands

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