Advertisement
Guest User

Sequence of Commands

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