-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
371 lines (320 loc) · 9.73 KB
/
Copy pathmain.cpp
File metadata and controls
371 lines (320 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//Simple text Editor
#include <iostream>
using namespace std;
//class node
struct node
{
string item;
node *next;
};
//Build ADT stack using linkedlist
class stack_linkedlist
{
private:
node* top;
public:
stack_linkedlist() //constructor
{
top=NULL;
}
bool isempty() //isempty
{
return top==NULL;
}
void push(string a) //push
{
node*ptr = new node;
ptr->item=a;
ptr->next=top;
top=ptr;
}
string pop_string() //pop
{
if(isempty())
{
cout<<"sorry...stack is empty"<<endl;
return 0;
}else{
string n;
node *ptr;
n=top->item;
ptr=top;
top=top->next;
delete ptr;
return n;
}
}
string get_top() //gettop
{
return top->item;
}
string delete_last() //delete last character in last string in last node
{
if (isempty())
{
cout << "Sorry...stack is empty" << endl;
return" ";
}
node *ptr = top;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
// Delete the last character of the last string
string deletedString;
// Delete the last character of the last string
if (!ptr->item.empty())
{
string a = ptr->item;
int len = a.size();
if (len > 0) {
// Create a new string without the last character
string newString;
for (int i = 0; i < len - 1; ++i) {
newString += a[i];
}
deletedString = a[len - 1];
ptr->item = newString;
}
}
if (ptr->item.empty())
{
pop_string();
}
return deletedString;
}
void display() //display(LIFO OR FILO)
{
for(node *ptr=top ; ptr!=NULL ; ptr=ptr->next)
{
cout<<ptr->item<<" ";
}
cout<<endl;
}
string display_index(int index) //display index nodes
{
int currentindex = 0;
stack_linkedlist tempStack;
// Copy the items to a temporary stack
for (node* ptr = top; ptr != NULL; ptr = ptr->next)
{
tempStack.push(ptr->item);
}
for (node* ptr = tempStack.top; ptr != NULL; ptr = ptr->next)
{
if (currentindex == index)
{
cout << "Item at index " << index << ": " << ptr->item << endl;
return ptr->item ;
}
currentindex++;
}
cout << "Not found" << endl;
return NULL;
}
void display_inverse() // display inverse
{
if (isempty())
{
cout << "Sorry....stack is empty" << endl;
}
stack_linkedlist l;
string a;
node* ptr;
for (ptr = top; ptr != NULL; ptr = ptr->next)
{
a = ptr->item;
l.push(a);
}
l.display();
}
};
// Undo operation function
/*
-Know using pointer to pass stack_linkedlist but when use
pointer to access to methods in stack_linkedlist use (->) not (.).
-The arrow notation is a shorthand for dereferencing a pointer and
accessing the member or method.
*/
void UNDO(stack_linkedlist* Undo, stack_linkedlist* Redo)
{
if (Undo->isempty())
{
Undo->push(Redo->pop_string());
return;
}
// Transfer characters from Undo to Redo
while (!Undo->isempty())
{
string undonstring = Undo->pop_string();
Redo->push(undonstring);
}
}
//to check if digit or not
int is_digit(char a)
{
return (a >= '0' && a <= '9');
}
//main function
int main()
{
//variables
string choice; //choice of main menu
string input=""; //accept string from user
stack_linkedlist text; //main stack
stack_linkedlist history; //history stack
bool datastatus = false; //to check if date entre or not
cout << "----.Hello.-----" << endl;
do
{
cout << "------menu------" << endl;
cout << "1. Append" << endl;
cout << "2. Delete" << endl;
cout << "3. Print" << endl;
cout << "4. Undo" << endl;
cout << "5. Exit" << endl;
// to check number from 1-5 not more or less
while (true)
{
try
{
cout << "Enter a number between 1 and 5: ";
if (cin >> choice)
{
if (choice < "1" || choice > "5")
{
throw invalid_argument("Please enter a number between 1 and 5.");
}
break; // Exit the loop if the input is valid
}
}
catch (const invalid_argument)
{
cout << "Invalid input. Please enter a number between 1 and 5." << endl;
}
}
cout << "----------------" << endl;
if (choice == "5")
{
cout << "Thank You " << endl;
return 0;
}
if (datastatus == false && choice > "1")
{
cout << "Can't do any operation before append" << endl;
}
else
{
if (choice == "1") // Append
{
while (true)
{
cout << "Enter new String : ";
cin >> ws; // consume leading whitespaces
getline(cin, input); // accept input with spaces
// Check if the input contains any empty spaces or digits
bool validInput = true;
for (int i = 0; i < input.size(); i++)
{
if (is_digit(input[i]))
{
validInput = false;
break;
}
}
if (validInput)
{
break; // Exit the loop if the input is valid
}
else
{
cout << "Invalid input. Please enter a name without empty spaces or digits." << endl;
}
}
text.push(input); //store in stack
datastatus = true;
}
else if (choice == "2") // Delete
{
//variables
int n , number ;
start: // label
cout << "1. if you want to delete the last element " << endl;
cout << "2. if you want to delete a numbers of elements " << endl;
cout << "3. if you want to delete the entire string" << endl;
cout << "Enter your choice : ";
cin >> n;
if (n == 1)
{
history.push(text.delete_last());
}
else if (n == 2)
{
cout<<"String : ";
text.display_inverse();
cout<<"Enter number of elements you want delete : ";
cin>>number;
for(int i =0 ; i<number ; i++)
{
history.push(text.delete_last());
}
}
else if (n == 3)
{
history.push(text.pop_string());
}
else
{
cout << "Invalid choice. Please try again" << endl;
cout << "----------------" << endl;
goto start; // Back to this label (this label at first of this case)
}
}
else if (choice == "3") // Print
{
//variables
int n , index , specific_index;
string title;
loop: // label
cout << "1. if you want to print a specific index " << endl;
cout << "2. if you want to print the string " << endl;
cout << "Enter your choice : ";
cin >> n;
if (n == 1)
{
cout<<"Enter index to print : ";
cin>>index;
title = text.display_index(index);
cout<<"Enter index to print specific index in this string : ";
cin>>specific_index;
for(int i=0 ; i<title.size() ; i++)
{
if(i==specific_index)
cout<<"item in specific index is : "<<title[i]<<endl;
}
}
else if (n == 2)
{
text.display_inverse();
}
else
{
cout << "Invalid choice. Please try again" << endl;
cout << "----------------" << endl;
goto loop; // Back to this label (this label at first of this case)
}
}
else if (choice == "4") // Undo
{
//passing address of two stack
UNDO(&history,&text);
}
else if (choice == "5") // Exit
{
cout << "Thank You" << endl;
return 0;
}
}
} while (choice != "5");
return 0;
}