Advertisement
Guest User

Command Interpreter

a guest
Aug 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Exam_Preparation_III
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var input = Console.ReadLine();
  12.  
  13. List<string> inputArray = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
  14.  
  15. string inputCommand = Console.ReadLine();
  16.  
  17. while (inputCommand != "end")
  18. {
  19. List<string> command = inputCommand.Split(' ').ToList();
  20. switch (command[0])
  21. {
  22. case "reverse":
  23. int start = int.Parse(command[2]);
  24. int count = int.Parse(command[4]);
  25. if (start >= inputArray.Count || count > inputArray.Count - start || start < 0 || count < 0)
  26. {
  27. Console.WriteLine("Invalid input parameters.");
  28. }
  29. else
  30. {
  31. inputArray.Reverse(start, count);
  32. }
  33. break;
  34.  
  35. case "sort":
  36. start = int.Parse(command[2]);
  37. count = int.Parse(command[4]);
  38. if (start >= inputArray.Count || count > inputArray.Count - start || start < 0 || count < 0)
  39. {
  40. Console.WriteLine("Invalid input parameters.");
  41. }
  42. else
  43. {
  44. inputArray.Sort(start, count, StringComparer.InvariantCulture);
  45. }
  46. break;
  47.  
  48. case "rollLeft":
  49. count = int.Parse(command[1]);
  50. if (count >= 0)
  51. {
  52. for (int i = 0; i < count % inputArray.Count; i++)
  53. {
  54. string firstElement = inputArray[0];
  55.  
  56. for (int j = 0; j < inputArray.Count - 1; j++)
  57. {
  58. inputArray[j] = inputArray[j + 1];
  59. }
  60.  
  61. inputArray[inputArray.Count - 1] = firstElement;
  62. }
  63. }
  64. else
  65. {
  66. Console.WriteLine("Invalid input parameters.");
  67. }
  68. break;
  69.  
  70. case "rollRight":
  71. count = int.Parse(command[1]);
  72. if (count >= 0)
  73. {
  74. for (int i = 0; i < count % inputArray.Count; i++)
  75. {
  76. string lastElement = inputArray[inputArray.Count - 1];
  77.  
  78. for (int j = inputArray.Count - 1; j > 0; j--)
  79. {
  80. inputArray[j] = inputArray[j - 1];
  81. }
  82.  
  83. inputArray[0] = lastElement;
  84. }
  85. }
  86. else
  87. {
  88. Console.WriteLine("Invalid input parameters.");
  89. }
  90. break;
  91. }
  92.  
  93. inputCommand = Console.ReadLine();
  94. }
  95. Console.WriteLine("[" + (string.Join(", ", inputArray)) + "]");
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement