desislava777

Command Interpreter

Aug 14th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 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 ConsoleApplication22
  8. {
  9. class Program
  10. {
  11. static string[] input;
  12. static void Main(string[] args)
  13. {
  14. input = Console.ReadLine().Split(' ').ToArray();
  15. string command = Console.ReadLine();
  16. while (command != "end")
  17. {
  18. string[] tokens = command.Split(' ');
  19. switch (tokens[0])
  20. {
  21. case "reverse":
  22. int start = int.Parse(tokens[2]);
  23. int count = int.Parse(tokens[4]);
  24. Reverse(start, count);
  25. break;
  26. case "sort":
  27. int starts = int.Parse(tokens[2]);
  28. int counts = int.Parse(tokens[4]);
  29. Sort(starts, counts);
  30. break;
  31. case "rollLeft":
  32. int numOfRotationLeft = int.Parse(tokens[1]);
  33. RollLeft(numOfRotationLeft);
  34. break;
  35. case "rollRight":
  36. int numOfRotationRight = int.Parse(tokens[1]);
  37. RollRight(numOfRotationRight);
  38. break;
  39. }
  40. command = Console.ReadLine();
  41. }
  42. }
  43. private static void RollLeft(int numOfRotationLeft)
  44. {
  45. for (int i = 0; i < numOfRotationLeft % input.Length; i++)
  46. {
  47. var temp = input[0];
  48. for (int j = 1; j < input.Length; j++)
  49. {
  50. input[j - 1] = input[j];
  51. }
  52. input[input.Length - 1] = temp;
  53. }
  54. Console.WriteLine("[" + string.Join(", ", input) + "]");
  55. }
  56. private static void RollRight(int numOfRotationRight)
  57. {
  58. for (int i = 0; i < numOfRotationRight; i++)
  59. {
  60. var temp = input[input.Length - 1];
  61. for (int j = input.Length - 1; j >= 1; j--)
  62. {
  63. input[j] = input[j - 1];
  64. }
  65. input[0] = temp;
  66. }
  67. Console.WriteLine("[" + string.Join(", ", input) + "]");
  68. }
  69. private static void Sort(int start, int count)
  70. {
  71. if (IsValid(start, count))
  72. {
  73. string[] str = input.Skip(start).Take(count).ToArray();
  74. Array.Sort(str);
  75. var num = new List<string>();
  76. for (int i = 0; i < input.Length - (input.Length - start); i++)
  77. num.Add(input[i]);
  78. num.AddRange(str);
  79. for (int i = start + count; i < input.Length; i++)
  80. num.Add(input[i]);
  81. Console.WriteLine("[" + string.Join(", ", num) + "]");
  82. }
  83. }
  84. private static void Reverse(int start, int count)
  85. {
  86. if (IsValid(start, count))
  87. {
  88. string[] str = input.Skip(start).Take(count).ToArray();
  89. Array.Reverse(str);
  90. var num = new List<string>();
  91. for (int i = 0; i < input.Length - (input.Length - start); i++)
  92. num.Add(input[i]);
  93. num.AddRange(str);
  94. for (int i = start + count; i < input.Length; i++)
  95. num.Add(input[i]);
  96. Console.WriteLine("[" + string.Join(", ", num) + "]");
  97. }
  98. }
  99. private static bool IsValid(int start, int count)
  100. {
  101. bool isInRange = start < input.Length && start >= 0;
  102. bool isValidCount = (start + count) <= input.Length && count >= 0;
  103. if (isInRange && isValidCount)
  104. {
  105. return true;
  106. }
  107. else
  108. {
  109. Console.WriteLine("Invalid input parameters.");
  110. return false;
  111. }
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment