示例#1
0
文件: Test.cs 项目: plami/C-Sharp-OOP
        static void Main()
        {
            GenericList <int> list = new GenericList <int>(3);

            list.AddElement(4);
            list.AddElement(100);
            list.InsertElement(54, 1);
            list.InsertElement(54, 0);
            list.RemoveElement(0);
            Console.WriteLine(list);
            Console.WriteLine(list.IfContains(0));
            Console.WriteLine(list.Max <int>());
            GenericList <int> .DisplayVersion();
        }
示例#2
0
文件: Program.cs 项目: xnikoloff/OOP
        static void Main(string[] args)
        {
            GenericList <int> gl = new GenericList <int>(2);


            gl.AddElement(5);
            gl.AddElement(2);
            gl.AddElement(1);

            gl.InsertElement(4, 1);

            gl.PrintList();

            Console.WriteLine("Remove");
            gl.RemoveElement(2);

            gl.PrintList();

            Console.WriteLine("Get element: ");
            gl.GetElement(2);

            Console.WriteLine("Search: ");
            gl.Search(1);

            Console.WriteLine("Print array");
            gl.PrintArray();
        }
示例#3
0
        //5. Write a generic class GenericList<T> that keeps a list of
        //elements of some parametric type T. Keep the elements of the list
        //in an array with fixed capacity which is given as parameter in the
        //class constructor. Implement methods for adding element, accessing
        //element by index, removing element by index, inserting element at given
        //position, clearing the list, finding element by its value and ToString().
        //Check all input parameters to avoid accessing elements at invalid positions.

        //6. Implement auto-grow functionality: when the internal array
        //is full, create a new array of double size and move all elements to it.

        //7. Create generic methods Min<T>() and Max<T>() for finding the
        //minimal and maximal element in the  GenericList<T>. You may need to
        //add a generic constraints for the type T.

        static void Main()
        {
            //this class is for testing purposes

            GenericList <int> testList = new GenericList <int>(1);

            //adding some elements
            testList.AddElement(15);
            testList.AddElement(10);
            testList.AddElement(49);
            testList.AddElement(18);

            //testing indexator
            Console.WriteLine(testList[1]);
            testList[1] = 56;
            Console.WriteLine(testList[1]);

            //testing remove by index
            testList.RemoveElement(1);
            Console.WriteLine(testList[1]);
            testList.AddElement(1);
            testList.RemoveElement(testList.Count - 1);

            //inserting by index
            testList.InsertElement(0, 100);

            //testing element counter
            Console.WriteLine("Count: " + testList.Count);

            //find element
            Console.WriteLine(testList.FindElement(49));

            //to string
            Console.WriteLine(testList.ToString());

            testList.AddElement(1);
            testList.AddElement(1);
            testList.AddElement(1);
            testList.AddElement(1);
            testList.AddElement(1);
            testList.AddElement(1);

            //min & max element
            Console.WriteLine(testList.MinElement());
            Console.WriteLine(testList.MaxElement());

            //clearing the list
            testList.Clear();
            Console.WriteLine();
        }
示例#4
0
        static void Main()
        {
            Console.WriteLine("Hello World.");
            Console.WriteLine("Make new List.");
            GenericList <float> newList = new GenericList <float>(20);

            Console.WriteLine("Add some numbers to the List - 5.3, 6.27, 7.55, 8, 9.09");
            newList.AddElement(5.3f);
            newList.AddElement(6.27f);
            newList.AddElement(7.55f);
            newList.AddElement(8f);
            newList.AddElement(9.09f);
            Console.WriteLine("Print Max element: {0}", newList.Max());
            Console.WriteLine("Print Min element: {0}", newList.Min());
            Console.WriteLine("Add 10 in the biggining.");
            newList.InsertElement(0, 10);
            Console.WriteLine("Print Max element again: {0}", newList.Max());
            Console.WriteLine("Print the List.");
            Console.WriteLine(newList);
            Console.WriteLine("Remove the fourth element.");
            newList.RemoveElement(3);
            Console.WriteLine("Print the List.");
            Console.WriteLine(newList);
            Console.WriteLine("Remove the fourth element.");
            newList.RemoveElement(3);
            Console.WriteLine("Print the List.");
            Console.WriteLine(newList);
            Console.WriteLine("Remove the fourth element.");
            newList.RemoveElement(3);
            Console.WriteLine("Print the List.");
            Console.WriteLine(newList);
            Console.WriteLine("Add 15.56 in the end.");
            newList.InsertElement(3, 15.56f);
            Console.WriteLine("Print the List.");
            Console.WriteLine(newList);
        }
示例#5
0
        static void Main(string[] args)
        {
            GenericList <int> arrayGenerics = new GenericList <int>(10);

            arrayGenerics.AddElement(4);
            arrayGenerics.AddElement(2222);
            arrayGenerics.AddElement(2212);
            arrayGenerics.Clear();
            arrayGenerics.AddElement(4);
            arrayGenerics.AddElement(2);
            arrayGenerics.AddElement(22222);
            arrayGenerics.RemoveElemenByIndext(0);

            arrayGenerics.InsertElement(0, 213321);
            arrayGenerics.RemoveElemenByIndext(0);
            Console.WriteLine(arrayGenerics.ToString());
            Console.WriteLine();

            Console.WriteLine("Max:" + arrayGenerics.Max());
            Console.WriteLine("Min:" + arrayGenerics.Min());
        }
示例#6
0
        static void Main()
        {
            Console.WriteLine("Hello! This program tests the functionality of the GenericList project. It will run a few tests to determine if the code has been written correctly. Enjoy!");
            Console.WriteLine();
            Console.WriteLine("Creating a new generic list of type GenericList<int> and printing it on the console (empty list)");
            GenericList <int> list = new GenericList <int>();

            Console.WriteLine(list.ToString());
            Console.WriteLine();
            List <int> testList = new List <int>();

            testList.Add(3);

            // Testing "add" method
            Console.WriteLine("Adding 4 elements to the list and printing list after each addition:");
            list.AddElement(5);
            Console.WriteLine(list.ToString());
            list.AddElement(89);
            Console.WriteLine(list.ToString());
            list.AddElement(-15);
            Console.WriteLine(list.ToString());
            list.AddElement(7);
            Console.WriteLine(list.ToString());
            Console.WriteLine();

            // Testing "remove" method
            Console.WriteLine("Removing elements at positions 2, 2 and 0 consecutively and printing list after each removal:");
            list.RemoveElement(2);
            Console.WriteLine(list.ToString());
            list.RemoveElement(2);
            Console.WriteLine(list.ToString());
            list.RemoveElement(0);
            Console.WriteLine(list.ToString());
            Console.WriteLine("Press any key to continue testing...");
            Console.ReadKey();
            Console.WriteLine();
            Console.WriteLine("Adding a new element and prinitng the list:");
            list.AddElement(33);
            Console.WriteLine(list.ToString());
            Console.WriteLine();

            // Testing "insert" method
            Console.WriteLine("Inserting '3' at position 1:");
            list.InsertElement(1, 3);
            Console.WriteLine(list.ToString());
            Console.WriteLine("Inserting '-4' at position 0:");
            list.InsertElement(0, -4);
            Console.WriteLine(list.ToString());
            Console.WriteLine("Inserting '-16' at position 3:");
            list.InsertElement(3, -16);
            Console.WriteLine(list.ToString());
            Console.WriteLine();

            // Testing class by doing larger amount of operations
            Console.WriteLine("Adding 50 elements to the list and printing it:");
            for (int i = 0; i <= 50; i++)
            {
                list.AddElement(i);
            }
            Console.WriteLine(list.ToString());
            Console.WriteLine();
            Console.WriteLine("Removing 25 elements from the list and printing it:");
            for (int i = 0; i <= 25; i++)
            {
                list.RemoveElement(i);
            }
            Console.WriteLine(list.ToString());
            Console.WriteLine();
            Console.WriteLine("Inserting 15 elements in the list and printing it:");
            for (int i = 0; i <= 15; i++)
            {
                list.InsertElement(i, i + 3);
            }
            Console.WriteLine(list.ToString());
            Console.WriteLine();

            // Testing "search" method
            Console.WriteLine("Trying to find element with value 15:");
            Console.WriteLine("The element has index [{0}]", list.FindElement(15));
            Console.WriteLine();

            // Testing "min" and "max" methods
            Console.WriteLine("Finding the value of the smallest and the biggest elements in the list:");
            Console.WriteLine("The smallest element is {0}", list.Min <int>());
            Console.WriteLine("The biggest element is {0}", list.Max <int>());
            Console.WriteLine();

            // Testing "clear" method
            Console.WriteLine("Clearing the list and printing it on the console:");
            list.ClearList();
            Console.WriteLine(list.ToString());

            // Saying good bye!
            Console.WriteLine("The test of the GenericList project has been completed successfully! Have a nice day!");
        }
示例#7
0
        static void Main()
        {
            /*### Problem 5. Generic class
             * Write a generic class `GenericList<T>` that keeps a list of elements of some parametric type `T`.
             * Keep the elements of the list in an array with fixed capacity which is given as parameter
             * Implement methods for:
             * adding element
             * accessing element by index
             * removing element by index
             * inserting element at given position
             * clearing the list
             * finding element by its value
             * override `ToString()`.
             * Check all input parameters to avoid accessing elements at invalid positions.*/

            //Create a generic list of strings with lenght of 3
            GenericList <string> testList = new GenericList <string>(3);

            //adding elements
            Console.WriteLine("<========== Adding Elements =========>");
            testList.AddElement("1");
            testList.AddElement("2");
            testList.AddElement(" ^");
            Console.WriteLine(testList); //ToString is overwritten!
            Console.WriteLine();

            //remove element at index 2
            Console.WriteLine("<========== Removing Element at Index 2 =========>");
            testList.RemoveElement(2);
            Console.WriteLine(testList);
            Console.WriteLine();

            //insert element
            Console.WriteLine("<========== Insert Element at Index 0 =========>");
            testList.InsertElement("666", 0);
            Console.WriteLine(testList);
            Console.WriteLine();

            //access element at given position
            Console.WriteLine("<========== Access Element at Index 1 =========>");
            Console.WriteLine(testList[1]);
            Console.WriteLine();

            //find element
            Console.WriteLine("<========== Search for Element =========>");
            Console.WriteLine(testList.FindElement("3"));
            Console.WriteLine(testList.FindElement("666"));
            Console.WriteLine();

            //clear the contents of the generic list
            Console.WriteLine("<========== Clear the List =========>");
            Console.WriteLine("\r\nAttempting to clear the generic list.");
            testList.ClearAll();
            Console.WriteLine(testList);

            /*### Problem 6. Auto-grow
             * Implement auto-grow functionality: when the internal array is full, create a new array of double size and move all elements to it.*/


            Console.WriteLine("<========== Testing the Auto-Grow =========>");
            GenericList <double> testList2 = new GenericList <double>(5);

            //create a list with 5 members from 1 to 5;
            for (int i = 1; i <= 5; i++)
            {
                testList2.AddElement(i);
            }
            Console.WriteLine("Initial state of the list:");
            Console.WriteLine(testList2);

            Console.WriteLine("\r\n6 values added...");
            testList2.AddElement(3.14d);
            testList2.AddElement(99);
            testList2.AddElement(98);
            testList2.AddElement(97);
            testList2.AddElement(96);
            testList2.AddElement(95);

            Console.WriteLine("\r\nThe list was increased:");
            Console.WriteLine(testList2);

            /*### Problem 7. Min and Max
             * Create generic methods `Min<T>()` & `Max<T>()` for finding the minimal and maximal elements
             * You may need to add a generic constraints for the type `T`.*/


            Console.WriteLine("\r\n<========== Testing Min & Max =========>");
            Console.WriteLine("Minimal element is {0}", testList2.Min());
            Console.WriteLine("Maximal element is {0}", testList2.Max());
            Console.WriteLine();
        }