Advertisement
Guest User

Sequence of Commands

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