Advertisement
Guest User

Untitled

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