Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- class CommandInterpreter
- {
- static void Main()
- {
- var input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
- var commandInput = Console.ReadLine();
- var start = 0;
- var count = 0;
- while (commandInput != "end")
- {
- var commandSplit = commandInput.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
- var command = commandSplit[0];
- if (commandSplit.Length == 5) //Проверява кой тип команда е въведена, за да приравни променливите.
- {
- start = int.Parse(commandSplit[2]);
- count = int.Parse(commandSplit[4]);
- if (count > input.Length - start)
- {
- Console.WriteLine("Invalid input parameters.");
- goto COMMAND;
- }
- }
- else
- {
- count = int.Parse(commandSplit[1]);
- if (count > input.Length) count %= input.Length; //Oстават само меродавни отмествания, а тези които превъртат масива(count кратно на input.Length) се пренебрегват.
- }
- if ((start < 0) || (start > input.Length - 1) || count < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- goto COMMAND;
- }
- if (command == "reverse")
- {
- Array.Reverse(input, start, count);
- }
- else if (command == "sort")
- {
- Array.Sort(input, start, count);
- }
- else if (command == "rollRight") //Изпълнява се чрез копиране на части от input в друг масив(temp).
- {
- var temp = new string[input.Length];
- Array.Copy(input, 0, temp, count, temp.Length - count);
- Array.Copy(input, input.Length - count, temp, 0, count);
- input = temp;
- }
- else //rollLeft - Изпълнява се чрез копиране на части от input в друг масив(temp).
- {
- var temp = new string[input.Length];
- Array.Copy(input, count, temp, 0, temp.Length - count);
- Array.Copy(input, 0, temp, temp.Length - count, count);
- input = temp;
- }
- COMMAND: commandInput = Console.ReadLine(); //Тук слиза понякога и от 26-ред
- }
- Console.WriteLine("[" + String.Join(", ", input) + "]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement