1+ #include < iostream>
2+ #include < stack>
3+ #include < string>
4+ #include < vector>
5+
6+ using namespace std ;
7+
8+ /* *
9+ * TextEditor class - Manages typing, undo, and redo operations
10+ */
11+ class TextEditor {
12+ private:
13+ vector<string> currentText; // Stores the current document text
14+ stack<string> undoStack; // Stack for undo operations
15+ stack<string> redoStack; // Stack for redo operations
16+
17+ public:
18+ /* *
19+ * Constructor - Initializes the text editor
20+ */
21+ TextEditor () {
22+ cout << " Text Editor initialized!\n " ;
23+ }
24+
25+ /* *
26+ * Type text - Adds new text to the document
27+ * @param text The text to add
28+ */
29+ void typeText (const string& text) {
30+ if (text.empty ()) {
31+ cout << " Error: Cannot add empty text!\n " ;
32+ return ;
33+ }
34+
35+ undoStack.push (text);
36+ currentText.push_back (text);
37+
38+ // Clear redo stack when new text is typed
39+ while (!redoStack.empty ()) {
40+ redoStack.pop ();
41+ }
42+
43+ cout << " ✓ Text added: \" " << text << " \"\n " ;
44+ }
45+
46+ /* *
47+ * Undo - Removes the last typed text
48+ */
49+ void undo () {
50+ if (undoStack.empty ()) {
51+ cout << " Nothing to undo!\n " ;
52+ return ;
53+ }
54+
55+ string lastText = undoStack.top ();
56+ undoStack.pop ();
57+
58+ redoStack.push (lastText);
59+ currentText.pop_back ();
60+
61+ cout << " ↩ Undone: \" " << lastText << " \"\n " ;
62+ }
63+
64+ /* *
65+ * Redo - Restores the most recently undone text
66+ */
67+ void redo () {
68+ if (redoStack.empty ()) {
69+ cout << " Nothing to redo!\n " ;
70+ return ;
71+ }
72+
73+ string textToRestore = redoStack.top ();
74+ redoStack.pop ();
75+
76+ undoStack.push (textToRestore);
77+ currentText.push_back (textToRestore);
78+
79+ cout << " ↪ Redone: \" " << textToRestore << " \"\n " ;
80+ }
81+
82+ /* *
83+ * Display the current document text
84+ */
85+ void showCurrentText () const {
86+ cout << " \n\n " ;
87+ cout << " 📜 CURRENT DOCUMENT:\n " ;
88+ cout << " \n " ;
89+
90+ if (currentText.empty ()) {
91+ cout << " [Empty Document]\n " ;
92+ } else {
93+ for (size_t i = 0 ; i < currentText.size (); i++) {
94+ cout << currentText[i];
95+ if (i < currentText.size () - 1 ) {
96+ cout << " " ;
97+ }
98+ }
99+ cout << " \n " ;
100+ }
101+
102+ cout << " ═══════════════════════════════════════\n\n " ;
103+ }
104+
105+ /* *
106+ * Display statistics about the editor state
107+ */
108+ void showStats () const {
109+ cout << " \n 📊 Editor Statistics:\n " ;
110+ cout << " • Words typed: " << currentText.size () << " \n " ;
111+ cout << " • Undo available: " << undoStack.size () << " \n " ;
112+ cout << " • Redo available: " << redoStack.size () << " \n\n " ;
113+ }
114+
115+ /* *
116+ * Destructor
117+ */
118+ ~TextEditor () {
119+ cout << " Text Editor closed. Goodbye!\n " ;
120+ }
121+ };
122+
123+ /* *
124+ * Display the menu
125+ */
126+ void displayMenu () {
127+ cout << " \n " ;
128+ cout << " // TYPING UNDO/REDO SYSTEM MENU //\n " ;
129+ cout << " \n " ;
130+ cout << " 1. ✍️ Type Text \n " ;
131+ cout << " 2. ↩️ Undo Typing \n " ;
132+ cout << " 3. ↪️ Redo Typing \n " ;
133+ cout << " 4. 📜 Show Current Text \n " ;
134+ cout << " 5. 📊 Show Statistics \n " ;
135+ cout << " 6. 🚪 Exit \n " ;
136+ cout << " \n " ;
137+ cout << " Enter your choice: " ;
138+ }
139+
140+ /* *
141+ * Main function - Entry point of the program
142+ */
143+ int main () {
144+ TextEditor editor;
145+ int choice;
146+ string text;
147+
148+ cout << " \n\n " ;
149+ cout << " // Welcome to Typing Undo/Redo System! //\n " ;
150+ cout << " \n " ;
151+
152+ do {
153+ displayMenu ();
154+ cin >> choice;
155+ cin.ignore (); // Clear the newline character from buffer
156+
157+ switch (choice) {
158+ case 1 :
159+ cout << " Enter text to type: " ;
160+ getline (cin, text);
161+ editor.typeText (text);
162+ break ;
163+
164+ case 2 :
165+ editor.undo ();
166+ break ;
167+
168+ case 3 :
169+ editor.redo ();
170+ break ;
171+
172+ case 4 :
173+ editor.showCurrentText ();
174+ break ;
175+
176+ case 5 :
177+ editor.showStats ();
178+ break ;
179+
180+ case 6 :
181+ cout << " \n 🚪 Exiting program...\n " ;
182+ break ;
183+
184+ default :
185+ cout << " ❌ Invalid choice! Please select 1-6.\n " ;
186+ }
187+
188+ } while (choice != 6 );
189+
190+ return 0 ;
191+ }
0 commit comments