Exemple #1
0
        public static void Menu(BTree btree)
        {
            string str = "";
            bool exit = false;
            Console.WriteLine("Welcome to the menu: ");

            while (!exit)
            {
                Console.WriteLine(" _________________________________________________");
                Console.WriteLine("|                                                 |");
                Console.WriteLine("| > a - Add the element (double)                  |");
                Console.WriteLine("| > ai - Add an interval from a to b with step c  |");
                Console.WriteLine("| > f - Find an element in the B-tree             |");
                Console.WriteLine("| > d - Delete the element (double)               |");
                Console.WriteLine("| > di - Delete an interval from a to b           |");
                Console.WriteLine("| > o - Out the B-tree                            |");
                Console.WriteLine("| > e - Exit                                      |");
                Console.WriteLine("|_________________________________________________|");

                str = Console.ReadLine();
                switch (str)
                {
                    case "a":
                        {
                            Console.Clear();
                            string elem;
                            double key;
                            bool correct = false;

                            while (!correct)
                            {
                                try
                                {
                                    Console.Write("Add the element: ");
                                    elem = Console.ReadLine();
                                    key = Convert.ToDouble(elem);
                                    correct = true;
                                    btree.Insert(key);
                                }
                                catch (FormatException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                                catch (OverflowException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                            }
                            Console.Clear();
                            btree.Out();
                        }
                        break;

                    case "ai":
                        {
                            Console.Clear();
                            Console.WriteLine("Add an interval from a to b with step c");
                            string a, b, c;
                            double beg, end, step;
                            bool correct = false;

                            while (!correct)
                            {
                                try
                                {
                                    Console.Write("From: ");
                                    a = Console.ReadLine();
                                    Console.Write("To: ");
                                    b = Console.ReadLine();
                                    Console.Write("Step: ");
                                    c = Console.ReadLine();
                                    beg = Convert.ToDouble(a);
                                    end = Convert.ToDouble(b);
                                    step = Convert.ToDouble(c);
                                    correct = true;
                                    for (double i = beg; i < end; i += step)
                                        btree.Insert(i);
                                }
                                catch (FormatException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                                catch (OverflowException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                            }
                            Console.Clear();
                            btree.Out();
                        }
                        break;

                    case "f":
                        {
                            Console.Clear();
                            string elem;
                            double key;
                            bool correct = false;

                            while (!correct)
                            {
                                try
                                {
                                    Console.Write("Find the element: ");
                                    elem = Console.ReadLine();
                                    key = Convert.ToDouble(elem);
                                    correct = true;
                                    if (btree.Find(key) != -1.0)
                                        btree.Out(key);
                                    else
                                        Console.WriteLine("There is no key like it");
                                }
                                catch (FormatException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                                catch (OverflowException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                            }
                        }
                        break;

                    case "d":
                        {
                            Console.Clear();
                            string elem;
                            double key;
                            bool correct = false;

                            while (!correct)
                            {
                                try
                                {
                                    Console.Write("Delete the element: ");
                                    elem = Console.ReadLine();
                                    key = Convert.ToDouble(elem);
                                    correct = true;
                                    btree.Out(key);
                                    if (!btree.Delete(key))
                                    {
                                        Console.WriteLine("The element isn't in the tree");
                                        elem = Console.ReadLine();
                                        Console.Clear();
                                        break;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Delete?");
                                        elem = Console.ReadLine();
                                        Console.Clear();
                                        btree.Out();
                                    }
                                }
                                catch (FormatException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                                catch (OverflowException)
                                {
                                    Console.Write("Incorrect input, try again: ");
                                    correct = false;
                                }
                            }
                        }
                        break;

                    case "di":
                        {
                            Console.Clear();
                            Console.WriteLine("Not Workable");
                        }
                        break;

                    case "o":
                        {
                            Console.Clear();
                            btree.Out();
                        }
                        break;

                    case "e":
                        {
                            exit = true;
                            Console.Clear();
                        }
                        break;

                    default:
                        {
                            Console.Clear();
                        }
                        break;
                }
            }
        }
Exemple #2
0
 static void Main(string[] args)
 {
     BTree btree = new BTree();
     Menu(btree);
 }