Advertisement
martinvalchev

Sequence of Commands

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