IvaAnd

Satcks & Queues _8. Simple Text Editor

Sep 23rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class Ex08_SimpleTextEditor {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. int countCommands = Integer.parseInt(scanner.nextLine());
  8.  
  9. ArrayDeque<String> commands = new ArrayDeque<>();
  10. ArrayDeque<Character> text = new ArrayDeque<>();
  11. ArrayDeque<Character> erasedChars = new ArrayDeque<>();
  12.  
  13. for (int i = 0; i < countCommands; i++) {
  14. String command = scanner.nextLine();
  15. if (command.charAt(0) != '3' && command.charAt(0) != '4') {
  16. commands.push(command);
  17. }
  18. String[] tokens = command.split("\\s+");
  19. int action = Integer.parseInt(tokens[0]);
  20.  
  21.  
  22. switch (action) {
  23. case 1:
  24. String element = tokens[1];
  25. for (int j = 0; j < element.length(); j++) {
  26. text.push(element.charAt(j));
  27. }
  28. break;
  29. case 2:
  30. int countCharsToErase = Integer.parseInt(tokens[1]);
  31. for (int k = 0; k < countCharsToErase; k++) {
  32. char curr = text.pop();
  33. erasedChars.offer(curr);
  34. }
  35. break;
  36. case 3:
  37. System.out.println(text.peek());
  38. break;
  39. case 4:
  40. String undoCommand = commands.pop();
  41. if (undoCommand.charAt(0) == '1') {
  42. element = undoCommand.substring(2);
  43. for (int j = 0; j < element.length(); j++) {
  44. text.pop();
  45. }
  46.  
  47. } else if (undoCommand.charAt(0) == '2') {
  48. int reinstateCharsCount = Character.getNumericValue(undoCommand.charAt(2));
  49. if (erasedChars.size() >= reinstateCharsCount) {
  50. for (int l = 0; l < reinstateCharsCount; l++) {
  51. char toReinstate = erasedChars.poll();
  52. text.push(toReinstate);
  53. }
  54. }
  55.  
  56. }
  57. break;
  58. }
  59.  
  60. }
  61.  
  62. }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment