コード例 #1
0
        public static void createDictionary()
        {
            int dictionaryNumber = 0;

            while (dictionaryNumber < 1 || dictionaryNumber > 2)
            {
                Console.WriteLine("What will we be inserting into the dictionary?");
                Console.WriteLine("1. Strings");
                Console.WriteLine("2. Integers");

                dictionaryNumber = Convert.ToInt32(Console.ReadLine());
            }

            CamDictionary <string> dictionary1 = new CamDictionary <string>();
            CamDictionary <int>    dictionary2 = new CamDictionary <int>();

            int i = 0;

            while (i < 1 || i > 4)
            {
                Console.WriteLine("Select option:");
                Console.WriteLine("1. Manually build dictionary");
                Console.WriteLine("2. Automatic build with small dataset");
                Console.WriteLine("3. Automatic build with large dataset");
                Console.WriteLine("4. Automatic build with hardcore dataset");

                i = Convert.ToInt32(Console.ReadLine());
            }

            int j = 0;

            while (j < 1 || j > 2)
            {
                Console.WriteLine("Enable optimisations (AVL tree balancing)?");
                Console.WriteLine("1. Yes");
                Console.WriteLine("2. No");

                j = Convert.ToInt32(Console.ReadLine());
            }

            if (j == 2)
            {
                dictionary1.noOptimisations = true;
                dictionary2.noOptimisations = true;
            }

            int k = 0;

            while (k < 1 || k > 2)
            {
                Console.WriteLine("Enable live feedback of every operation?");
                Console.WriteLine("Note: If optimisations are disabled, heights will all be shown as zero.");
                Console.WriteLine("Note 2: It is recommended to turn this OFF for large datasets.");
                Console.WriteLine("It will crash on systems with low RAM if you try!!");
                Console.WriteLine("1. Yes");
                Console.WriteLine("2. No");

                k = Convert.ToInt32(Console.ReadLine());
            }

            if (k == 1)
            {
                dictionary1.visualFeedback = true;
                dictionary2.visualFeedback = true;
            }

            if (dictionaryNumber == 1)
            {
                buildStringDictionary(dictionary1, i);
            }
            else
            {
                buildIntegerDictionary(dictionary2, i);
            }

            bool inProgram = true;

            while (inProgram)
            {
                int choice = 0;
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Which operation would you like to perform?");
                Console.WriteLine("1. Add");
                Console.WriteLine("2. Delete");
                Console.WriteLine("3. Search");
                Console.WriteLine("4. Min");
                Console.WriteLine("5. Max");
                Console.WriteLine("6. Predecessor");
                Console.WriteLine("7. Successor");
                Console.WriteLine("8. Display dictionary diagram");
                Console.WriteLine("9. List all entries");
                Console.WriteLine("10. Display log");
                Console.WriteLine("11. Exit");

                choice = Convert.ToInt32(Console.ReadLine());
                Console.ForegroundColor = ConsoleColor.Yellow;
                if (dictionaryNumber == 1)
                {
                    switch (choice)
                    {
                    case 1:
                        Console.WriteLine("Add a string to the dictionary");
                        dictionary1.add(Console.ReadLine());
                        break;

                    case 2:
                        Console.WriteLine("Delete a string from the dictionary");
                        dictionary1.delete(Console.ReadLine());
                        break;

                    case 3:
                        Console.WriteLine("Search for a string in the dictionary");
                        string stringToSearch = Console.ReadLine();
                        bool   found          = dictionary1.contains(stringToSearch);
                        if (found)
                        {
                            Console.WriteLine("String " + stringToSearch + " was found!");
                        }
                        else
                        {
                            Console.WriteLine("String " + stringToSearch + " not found!");
                        }
                        break;

                    case 4:
                        Console.WriteLine("The min value is: " + dictionary1.min());
                        break;

                    case 5:
                        Console.WriteLine("The max value is: " + dictionary1.max());
                        break;

                    case 6:
                        Console.WriteLine("Find the predecessor of a string in the dictionary");
                        stringToSearch = Console.ReadLine();
                        Console.WriteLine("The predecessor of " + stringToSearch + " is " + dictionary1.predecessor(stringToSearch));
                        break;

                    case 7:
                        Console.WriteLine("Find the successor of a string in the dictionary");
                        stringToSearch = Console.ReadLine();
                        Console.WriteLine("The successor of " + stringToSearch + " is " + dictionary1.successor(stringToSearch));
                        break;

                    case 8:
                        dictionary1.print();
                        break;

                    case 9:
                        Console.WriteLine(dictionary1.toString());
                        break;

                    case 10:
                        Console.WriteLine(dictionary1.getLogString());
                        break;

                    case 11:
                        inProgram = false;
                        break;
                    }
                }
                else
                {
                    switch (choice)
                    {
                    case 1:
                        Console.WriteLine("Add an integer to the dictionary");
                        dictionary2.add(Convert.ToInt32(Console.ReadLine()));
                        break;

                    case 2:
                        Console.WriteLine("Delete an integer from the dictionary");
                        dictionary2.delete(Convert.ToInt32(Console.ReadLine()));
                        break;

                    case 3:
                        Console.WriteLine("Search for an integer in the dictionary");
                        int  integer = Convert.ToInt32(Console.ReadLine());
                        bool found   = dictionary2.contains(integer);
                        if (found)
                        {
                            Console.WriteLine("Integer " + integer + " was found!");
                        }
                        else
                        {
                            Console.WriteLine("Integer " + integer + " not found!");
                        }
                        break;

                    case 4:
                        Console.WriteLine("The min value is: " + dictionary2.min());
                        break;

                    case 5:
                        Console.WriteLine("The max value is: " + dictionary2.max());
                        break;

                    case 6:
                        Console.WriteLine("Find the predecessor of an integer in the dictionary");
                        integer = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("The predecessor of " + integer + " is " + dictionary2.predecessor(integer));
                        break;

                    case 7:
                        Console.WriteLine("Find the successor of an integer in the dictionary");
                        integer = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("The successor of " + integer + " is " + dictionary2.successor(integer));
                        break;

                    case 8:
                        dictionary2.print();
                        break;

                    case 9:
                        Console.WriteLine(dictionary2.toString());
                        break;

                    case 10:
                        Console.WriteLine(dictionary2.getLogString());
                        break;

                    case 11:
                        inProgram = false;
                        break;
                    }
                }
            }
        }