Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _9._Simple_Text_Editor_REAL_EXE
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int countOfOperations = int.Parse(Console.ReadLine());
  13. var stack = new Stack<string>();
  14. StringBuilder stringBuilder = new StringBuilder();
  15. string finalText = string.Empty;
  16. for (int i = 0; i < countOfOperations; i++)
  17. {
  18. var command = Console.ReadLine();
  19. var commandInList = command.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray();
  20. if (commandInList[0] == "1")
  21. {
  22. stack.Push(command);
  23. stringBuilder.Append(commandInList[1]);
  24. finalText += stringBuilder.ToString();
  25. stringBuilder = new StringBuilder();
  26. }
  27.  
  28. else if (commandInList[0] == "2")
  29. {
  30. int count = int.Parse(commandInList[1]);
  31. int startIndex = (finalText.Length) - count;
  32. if (startIndex >= 0)
  33. {
  34. string erasedText = finalText.Substring(startIndex, count);
  35. finalText = finalText.Remove(startIndex, count);
  36. stack.Push(erasedText);
  37. }
  38.  
  39. }
  40.  
  41. else if (commandInList[0] == "3")
  42. {
  43. int position = int.Parse(commandInList[1]) - 1;
  44. if (position < finalText.Length && position >= 0)
  45. {
  46. Console.WriteLine(finalText[position]);
  47. }
  48. }
  49.  
  50. else if (commandInList[0] == "4")
  51. {
  52. if (stack.Any())
  53. {
  54. string com = stack.Pop();
  55. if (com.StartsWith('1'))
  56. {
  57. com = com.Remove(0, 2);
  58. int index = finalText.IndexOf(com);
  59. finalText = finalText.Remove(index, com.Length);
  60. }
  61.  
  62. else
  63. {
  64. finalText += com;
  65. }
  66. }
  67.  
  68. }
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement