Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class Ex08_SimpleTextEditor {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int countCommands = Integer.parseInt(scanner.nextLine());
- ArrayDeque<String> commands = new ArrayDeque<>();
- ArrayDeque<Character> text = new ArrayDeque<>();
- ArrayDeque<Character> erasedChars = new ArrayDeque<>();
- for (int i = 0; i < countCommands; i++) {
- String command = scanner.nextLine();
- if (command.charAt(0) != '3' && command.charAt(0) != '4') {
- commands.push(command);
- }
- String[] tokens = command.split("\\s+");
- int action = Integer.parseInt(tokens[0]);
- switch (action) {
- case 1:
- String element = tokens[1];
- for (int j = 0; j < element.length(); j++) {
- text.push(element.charAt(j));
- }
- break;
- case 2:
- int countCharsToErase = Integer.parseInt(tokens[1]);
- for (int k = 0; k < countCharsToErase; k++) {
- char curr = text.pop();
- erasedChars.offer(curr);
- }
- break;
- case 3:
- System.out.println(text.peek());
- break;
- case 4:
- String undoCommand = commands.pop();
- if (undoCommand.charAt(0) == '1') {
- element = undoCommand.substring(2);
- for (int j = 0; j < element.length(); j++) {
- text.pop();
- }
- } else if (undoCommand.charAt(0) == '2') {
- int reinstateCharsCount = Character.getNumericValue(undoCommand.charAt(2));
- if (erasedChars.size() >= reinstateCharsCount) {
- for (int l = 0; l < reinstateCharsCount; l++) {
- char toReinstate = erasedChars.poll();
- text.push(toReinstate);
- }
- }
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment