Advertisement
GeorgiGG

Untitled

Jan 18th, 2022
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _09.Simple_text_editor
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.            
  15.             Stack<char> stack = new Stack<char>();
  16.             Stack<int> undo = new Stack<int>();
  17.             Stack<char> undoAction = new Stack<char>();
  18.  
  19.             string action = "";
  20.             for (int i = 0; i < n; i++)
  21.             {
  22.                 string[] input = Console.ReadLine().Split(' ').ToArray();
  23.                 string command = input[1];
  24.                 if (input.Length > 1)
  25.                     action = input[2];
  26.  
  27.                 if (command == "1")
  28.                 {
  29.                     foreach (var item in action)
  30.                     {
  31.                         stack.Push(item);
  32.                         undo.Push(int.Parse(command));
  33.                         undoAction.Push(item);
  34.                     }
  35.                 }
  36.                 else if (command == "2")          
  37.                     for (int j = 0; j < int.Parse(action); j++)
  38.                         {
  39.                             undoAction.Push(stack.pop);
  40.                             undo.Push(int.Parse(command));
  41.                         }  
  42.                 else if (command == "3")
  43.                     Console.WriteLine(stack.ElementAt(int.Parse(action)));
  44.                 else if (command == "4")
  45.                 {
  46.                     if (undo.Pop() == 2)
  47.                         stack.Push(undoAction.Pop());
  48.                     else if (undo.Pop() == 1)
  49.                         stack.Pop();
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement