Пример #1
0
        public void VisitPrimitiveElement(PrimitiveElement primitiveElement)
        {
            PrimitiveElement element = primitiveElement as PrimitiveElement;

            element.line.Size      = new Size(element.SizeY, element.SizeX);
            element.line.BackColor = Color.Green;
        }
        public static void Execute()
        {
            // Create a tree structure
            var root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            // Create a branch
            var comp = new CompositeElement("Two Circles");

            comp.Add(new PrimitiveElement("Black Circle"));
            comp.Add(new PrimitiveElement("White Circle"));

            root.Add(comp);

            // Add and remove a PrimitiveElement
            var pe = new PrimitiveElement("Yellow Line");

            root.Add(pe);
            root.Remove(pe);

            // Recursively display nodes
            root.Exibir(1);
        }
Пример #3
0
        private static void TestComposite()
        {
            Console.WriteLine("Testing composite...");
            // Create a tree structure
            var root =
                new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            // Create a branch
            var comp =
                new CompositeElement("Two Circles");

            comp.Add(new PrimitiveElement("Black Circle"));
            comp.Add(new PrimitiveElement("White Circle"));
            root.Add(comp);

            // Add and remove a PrimitiveElement
            var pe =
                new PrimitiveElement("Yellow Line");

            root.Add(pe);
            root.Remove(pe);

            // Recursively display nodes
            root.Display(1);
        }
Пример #4
0
        public static void Main()
        {
            var root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Green Line"));
            root.Add(new PrimitiveElement("Blue Line"));


            var composition = new CompositeElement("Two Circles");

            composition.Add(new PrimitiveElement("Black Circle"));
            composition.Add(new PrimitiveElement("White Circle"));
            root.Add(composition);


            var composition1 = new CompositeElement("Two Squares");

            composition1.Add(new PrimitiveElement("Black Square"));
            composition1.Add(new PrimitiveElement("White Square"));
            root.Add(composition1);

            var primitiveElement = new PrimitiveElement("Line");

            root.Add(primitiveElement);
            root.Remove(primitiveElement);

            root.Display(5);
        }
Пример #5
0
            public void Main()
            {
                // Create a tree structure
                CompositeElement root =
                    new CompositeElement("Picture");

                root.Add(new PrimitiveElement("Red Line"));
                root.Add(new PrimitiveElement("Blue Circle"));
                root.Add(new PrimitiveElement("Green Box"));

                // Create a branch
                CompositeElement comp =
                    new CompositeElement("Two Circles");

                comp.Add(new PrimitiveElement("Black Circle"));
                comp.Add(new PrimitiveElement("White Circle"));
                root.Add(comp);

                // Add and remove a PrimitiveElement
                PrimitiveElement pe =
                    new PrimitiveElement("Yellow Line");

                root.Add(pe);
                root.Remove(pe);

                // Recursively display nodes
                root.Display(1);
            }
Пример #6
0
    // Entry point into console application.
    static void Main()
    {
        // Create a tree structure 
        CompositeElement root = new CompositeElement("Picture");
        root.Add(new PrimitiveElement("Red Line"));
        root.Add(new PrimitiveElement("Blue Circle"));
        root.Add(new PrimitiveElement("Green Box"));

        // Create a branch
        CompositeElement comp1 = new CompositeElement("Two Circles");
        comp1.Add(new PrimitiveElement("Black Circle"));
        comp1.Add(new PrimitiveElement("White Circle"));
        root.Add(comp1);

        // Create a branch
        CompositeElement comp2 = new CompositeElement("Two Squares");
        comp2.Add(new PrimitiveElement("Red Square"));
        comp2.Add(new PrimitiveElement("Blue Square"));
        root.Add(comp2);

        // Add and remove a PrimitiveElement
        PrimitiveElement pe1 = new PrimitiveElement("Green Line");
        root.Add(pe1);

        // Add and remove a PrimitiveElement
        PrimitiveElement pe2 = new PrimitiveElement("Yellow Line");
        root.Add(pe2);
        root.Remove(pe2);

        // Recursively display nodes
        root.Display(1);

        // Wait for user
        Console.ReadKey();
    }
Пример #7
0
        public void Main()
        {
            // Create a tree structure

            var root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            // Create a branch

            var compositeElement = new CompositeElement("Two Circles");

            compositeElement.Add(new PrimitiveElement("Black Circle"));
            compositeElement.Add(new PrimitiveElement("White Circle"));
            root.Add(compositeElement);

            // Add and remove a PrimitiveElement

            var primitive = new PrimitiveElement("Yellow Line");

            root.Add(primitive);
            root.Remove(primitive);

            // Recursively display nodes
            root.Display(1);

            //GetComposite will return null if its a primitive element
            primitive.GetComposite()?.Add(new PrimitiveElement("Black Line"));
        }
Пример #8
0
        public void AddAndRemoveFromCompositeElementTest()
        {
            var element = new PrimitiveElement("Red Line");

            root.Add(element);

            Assert.AreEqual(1, root.Elements.Count);

            root.Remove(element);

            Assert.AreEqual(0, root.Elements.Count);
        }
Пример #9
0
        static void CompositeTester()
        {
            #region sample 1
            var root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            var comp = new Composite("Composite X");
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));

            root.Add(comp);
            root.Add(new Leaf("Leaf C"));

            // Add and remove a leaf
            var leaf = new Leaf("Leaf D");
            root.Add(leaf);
            root.Remove(leaf);

            // Recursively display tree
            root.Display(1);
            #endregion

            #region sample 2
            // Create a tree structure
            var rootImpl = new CompositeElement("Picture");
            rootImpl.Add(new PrimitiveElement("Red Line"));
            rootImpl.Add(new PrimitiveElement("Blue Circle"));
            rootImpl.Add(new PrimitiveElement("Green Box"));

            // Create a branch
            var compImpl = new CompositeElement("Two Circles");
            compImpl.Add(new PrimitiveElement("Black Circle"));
            compImpl.Add(new PrimitiveElement("White Circle"));
            rootImpl.Add(compImpl);

            // Add and remove a PrimitiveElement
            var pe = new PrimitiveElement("Yellow Line");
            rootImpl.Add(pe);
            rootImpl.Remove(pe);

            // Recursively display nodes
            rootImpl.Display(1);
            #endregion
        }
Пример #10
0
        private static void ShowComposite()
        {
            Console.WriteLine("================================================");
            Console.WriteLine("Pattern code (Composite):");
            Composite root = new Composite("root");

            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite comp = new Composite("Composite X");

            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));

            root.Add(comp);
            root.Add(new Leaf("Leaf C"));

            Leaf leaf = new Leaf("Leaf D");

            root.Add(leaf);
            root.Remove(leaf);

            root.Display(1);


            Console.WriteLine("\nReal code (Composite):");
            CompositeElement picture = new CompositeElement("Picture");

            picture.Add(new PrimitiveElement("Red Line"));
            picture.Add(new PrimitiveElement("Blue Circle"));
            picture.Add(new PrimitiveElement("Green Box"));

            CompositeElement circles = new CompositeElement("Two Circles");

            circles.Add(new PrimitiveElement("Black Circle"));
            circles.Add(new PrimitiveElement("White Circle"));
            picture.Add(circles);

            PrimitiveElement line = new PrimitiveElement("Yellow Line");

            picture.Add(line);
            picture.Remove(line);

            picture.Display(1);
        }
Пример #11
0
        public static void Test()
        {
            CompositeElement root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));
            CompositeElement comp = new CompositeElement("Two Circle");

            comp.Add(new PrimitiveElement("Black Circle"));
            comp.Add(new PrimitiveElement("White Circle"));
            root.Add(comp);
            PrimitiveElement l = new PrimitiveElement("Yelloow Line");

            root.Add(l);
            root.Display(1);
            root.Remove(l);
            root.Display(1);
        }
        public void TestCompositeRealWorld()
        {
            var picture = new CompositeElement("Picture");

            picture.Add(new PrimitiveElement("Red Line"));
            picture.Add(new PrimitiveElement("Blue Circle"));
            picture.Add(new PrimitiveElement("Green Box"));

            var composite = new CompositeElement("Two Circles");

            composite.Add(new PrimitiveElement("Black Circle"));
            composite.Add(new PrimitiveElement("White Circle"));
            picture.Add(composite);

            var yellowLine = new PrimitiveElement("Yellow Line");

            picture.Add(yellowLine);
            picture.Remove(yellowLine);

            picture.Display(1);
        }
Пример #13
0
    // Entry point into console application.
    static void Main()
    {
        // Create a tree structure
        CompositeElement root = new CompositeElement("Picture");

        root.Add(new PrimitiveElement("Red Line"));
        root.Add(new PrimitiveElement("Blue Circle"));
        root.Add(new PrimitiveElement("Green Box"));

        // Create a branch
        CompositeElement comp1 = new CompositeElement("Two Circles");

        comp1.Add(new PrimitiveElement("Black Circle"));
        comp1.Add(new PrimitiveElement("White Circle"));
        root.Add(comp1);

        // Create a branch
        CompositeElement comp2 = new CompositeElement("Two Squares");

        comp2.Add(new PrimitiveElement("Red Square"));
        comp2.Add(new PrimitiveElement("Blue Square"));
        root.Add(comp2);

        // Add and remove a PrimitiveElement
        PrimitiveElement pe1 = new PrimitiveElement("Green Line");

        root.Add(pe1);

        // Add and remove a PrimitiveElement
        PrimitiveElement pe2 = new PrimitiveElement("Yellow Line");

        root.Add(pe2);
        root.Remove(pe2);

        // Recursively display nodes
        root.Display(1);

        // Wait for user
        Console.ReadKey();
    }
Пример #14
0
        public static void TestCompositeElement()
        {
            var rootImpl = new CompositeElement("Picture");
            var redLine  = new PrimitiveElement("Red Line");

            rootImpl.Add(redLine);
            var blueCircle = new PrimitiveElement("Blue Circle");

            rootImpl.Add(blueCircle);
            var greenBox = new PrimitiveElement("Green Box");

            rootImpl.Add(greenBox);

            var compImpl    = new CompositeElement("Two Circles");
            var blackCircle = new PrimitiveElement("Black Circle");

            compImpl.Add(blackCircle);
            var whiteCircle = new PrimitiveElement("White Circle");

            compImpl.Add(whiteCircle);
            rootImpl.Add(compImpl);
            foreach (var element in rootImpl.Display(1, true).Item2)
            {
                if (element.GetType() == typeof(PrimitiveElement))
                {
                    Assert.True(element.Equals(redLine) || element.Equals(blueCircle) || element.Equals(greenBox));
                }
                else if (element.GetType() == typeof(CompositeElement))
                {
                    foreach (var el2 in element.Display(1, true).Item2)
                    {
                        Assert.True(el2.Equals(blackCircle) || el2.Equals(whiteCircle));
                    }
                }
            }
            var pe = new PrimitiveElement("Yellow Line");

            StringAssert.AreEqualIgnoringCase(rootImpl.Add(pe, true), "");
            StringAssert.AreEqualIgnoringCase(rootImpl.Remove(pe, true), "");
        }
Пример #15
0
        public CompositePattern()
        {
            var root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            var compositeNode = new CompositeElement("Two Circles");

            compositeNode.Add(new PrimitiveElement("Black Circle"));
            compositeNode.Add(new PrimitiveElement("White Circle"));

            root.Add(compositeNode);

            var pnode = new PrimitiveElement("Yellow Line");

            root.Add(pnode);
            root.Remove(pnode);

            root.Display(1);
        }
Пример #16
0
        public (List <CompositeElement>, List <PrimitiveElement>) SpawnObstacles()
        {
            List <CompositeElement> compositeList = new List <CompositeElement>();
            List <PrimitiveElement> primitiveList = new List <PrimitiveElement>();

            CompositeElement obstacle = new CompositeElement("horizontal1", 220, 200, 200, 20);

            obstacle.Add(new PrimitiveElement("vertical1", 400, 200, 200, 20));

            //obstacle.Add(new CompositeElement("horizontal2", 1000, 380, 200, 20));
            //obstacle.Add(new PrimitiveElement("vertical2", 980, 200, 200, 20));

            CompositeElement obstacle2 = new CompositeElement("horizontal2", 1000, 380, 200, 20);

            obstacle2.Add(new PrimitiveElement("vertical2", 980, 200, 200, 20));

            PrimitiveElement verticalMiddle1 = new PrimitiveElement("verticalMiddle1", 610, 0, 200, 20);
            PrimitiveElement verticalMiddle2 = new PrimitiveElement("verticalMiddle2", 810, 500, 200, 20);

            compositeList.Add(obstacle);
            compositeList.Add(obstacle2);

            primitiveList.Add(verticalMiddle1);
            primitiveList.Add(verticalMiddle2);

            //obstacle.Add(obstacle2);
            //obstacle.Add(obstacle2.elements[0]);
            //obstacle.Add(verticalMiddle1);
            //obstacle.Add(verticalMiddle2);
            //obstacle.Display(1);
            //obstacle2.Display(1);
            //verticalMiddle1.Display(1);
            //verticalMiddle2.Display(1);

            obstacleVisit(compositeList, primitiveList);

            return(compositeList, primitiveList);
        }
Пример #17
0
        static void Main(string[] args)
        {
            CompositeElement root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            CompositeElement comp = new CompositeElement("Two Circles");

            comp.Add(new PrimitiveElement("Black Circle"));
            comp.Add(new PrimitiveElement("White Circle"));
            root.Add(comp);

            PrimitiveElement pe = new PrimitiveElement("Yellow Line");

            root.Add(pe);
            root.Remove(pe);

            root.Display(1);

            Console.ReadKey();
        }
Пример #18
0
        static void Main(string[] args)
        {
            #region Design Patterns
            #region Decorator Pattern Example
            Espresso beverage1 = new Espresso();
            Console.WriteLine("Double Mocha Esspresso");
            Mocha d1 = new Mocha(beverage1);
            Mocha d2 = new Mocha(d1);
            Console.WriteLine(d2.Cost() + " " + d2.Description);

            Console.WriteLine("\n House Blend, Moch, Whip");
            HouseBlend beverage2 = new HouseBlend();
            Mocha      d3        = new Mocha(beverage2);
            Whip       d4        = new Whip(d3);
            Console.WriteLine(d4.Cost() + " " + d4.Description);
            #endregion

            #region Factory Pattern
            // Note: constructors call Factory Method
            Document[] documents = new Document[2];

            documents[0] = new Resume();
            documents[1] = new Report();

            // Display document pages
            foreach (Document document in documents)
            {
                Console.WriteLine("\n" + document.GetType().Name + "--");
                foreach (Page page in document.Pages)
                {
                    Console.WriteLine(" " + page.GetType().Name);
                }
            }
            #endregion

            #region Abstract Factory Pattern
            ContinentFactory africa = new AfricaFactory();
            AnimalWorld      world  = new AnimalWorld(africa);
            world.RunFoodChain();

            ContinentFactory america = new AmericaFactory();
            world = new AnimalWorld(america);
            world.RunFoodChain();
            #endregion

            #region Singleton
            Console.WriteLine(Singleton.Math.Instance.add(34, 3));
            #endregion

            #region Obsever Pattern
            BaggageHandler  provider  = new BaggageHandler();
            ArrivalsMonitor observer1 = new ArrivalsMonitor("BaggageClaimMonitor1");
            ArrivalsMonitor observer2 = new ArrivalsMonitor("SecurityExit");

            provider.BaggageStatus(712, "Detroit", 3);
            observer1.Subscribe(provider);
            provider.BaggageStatus(712, "Kalamazoo", 3);
            provider.BaggageStatus(400, "New York-Kennedy", 1);
            provider.BaggageStatus(712, "Detroit", 3);
            observer2.Subscribe(provider);
            provider.BaggageStatus(511, "San Francisco", 2);
            provider.BaggageStatus(712);
            observer2.Unsubscribe();
            provider.BaggageStatus(400);
            provider.LastBaggageClaimed();
            #endregion

            #region Strategy Pattern
            SortedList studentRecords = new SortedList();
            studentRecords.Add("samual");
            studentRecords.Add("Jimmy");
            studentRecords.Add("Sandra");
            studentRecords.Add("Vivek");
            studentRecords.Add("Anna");

            studentRecords.SetSortStrategy(new QuickSort());
            studentRecords.Sort();

            studentRecords.SetSortStrategy(new ShellSort());
            studentRecords.Sort();

            studentRecords.SetSortStrategy(new MergeSort());
            studentRecords.Sort();
            #endregion

            #region Composite Pattern
            CompositeElement root = new CompositeElement("Picture");
            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            CompositeElement comp = new CompositeElement("Two Circles");
            comp.Add(new PrimitiveElement("Black Circle"));
            comp.Add(new PrimitiveElement("White Circle"));
            root.Add(comp);

            PrimitiveElement pe = new PrimitiveElement("Yellow Line");
            root.Add(pe);
            root.Remove(pe);

            root.Display(1);
            #endregion

            #region Command Pattern
            User user = new User();

            user.Compute('+', 100);
            user.Compute('-', 50);
            user.Compute('*', 10);
            user.Compute('/', 2);

            user.Undo(4);
            user.Redo(3);
            #endregion

            #region COR (Chain of Responsibility)
            // Setup Chain of Responsibility
            Handler h1 = new ConcreteHandlerA();
            Handler h2 = new ConcreteHandlerB();
            Handler h3 = new ConcreteHandlerC();
            h2.SetSuccessor(h3);
            h1.SetSuccessor(h2);


            // Generate and process request
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }
            #endregion

            #endregion

            #region AD

            #region List
            MyArrayList list = new MyArrayList(5);
            //setting values
            list.add(1);
            list.add(2);
            list.add(3);
            list.add(4);
            list.add(5);

            Console.WriteLine(list.get(2) + " staat op positie 2");
            //testing print function
            list.print();
            //testing set function
            list.set(2, 10); //true
            list.print();
            list.set(10, 2); //false
            //occurences test
            list.set(0, 2);
            list.set(4, 2);
            list.countOccurences(2);
            //testing clear
            list.clear();
            list.print();

            MyLinkedList <int> linkedList = new MyLinkedList <int>();
            linkedList.addFirst(1);
            linkedList.addFirst(12);
            linkedList.addFirst(3);
            linkedList.addFirst(4);
            linkedList.addFirst(5);
            linkedList.insert(0, 123);
            linkedList.remove(12);
            MyLinkedList <int> .printList(linkedList);

            Console.ReadLine();
            #endregion

            #region Graph
            Graph g = new Graph();

            try
            {
                StreamReader d = new StreamReader(Console.ReadLine());
                string       line;
                while ((line = d.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                    string[] words = line.Split(' ');
                    try
                    {
                        if (words.Length > 3)
                        {
                            Console.WriteLine("Skipping bad line: " + line);
                            continue;
                        }
                        string source = words[0];
                        string dest   = words[1];
                        int    cost   = Int32.Parse(words[2]);

                        g.addEdge(source, dest, cost);
                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine("Skipping bad line: " + line);
                        Console.WriteLine(e);
                    }
                }
            }
            catch (Exception e) { Console.WriteLine(e); }


            //while (processRequest(g));
            #endregion

            #region Binairy tree
            string seperator = "-----------------------------------------------------------------------";
            Console.WriteLine(seperator);
            Console.WriteLine("Binary Tree Basic: ");
            BinaryNode <int> NodeA = new BinaryNode <int>(2);
            BinaryNode <int> NodeB = new BinaryNode <int>(5);
            BinaryNode <int> NodeC = new BinaryNode <int>(6);
            BinaryNode <int> NodeD = new BinaryNode <int>(3);
            BinaryNode <int> NodeE = new BinaryNode <int>(7);
            BinaryNode <int> NodeF = new BinaryNode <int>(10);
            BinaryNode <int> NodeG = new BinaryNode <int>(1);

            NodeE.setRight(NodeG);

            NodeA.setLeft(NodeC);
            NodeA.setRight(NodeD);

            NodeB.setLeft(NodeE);
            NodeB.setRight(NodeF);


            BinaryTree <int> tree = new BinaryTree <int>(12);
            tree.Root.setLeft(NodeA);
            tree.Root.setRight(NodeB);

            tree.printPreOrder();
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Binary Search Tree: ");
            BinaryTree <int> BST = new BinaryTree <int>();
            BST.Insert(5);
            BST.Insert(1254);
            BST.Insert(252);
            BST.Insert(378);
            BST.Insert(45);

            BST.Insert(668);
            BST.Insert(71);
            BST.Insert(8);
            BST.Insert(942);
            //fail test
            BST.Insert(1);

            Console.WriteLine(seperator);
            Console.WriteLine("Printing in order: ");
            BST.printInOrder();
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Finding Min:");
            Console.WriteLine(BST.FindMin());
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Finding Max: ");
            Console.WriteLine(BST.FindMax());
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Finding value 5:");
            Console.WriteLine(BST.Find(5));
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Removing 5");
            BST.Remove(5);
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Printing in order: ");
            BST.printInOrder();
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Removing Min");
            BST.RemoveMin();
            Console.ReadLine();

            Console.WriteLine(seperator);
            Console.WriteLine("Printing in order: ");
            BST.printInOrder();
            Console.ReadLine();
            #endregion

            #region Max heap
            MaxHeap theHeap = new MaxHeap(21);
            theHeap.Insert(40);
            theHeap.Insert(70);
            theHeap.Insert(20);
            theHeap.Insert(60);
            theHeap.Insert(50);
            theHeap.Insert(100);
            theHeap.Insert(82);
            theHeap.Insert(35);
            theHeap.Insert(90);
            theHeap.Insert(10);
            theHeap.DisplayHeap();

            Console.WriteLine("Inserting a new node with value 120");
            theHeap.Insert(120);
            theHeap.DisplayHeap();

            Console.WriteLine("Removing max element");
            theHeap.Remove();
            theHeap.DisplayHeap();

            Console.WriteLine("Changing root to 130");
            theHeap.HeapIncreaseDecreaseKey(0, 130);
            theHeap.DisplayHeap();

            Console.WriteLine("Changing root to 5");
            theHeap.HeapIncreaseDecreaseKey(0, 5);
            theHeap.DisplayHeap();
            Console.ReadLine();
            #endregion

            #region Min heap
            int[] arrA = { 50, 2, 30, 7, 40, 4, 10, 16, 12 };
            Console.WriteLine("Original Array : ");
            for (int i = 0; i < arrA.Length; i++)
            {
                Console.Write("  " + arrA[i]);
            }
            MinHeap m = new MinHeap(arrA.Length);
            Console.WriteLine("\nMin-Heap : ");
            m.createHeap(arrA);
            m.display();
            Console.WriteLine("Extract Min :");
            for (int i = 0; i < arrA.Length; i++)
            {
                Console.Write("  " + m.extractMin());
            }
            #endregion

            #region Sorting
            Console.WriteLine();
            int[] tmpArray = { 0, 15, 48, 60, 88, 123, 1, 5, 4, 12, 99, 11, 200, 41 };
            Console.WriteLine(seperator);
            Console.WriteLine("Base Array: ");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);
            var watch = System.Diagnostics.Stopwatch.StartNew();
            SortingAlgorithms.ShellSort(tmpArray);
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Shell sorted Array in: " + elapsedMs + "ms");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);
            Console.ReadKey();

            tmpArray = new[] { 0, 15, 48, 60, 88, 123, 1, 5, 4, 12, 99, 11, 200, 41 };
            Console.WriteLine("Base Array: ");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);

            watch = System.Diagnostics.Stopwatch.StartNew();
            SortingAlgorithms.PerformInsertionSort(tmpArray);
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Insertion sorted Array in: " + elapsedMs + "ms");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);
            Console.ReadKey();

            tmpArray = new[] { 0, 15, 48, 60, 88, 123, 1, 5, 4, 12, 99, 11, 200, 41 };
            Console.WriteLine("Base Array: ");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);

            watch = System.Diagnostics.Stopwatch.StartNew();
            SortingAlgorithms.MergeSort(tmpArray);
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Merge sorted Array in: " + elapsedMs + "ms");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);
            Console.ReadKey();

            tmpArray = new[] { 0, 15, 48, 60, 88, 123, 1, 5, 4, 12, 99, 11, 200, 41, 46 };
            Console.WriteLine("Base Array: ");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);

            watch = System.Diagnostics.Stopwatch.StartNew();
            SortingAlgorithms.QuickSort(tmpArray, 0, tmpArray.Length - 1);
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Quick sorted Array in: " + elapsedMs + "ms");
            SortingAlgorithms.Show_array_elements(tmpArray);
            Console.WriteLine(seperator);
            Console.ReadKey();
            #endregion
            #endregion
            Console.ReadKey();
        }
Пример #19
0
  public static void Main( string[] args )
  {   
    // Create a tree structure 
    CompositeElement root = new CompositeElement( "Picture" );
    root.Add( new PrimitiveElement( "Red Line" ));
    root.Add( new PrimitiveElement( "Blue Circle" ));
    root.Add( new PrimitiveElement( "Green Box" ));

    CompositeElement comp = new CompositeElement( "Two Circles" );
    comp.Add( new PrimitiveElement( "Black Circle" ) );
    comp.Add( new PrimitiveElement( "White Circle" ) );
    root.Add( comp );

    // Add and remove a PrimitiveElement
    PrimitiveElement l = new PrimitiveElement( "Yellow Line" );
    root.Add( l );
    root.Remove( l );

    // Recursively display nodes
    root.Display( 1 );
  }
Пример #20
0
        static void Main(string[] args)
        {
            Console.WriteLine("******Singleton******\n");

            LoadBalancer L1 = LoadBalancer.GetLoadBalancer();
            LoadBalancer L2 = LoadBalancer.GetLoadBalancer();
            LoadBalancer L3 = LoadBalancer.GetLoadBalancer();
            LoadBalancer L4 = LoadBalancer.GetLoadBalancer();

            if (L1 == L2 && L2 == L3 && L3 == L4)
            {
                Console.WriteLine("Same Instance");
            }

            LoadBalancer load = LoadBalancer.GetLoadBalancer();

            for (int i = 0; i < 15; i++)
            {
                Console.WriteLine(load.Server);
            }

            Console.WriteLine("\n******AbstractFactory******\n");

            ContinentFactory africa = new AfricaFactory();
            AnimalWorld      earth  = new AnimalWorld(africa);

            earth.RunFoodChain();

            ContinentFactory america = new AmericaFactory();

            earth = new AnimalWorld(america);
            earth.RunFoodChain();

            Console.WriteLine("\n******Factory******\n");

            Document[] document = new Document[2];
            document[0] = new Report();
            document[1] = new Resume();

            foreach (Document doc in document)
            {
                Console.WriteLine(doc.GetType().Name + " contains:");
                foreach (Page page in doc.Pages)
                {
                    Console.WriteLine("\t" + page.GetType().Name);
                }
            }
            Console.WriteLine("\n******Facade******\n");
            Mortgage mort      = new Mortgage();
            Customer customer  = new Customer("Chad");
            int      amount    = 100000;
            bool     elligable = mort.IsElligable(customer, amount);

            Console.WriteLine(customer.Name + " is " + (elligable ? "elligable" : "not elligable"));

            Console.WriteLine("\n******Decorator******\n");
            Book book = new Book("George RR Martin", "A song of ice and fire", 22);

            book.Display();

            Video video = new Video("Jaws", "Spielberg", 92, 18);

            video.Display();

            Borrowable borrowableVideo = new Borrowable(video);

            borrowableVideo.BorrowItem("Sky");
            borrowableVideo.BorrowItem("Inga");
            borrowableVideo.Display();

            Console.WriteLine("\n******Prototype******\n");
            ColorManager colorManager = new ColorManager();

            colorManager["red"]   = new Color(255, 0, 0);
            colorManager["green"] = new Color(0, 255, 0);
            colorManager["blue"]  = new Color(0, 0, 255);

            colorManager["angry"] = new Color(255, 54, 0);
            colorManager["peace"] = new Color(128, 211, 128);
            colorManager["flame"] = new Color(211, 34, 20);

            Color color1 = colorManager["red"].Clone() as Color;
            Color color2 = colorManager["peace"].Clone() as Color;
            Color color3 = colorManager["flame"].Clone() as Color;

            Console.WriteLine("\n******Builder******\n");
            VehicleBuilder builder;

            Shop shop = new Shop();

            builder = new ScooterBuilder();
            shop.Construct(builder);
            builder.Vehicle.Show();

            builder = new CarBuilder();
            shop.Construct(builder);
            builder.Vehicle.Show();

            builder = new MotorcycleBuilder();
            shop.Construct(builder);
            builder.Vehicle.Show();

            Console.WriteLine("\n******Adapter******\n");
            Compound unknown = new RichCompound("Unknown");

            unknown.Display();
            Compound water = new RichCompound("Water");

            water.Display();
            Compound benzene = new RichCompound("Benzene");

            benzene.Display();
            Compound ethanol = new RichCompound("Ethanol");

            ethanol.Display();

            Console.WriteLine("\n******Bridge******\n");
            Customers customers = new Customers("Chicago");

            customers.Data = new CustomersData();
            customers.Show();
            customers.Next();
            customers.Show();
            customers.Next();
            customers.Show();
            customers.Add("Henry Velasquez");
            customers.ShowAll();

            Console.WriteLine("\n******Composite******\n");
            CompositeElement root = new CompositeElement("Picture");

            root.Add(new PrimitiveElement("Red Line"));
            root.Add(new PrimitiveElement("Blue Circle"));
            root.Add(new PrimitiveElement("Green Box"));

            CompositeElement comp = new CompositeElement("Two Circles");

            comp.Add(new PrimitiveElement("Black Circle"));
            comp.Add(new PrimitiveElement("White Circle"));
            root.Add(comp);

            PrimitiveElement pe = new PrimitiveElement("Yellow Line");

            root.Add(pe);
            root.Remove(pe);

            root.Display(1);

            Console.WriteLine("\n******Observer******\n");
            IBM ibm = new IBM("IBM", 120.00);

            ibm.Attach(new Investor("Sorros"));
            ibm.Attach(new Investor("Berkshire"));

            ibm.Price = 120.10;
            ibm.Price = 121.00;
            ibm.Price = 120.50;
            ibm.Price = 120.75;

            Console.WriteLine("\n******Strategy******\n");
            SortedList studentRecords = new SortedList();

            studentRecords.Add("Samuel");
            studentRecords.Add("Jimmy");
            studentRecords.Add("Sandra");
            studentRecords.Add("Vivek");
            studentRecords.Add("Anna");

            studentRecords.SetSortStrategy(new QuickSort());
            studentRecords.Sort();

            studentRecords.SetSortStrategy(new ShellSort());
            studentRecords.Sort();

            studentRecords.SetSortStrategy(new MergeSort());
            studentRecords.Sort();

            /*
             * Console.WriteLine("\n******Template******\n");
             * DataAccessObject daoCategories = new Categories();
             * daoCategories.Run();
             *
             * DataAccessObject daoProducts = new Products();
             * daoProducts.Run();
             */

            Console.WriteLine("\n***Chain of Responsibility***\n");
            Approver larry = new Director();
            Approver sam   = new VicePresident();
            Approver tammy = new President();

            larry.SetSuccessor(sam);
            sam.SetSuccessor(tammy);

            Purchase p = new Purchase(2034, 350.00, "Assets");

            larry.ProcessRequest(p);

            p = new Purchase(2035, 32590.10, "Project X");
            larry.ProcessRequest(p);

            p = new Purchase(2036, 122100.00, "Project Y");
            larry.ProcessRequest(p);

            Console.WriteLine("\n******Command******\n");
            User user = new User();

            user.Compute('+', 100);
            user.Compute('-', 50);
            user.Compute('*', 10);
            user.Compute('/', 2);

            user.Undo(4);

            user.Redo(3);

            Console.ReadKey();
        }
Пример #21
0
        public void AddToPrimitiveElementTest()
        {
            PrimitiveElement element = new PrimitiveElement("Red Line");

            element.Add(new PrimitiveElement("White Circle"));
        }