Advertisement
fbinnzhivko

03. Array Test

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