forked from Fred3267/Data-Structure-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
415 lines (355 loc) · 19.4 KB
/
Program.cs
File metadata and controls
415 lines (355 loc) · 19.4 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Group_Data_Structure
{
class Program
{
static void Main(string[] args)
{
Stack<string> staSta = new Stack<string>();
Queue<string> qItems = new Queue<string>();
Dictionary<string, int> dictDictionary = new Dictionary<string, int>();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//creates stopwatch item to time the search.
bool bFirstTime = true; //this boolean will help facilitate giving an error message if the user inputs an invalid option
int iGenMenu;
string sGenMenu;
do
{
bFirstTime = true;
do
{
do
{
if (bFirstTime == false)
{
Console.WriteLine("Please Enter a number between 1-4");
}
Console.WriteLine("Enter your Options\n");
Console.WriteLine("1. Stack");
Console.WriteLine("2. Queue");
Console.WriteLine("3. Dictionary");
Console.WriteLine("4. Exit\n");
sGenMenu = Console.ReadLine();
bFirstTime = false;
} while (Int32.TryParse(sGenMenu, out iGenMenu) == false);
iGenMenu = Convert.ToInt32(sGenMenu);
} while (iGenMenu < 1 || iGenMenu > 4);
bFirstTime = true;
if (iGenMenu == 1)
{
int iStaMenu;
string sStaMenu;
int iStaValue;
string sStaValue;
string sStaKey;
do
{
bFirstTime = true;
do
{
do
{
if (bFirstTime == false)
{
Console.WriteLine("Please Enter a number between 1-7");
}
Console.WriteLine("Enter your Options\n");
Console.WriteLine("1. Add one item to Stack");
Console.WriteLine("2. Add Huge List of Items to Stack");
Console.WriteLine("3. Display Stack");
Console.WriteLine("4. Delete from Stack");
Console.WriteLine("5. Clear Stack");
Console.WriteLine("6. Search Stack");
Console.WriteLine("7. Return to Main Menu");
sStaMenu = Console.ReadLine();
bFirstTime = false;
} while (Int32.TryParse(sStaMenu, out iStaMenu) == false);
iStaMenu = Convert.ToInt32(sStaMenu);
} while (iStaMenu < 1 || iStaMenu > 7);
if (iStaMenu == 1)
{
Console.WriteLine("What do you want to add?\n");
staSta.Push(Console.ReadLine());
Console.WriteLine("Added Successful\n");
}
else if (iStaMenu == 2)
{
staSta.Clear();
int counter = 1;
while (staSta.Count() <= 2000)
{
staSta.Push("New Entry" + counter);
counter++;
}
Console.WriteLine("Stack cleared\n");
}
else if (iStaMenu == 3)
{
foreach (string item in staSta)
{
Console.WriteLine(item);
}
Console.WriteLine("\n");
}
else if (iStaMenu == 4)
{
foreach (string item in staSta)
{
Console.WriteLine("What do you want to Delete?");
sStaValue = Console.ReadLine();
if (staSta.Peek().Equals(sStaValue))
{
staSta.Pop();
Console.WriteLine("Item Deleted");
}
else
{
Console.WriteLine("Item not found");
}
}
}
else if (iStaMenu == 5)
{
staSta.Clear();
}
else if (iStaMenu == 6)
{
foreach (string item in staSta)
{
Console.WriteLine("What do you want to search for?");
sStaValue = Console.ReadLine();
sw.Start();
if (staSta.Peek().Equals(sStaValue))
{
sw.Stop();
Console.WriteLine("Found It! Time to find: " + sw.Elapsed);
sw.Stop();
}
else
{
Console.WriteLine("Not found");
sw.Reset();
}
}
}
} while (iStaMenu != 7);
}
else if (iGenMenu == 2)
{
int iQMenu;
string sQMenu;
int iQValue;
string sQValue;
string sQKey;
do
{
bFirstTime = true;
do
{
do
{
if (bFirstTime == false)
{
Console.WriteLine("Please Enter a number between 1-7");
}
Console.WriteLine("Enter your Options\n");
Console.WriteLine("1. Add one item to Queue");
Console.WriteLine("2. Add Huge List of Items to Queue");
Console.WriteLine("3. Display Queue");
Console.WriteLine("4. Delete from Queue");
Console.WriteLine("5. Clear Queue");
Console.WriteLine("6. Search Queue");
Console.WriteLine("7. Return to Main Menu");
sQMenu = Console.ReadLine();
bFirstTime = false;
} while (Int32.TryParse(sQMenu, out iQMenu) == false);
iQMenu = Convert.ToInt32(sQMenu);
} while (iQMenu < 1 || iQMenu > 7);
if (iQMenu == 1)
{
Console.WriteLine("Please enter a string");
qItems.Enqueue(Console.ReadLine()); //Add item to the queue
}
else if (iQMenu == 2)
{
qItems.Clear(); //Clear the queue
int counter = 1;
while (qItems.Count() <= 2000)
{
qItems.Enqueue("New Entry" + counter); //Add item to the queue
counter++; //count how many entries have been made
}
}
else if (iQMenu == 3)
{
//Display all items
foreach (string item in qItems)
{
Console.WriteLine(item);
}
}
else if (iQMenu == 4)
{
string input = "";
Console.Write("Which item do you want to delete?");
input = Console.ReadLine();
}
else if (iQMenu == 5)
{
//Clear the queue
qItems.Clear();
Console.WriteLine("All items removed");
}
else if (iQMenu == 6)
{
string input = "";
Console.Write("Enter Search field: ");
input = Console.ReadLine();
//Start the timer
sw.Start();
if (qItems.Contains(input))
{
//Stop the timer
sw.Stop();
Console.WriteLine("Found It! Time to find: " + sw.Elapsed);
//Reset the timer
sw.Reset();
}
else
{
Console.WriteLine("Not found");
sw.Reset();
}
}
} while (iQMenu != 7);
}
else if (iGenMenu == 3)
{
int iDictMenu;
string sDictMenu;
int iDictValue;
string sDictValue;
string sDictKey;
do //this do loop will make sure that the dictionary code is run until the user chooses to exit
{
bFirstTime = true;
do //this loop will make sure that the user inputs a number between (and including) 1 and 7.
{
do //this loop will make sure that the user is actually inputting an int.
{
if (bFirstTime == false) //this will execute if the user fails to enter a valid entry the first time.
{
Console.WriteLine("\nPlease Enter a number between 1-7"); //instructional error message.
} //this is the menu.
Console.WriteLine("\n\nMake a selection.\n");
Console.WriteLine("1. Add one item to Dictionary");
Console.WriteLine("2. Add Huge List of Items to Dictionary");
Console.WriteLine("3. Display Dictionary");
Console.WriteLine("4. Delete from Dictionary");
Console.WriteLine("5. Clear Dictionary");
Console.WriteLine("6. Search Dictionary");
Console.WriteLine("7. Return to Main Menu");
Console.WriteLine();
Console.Write("My Selection: ");
sDictMenu = Console.ReadLine(); //here's where the user inputs option
bFirstTime = false; //this changes the bool to false so that if the loop happens, it will give the error message.
} while (Int32.TryParse(sDictMenu, out iDictMenu) == false);
iDictMenu = Convert.ToInt32(sDictMenu); //this will convert the input to an int.
} while (iDictMenu < 1 || iDictMenu > 7);
Console.WriteLine();
Console.WriteLine();//two blank lines.
if (iDictMenu == 1) //option 1 is selected: Adding one item to dictionary.
{
Console.Write("Add one item to Dictionary \nPlease input the key: ");
sDictKey = Console.ReadLine(); //this will become the dictionary key
Console.Write("Please input the value: ");
do //this loop will make sure that the user has input an integer before it will exit the loop.
{
sDictValue = Console.ReadLine();
if (Int32.TryParse(sDictValue, out iDictValue) == false) //this if statement will display an error message if user input a non-int
{
Console.WriteLine("Please enter a valid number.");
}
} while (Int32.TryParse(sDictValue, out iDictValue) == false);
iDictValue = Convert.ToInt32(sDictValue); //converts from string to integer for dictionary value.
dictDictionary.Add(sDictKey, iDictValue); //add the item to the dictionary using the two obtained value
Console.WriteLine("Item successfully added!");
}
else if (iDictMenu == 2) //option 2: add huge list of items to the dictionary.
{
dictDictionary.Clear(); //clears the current dictionary.
int i = 1;
while (i <= 2000) //this count will create 2000 items in the form of "New Entry 1, New Entry 2,..."
{
sDictKey = "New Entry " + i;
dictDictionary.Add(sDictKey, i);
i++;
}
Console.WriteLine("2000 new entries successfully added!"); //confirmation to the user.
}
else if (iDictMenu == 3) //option 3: displaying the items currently in the dictionary.
{
if (!dictDictionary.Count.Equals(0))
{
string k = "Key"; //this will help facilitate header padding.
string dashes = "--------";
Console.WriteLine(k.PadRight(25, ' ') + "Value\n" + dashes.PadRight(25, ' ') + "-----"); //this is the header
foreach (var input in dictDictionary)//for each item in the dictionary we will print out what is in there
{
Console.WriteLine(input.Key.PadRight(25, ' ') + input.Value);
}
Console.WriteLine("\nPrint Complete!"); //confirmation to the user.
}
else
{
Console.WriteLine("* Your dictionary is currently empty *"); //this line will tell the user that the dictionary is empty
}
}
else if (iDictMenu == 4) //option 4: Delete from Dictionary
{
Console.Write("What do you want to delete: ");//prompt the user to enter then key name of the item they want to delete.
string sDictSearch = Console.ReadLine(); //stores string value the user enters.
if (dictDictionary.ContainsKey(sDictSearch) == true) //this will execute if the dictionary contains the user value.
{
dictDictionary.Remove(sDictSearch);//will remove the item.
Console.WriteLine("\nRecord for " + sDictSearch + " successfully removed."); //confirmation to the user.
}
else if (dictDictionary.ContainsKey(sDictSearch) == false) //this is what executes if no match is found
{
Console.WriteLine("The requested key does not exist!"); //message to user.
}
}
else if (iDictMenu == 5) //option 5: clear all items from the dictionar
{
dictDictionary.Clear(); //this line clears the dictionary.
Console.WriteLine("Dictionary successfully cleared!"); //confirmation to user.
}
else if (iDictMenu == 6) //option 6: search the dictionary
{
Console.Write("What are you searching for: "); //user prompt
string sDictSearch = Console.ReadLine();//this will store the string the user inputs.
sw.Start(); //this starts the stopwatch to time the search.
if (dictDictionary.ContainsKey(sDictSearch) == true) //this is if the the value is found.
{
sw.Stop();
Console.WriteLine("\nItem found\n----------"); //information to the user
Console.WriteLine(sDictSearch.PadRight(25, ' ') + dictDictionary[sDictSearch]); //display the found item
Console.WriteLine("Time to find: " + sw.Elapsed); //display the time it took to find
sw.Reset();//reset the stopwatch
}
else
{
Console.WriteLine("No results found."); //this informs the user that nothing was found
sw.Stop();//stops the stopwatch
sw.Reset();//resets the stopwatch
}
}
} while (iDictMenu != 7); //this line will exit that menu once 7 is selected.
}
} while (iGenMenu != 4);
}
}
}