From abe9b46ef1515a3ec9e3e9be2a706cca55a774aa Mon Sep 17 00:00:00 2001 From: Fred3267 <231194fck@gmail.com> Date: Sat, 24 Sep 2016 15:20:03 -0600 Subject: [PATCH 1/3] Alternative complete program This is doing exactly the same, but with a method taking care of the sub menu. Please let me know if there is anything I can improve. --- Program.cs | 427 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 311 insertions(+), 116 deletions(-) diff --git a/Program.cs b/Program.cs index d565f4d..2f97c66 100644 --- a/Program.cs +++ b/Program.cs @@ -1,93 +1,122 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ConsoleApplication2 +namespace Group_Data_Structure { class Program { - static void Main(string[] args) + public static int subMenu(int mainMenuPick) { - //declare varibles - Boolean again; - Boolean again2 = true; - int iNum; - string sNum; - Stack staSta = new Stack(); - Stack staSta2 = new Stack(); - - //Loop until user wants to exit - while (again2 == true) - { - Console.WriteLine("Enter your Option\n"); - Console.WriteLine("1. Stack"); - Console.WriteLine("2. Queue"); - Console.WriteLine("3. Dictionary"); - Console.WriteLine("4. Exit\n"); - - sNum = Console.ReadLine(); + int iMenu; + bool bFirstTime = true; + string sMainMenu = ""; + string sSubMenuPick; - /*while (Int32.TryParse(sNum, out iNum) == true && Int32.Parse(sNum) <= 4 && Int32.Parse(sNum) >= 1) + if (mainMenuPick == 1) + { + sMainMenu = "Stack"; + } + else if (mainMenuPick == 2) + { + sMainMenu = "Queue"; + } + else if (mainMenuPick == 3) + { + sMainMenu = "Dictionary"; + } + 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. { - Console.WriteLine("Error. Please enter a Number 1-4"); - sNum = Console.ReadLine(); - }*/ - - iNum = Int32.Parse(sNum); + if (bFirstTime == false) //this will execute if the user fails to enter a valid entry the first time. + { + Console.WriteLine("Please Enter a number between 1-7"); //instructional error message. + } //this is the menu. + Console.WriteLine("Enter your Options\n"); + Console.WriteLine("1. Add one item to " + sMainMenu); + Console.WriteLine("2. Add Huge List of Items to " + sMainMenu); + Console.WriteLine("3. Display " + sMainMenu); + Console.WriteLine("4. Delete from " + sMainMenu); + Console.WriteLine("5. Clear " + sMainMenu); + Console.WriteLine("6. Search " + sMainMenu); + Console.WriteLine("7. Return to Main Menu"); + sSubMenuPick = 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(sSubMenuPick, out iMenu) == false); + iMenu = Convert.ToInt32(sSubMenuPick); //this will convert the input to an int. + } while (iMenu < 1 || iMenu > 7); + return iMenu; + } + static void Main(string[] args) + { + Stack staSta = new Stack(); + Queue qItems = new Queue(); + Dictionary dictDictionary = new Dictionary(); + System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//creates stopwatch item to time the search. - //for stack - if (iNum == 1) + 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 { - //loop until the user wants to exit - while (again = true) + do { - Console.WriteLine("Enter your Option\n"); - Console.WriteLine("1. Add one time to Stack"); - Console.WriteLine("2. Add a 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\n"); - - sNum = Console.ReadLine(); - - /*while (Int32.TryParse(sNum, out iNum) == true && Int32.Parse(sNum) <= 7 && Int32.Parse(sNum) >= 1) + if (bFirstTime == false) { - Console.WriteLine("Error. Please enter a Number 1-7"); - sNum = Console.ReadLine(); - }*/ + 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; - again = true; - iNum = Int32.Parse(sNum); + do + { + bFirstTime = true; + iStaMenu = subMenu(iGenMenu); - //for add - if (iNum == 1) + if (iStaMenu == 1) { Console.WriteLine("What do you want to add?\n"); staSta.Push(Console.ReadLine()); Console.WriteLine("Added Successful\n"); } - //to add 2000 entries - else if (iNum == 2) + else if (iStaMenu == 2) { staSta.Clear(); int counter = 1; - while (counter<= 2000) + while (staSta.Count() <= 2000) { staSta.Push("New Entry" + counter); counter++; } - + Console.WriteLine("Stack cleared\n"); } - - //display all items - else if (iNum == 3) + + else if (iStaMenu == 3) { foreach (string item in staSta) { @@ -97,72 +126,45 @@ static void Main(string[] args) Console.WriteLine("\n"); } - //delete a specific item - else if (iNum == 4) + else if (iStaMenu == 4) { - - - Console.WriteLine("What do you want to Delete?"); - sNum = Console.ReadLine(); - - if (staSta.Contains(sNum)) + foreach (string item in staSta) { - foreach (string item in staSta) - { + Console.WriteLine("What do you want to Delete?"); + sStaValue = Console.ReadLine(); - if (staSta.Peek() != sNum) - { - //holds items that user does not want to delte - staSta2.Push(staSta.Pop()); - } - - else - { - //deletes the item - staSta.Pop(); - Console.WriteLine("Item Deleted"); - - } + if (staSta.Peek().Equals(sStaValue)) + { + staSta.Pop(); + Console.WriteLine("Item Deleted"); } - - int iCounter = staSta2.Count(); - - //gives the item back to the orginal stack - for (int iCount = 0; iCount < iCounter; iCount++) + else { - staSta.Push(staSta2.Pop()); + Console.WriteLine("Item not found"); } } - - else - { - Console.WriteLine("Item Not Found"); - } } - - - //clears stack - else if (iNum == 5) + + else if (iStaMenu == 5) { staSta.Clear(); - Console.WriteLine("Stack cleared\n"); } - - //searchs stack - else if (iNum == 6) + + else if (iStaMenu == 6) { - System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); + foreach (string item in staSta) + { Console.WriteLine("What do you want to search for?"); - sNum = Console.ReadLine(); + sStaValue = Console.ReadLine(); sw.Start(); - if (staSta.Contains(sNum)) + if (staSta.Peek().Equals(sStaValue)) { sw.Stop(); Console.WriteLine("Found It! Time to find: " + sw.Elapsed); - sw.Reset(); + sw.Stop(); } else @@ -170,27 +172,220 @@ static void Main(string[] args) Console.WriteLine("Not found"); sw.Reset(); } + + + } } - else + } while (iStaMenu != 7); + } + else if (iGenMenu == 2) + { + int iQMenu; + string sQMenu; + int iQValue; + string sQValue; + string sQKey; + + do + { + bFirstTime = true; + iQMenu = subMenu(iGenMenu); + if (iQMenu == 1) { - again = false; + Console.WriteLine("Please enter a string"); + qItems.Enqueue(Console.ReadLine()); //Add item to the queue } - } - } - else if (iNum == 2) - { } - else if (iNum == 3) - { } + 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 - { - again2 = false; + 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; + iDictMenu = subMenu(iGenMenu); + 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); } } } - From 0f2d25ece24557755349c7b6ca799970280ea083 Mon Sep 17 00:00:00 2001 From: nshawcroft Date: Sun, 25 Sep 2016 20:25:26 -0600 Subject: [PATCH 2/3] Fixed the queue's huge list --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 2f97c66..d711fa5 100644 --- a/Program.cs +++ b/Program.cs @@ -201,7 +201,7 @@ static void Main(string[] args) { qItems.Clear(); //Clear the queue int counter = 1; - while (qItems.Count() <= 2000) + while (qItems.Count() < 2000) { qItems.Enqueue("New Entry" + counter); //Add item to the queue counter++; //count how many entries have been made From 3d4029875d91e89dcef6cdd02ff8e4cdb407f6d7 Mon Sep 17 00:00:00 2001 From: nshawcroft Date: Sun, 25 Sep 2016 21:08:45 -0600 Subject: [PATCH 3/3] Got the queue's delete item done --- Program.cs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index d711fa5..239e208 100644 --- a/Program.cs +++ b/Program.cs @@ -222,7 +222,31 @@ static void Main(string[] args) string input = ""; Console.Write("Which item do you want to delete?"); input = Console.ReadLine(); - + Queue tempQueue = new Queue(); //Temporary queue to move non-deleted items to + if (qItems.Contains(input)) + { + while (qItems.Count > 0) + { + string currentItem = qItems.Dequeue().ToString(); //Remove and look at the first item in the queue + if (currentItem != input) + { + tempQueue.Enqueue(currentItem); //Don't delete it, add to the temporary queue + } + else + { + //Do nothing, this is the one to delete + } + } + while (tempQueue.Count > 0) + { + qItems.Enqueue(tempQueue.Dequeue()); //Add back the items in the temporary queue to the original queue + } + Console.WriteLine("Item Deleted!"); + } + else + { + Console.WriteLine("Item not found"); //Report that the item to delete isn't in the queue + } } else if (iQMenu == 5)