MeGaDeTH_91

Untitled

Jul 8th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 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 _02.Command_Interpreter
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<string> stringArr = Console.ReadLine()
  14. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  15. .ToList();
  16. string commands = Console.ReadLine();
  17. List<string> resultList = new List<string>();
  18. while (commands != "end")
  19. {
  20. string[] commandArr = commands.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  21. .ToArray();
  22. string currCommand = commandArr[0].ToLower();
  23. if (currCommand == "reverse")
  24. {
  25. int stIndex = int.Parse(commandArr[2]);
  26. int count = int.Parse(commandArr[4]);
  27. List<string> temp = new List<string>();
  28. if (count >= 0 && stIndex >= 0 && stIndex + count <= stringArr.Count)
  29. {
  30. for (int i = stIndex; i < stIndex + count; i++)
  31. {
  32. temp.Add(stringArr[i]);
  33. }
  34. temp.Reverse();
  35. for (int i = 0; i < count; i++)
  36. {
  37. stringArr.RemoveAt(stIndex);
  38. }
  39. stringArr.InsertRange(stIndex, temp);
  40. }
  41. else
  42. {
  43.  
  44. Console.WriteLine("Invalid input parameters.");
  45. }
  46. }
  47. else if (currCommand == "sort")
  48. {
  49. int stIndex = int.Parse(commandArr[2]);
  50. int count = int.Parse(commandArr[4]);
  51. List<string> temp = new List<string>();
  52. if (count >= 0 && stIndex >= 0 && stIndex + count <= stringArr.Count)
  53. {
  54. for (int i = stIndex; i < stIndex + count; i++)
  55. {
  56. temp.Add(stringArr[i]);
  57. }
  58. temp.Sort();
  59. for (int i = 0; i < count; i++)
  60. {
  61. stringArr.RemoveAt(stIndex);
  62. }
  63. stringArr.InsertRange(stIndex, temp);
  64. }
  65. else
  66. {
  67.  
  68. Console.WriteLine("Invalid input parameters.");
  69. }
  70. }
  71. else if (currCommand == "rollleft")
  72. {
  73. int count = int.Parse(commandArr[1]) % stringArr.Count;
  74. if (count >= 0)
  75. {
  76. List<string> temp = new List<string>();
  77. for (int i = 0; i < count; i++)
  78. {
  79. string firstEl = stringArr[0];
  80. for (int j = 0; j < stringArr.Count; j++)
  81. {
  82. if (j + 1 < stringArr.Count)
  83. {
  84.  
  85. stringArr[j] = stringArr[j + 1];
  86. }
  87. else
  88. {
  89. stringArr[j] = firstEl;
  90. }
  91. }
  92. }
  93. }
  94. else
  95. {
  96.  
  97. Console.WriteLine("Invalid input parameters.");
  98. }
  99. }
  100. else if (currCommand == "rollright")
  101. {
  102. int count = int.Parse(commandArr[1]) % stringArr.Count;
  103. if (count >= 0)
  104. {
  105.  
  106. List<string> temp = new List<string>();
  107. for (int i = 0; i < count; i++)
  108. {
  109. string lastEl = stringArr[stringArr.Count - 1];
  110. for (int j = stringArr.Count - 1; j >= 0; j--)
  111. {
  112. if (j - 1 >= 0)
  113. {
  114. stringArr[j] = stringArr[j - 1];
  115. }
  116. else
  117. {
  118. stringArr[j] = lastEl;
  119. }
  120. }
  121. }
  122. }
  123. else
  124. {
  125. Console.WriteLine("Invalid input parameters.");
  126. }
  127. }
  128. commands = Console.ReadLine();
  129. }
  130. Console.Write("[");
  131. for (int i = 0; i < stringArr.Count; i++)
  132. {
  133. if (i == stringArr.Count - 1)
  134. {
  135. Console.Write($"{stringArr[i]}");
  136. }
  137. else
  138. {
  139. Console.Write($"{stringArr[i]}, ");
  140. }
  141. }
  142. Console.WriteLine("]");
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment