static void TestingGenericListFrom5To7()
        {
            Console.WriteLine("------------------- Testing Generic List From Task 5 To 7 -------------------");
            //********** TASK 5 **********
            Console.WriteLine("********** TASK 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.
            GenericList <int> genericList = new GenericList <int>(5);

            //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().
            genericList.Add(5);
            Console.WriteLine("Added element to the list is: {0}", genericList[0]);
            genericList.RemoveByIndex(0);
            genericList.Insert(0, 100);
            Console.WriteLine("Element {0} is inserted at position 0 ", genericList[0]);
            genericList.Add(5);
            genericList.Add(1);
            genericList.Add(2);
            genericList.Add(3);
            Console.WriteLine("Find element in the list that its value is 5. Its index is: {0}", genericList.Find(5));
            Console.WriteLine("This is .ToString method: {0}", genericList.ToString());
            genericList.Clear();
            Console.WriteLine("The list is cleared.");

            //********** TASK 6 **********
            Console.WriteLine("********** TASK 6 **********");
            Console.WriteLine("The GenericList is growing automaticly");

            //********** TASK 7 **********
            Console.WriteLine("********** TASK 7 **********");
            //Create generic methods Min<T>() and Max<T>() for finding the minimal and maximal element in the GenericList<T>.
            genericList.Add(5);
            genericList.Add(1);
            genericList.Add(2);
            genericList.Add(3);
            genericList.Add(231);
            Console.WriteLine("Min element on the list is: {0}", genericList.Min());
            Console.WriteLine("Max element on the list is: {0}", genericList.Max());
        }
Beispiel #2
0
        static void Main()
        {
            Point3D firstPoint = new Point3D(6, 5, 4);

            Console.WriteLine("First point: {0}", firstPoint);

            Console.WriteLine("Second Point: {0}", Point3D.O);

            Console.WriteLine("Distance between 2 points: {0:F2}", DistanceBetween2Points.CalculateDistance(firstPoint, Point3D.O));

            PathStorage.SavePath(); // Saving random sequence of some points in text file.

            Console.WriteLine(new string('=', 42) + "\r\nLoaded sequnce of points from a saved file\r\n" + new string('=', 42));
            PathStorage.LoadPath(); // Loading the sequence from the saved file and printing it on the console.

            Console.WriteLine(new string('=', 48) + "\r\nTesting GenericList's Add function and Auto-Grow\r\n" + new string('=', 48));
            GenericList <Point3D> pointsInList = new GenericList <Point3D>();

            for (int i = 0; i < 10; i++)
            {
                pointsInList.Add(new Point3D(rnd.Next(0, 100), rnd.Next(0, 100), rnd.Next(0, 100)));
                Console.WriteLine("[Index: {0}] {1}", i, pointsInList[i]);
            }

            Console.WriteLine(new string('=', 37) + "\r\nTesting GenericList's Access function\r\n" + new string('=', 37));
            Point3D accessPoint = pointsInList.AccessElement(5);

            Console.WriteLine(accessPoint);

            Console.WriteLine(new string('=', 37) + "\r\nTesting GenericList's Remove function\r\n" + new string('=', 37));
            pointsInList.Remove(5);
            for (int i = 0; i < pointsInList.Count; i++)
            {
                Console.WriteLine("[Index: {0}] {1}", i, pointsInList[i]);
            }

            Console.WriteLine(new string('=', 39) + "\r\nTesting GenericList's InsertAt function\r\n" + new string('=', 39));
            pointsInList.InsertAt(new Point3D(32, 17, 98), 5);
            for (int i = 0; i < pointsInList.Count; i++)
            {
                Console.WriteLine("[Index: {0}] {1}", i, pointsInList[i]);
            }

            Console.WriteLine(new string('=', 49) + "\r\nTesting GenericList's FindIndexOfElement function\r\n" + new string('=', 49));
            var indexOf = pointsInList.FindIndexOfElement(new Point3D(32, 17, 98));

            Console.WriteLine("The index of {0} is: {1}", new Point3D(32, 17, 98), indexOf);

            Console.WriteLine(new string('=', 39) + "\r\nTesting GenericList's ToString function\r\n" + new string('=', 39));
            Console.WriteLine(pointsInList.ToString());

            Console.WriteLine(new string('=', 37) + "\r\nTesting Min<T>() and Max<T>() methods\r\n" + new string('=', 37));
            double distance1 = DistanceBetween2Points.CalculateDistance(pointsInList[0], pointsInList[1]);
            double distance2 = DistanceBetween2Points.CalculateDistance(pointsInList[2], pointsInList[3]);
            double min       = Min <double>(distance1, distance2);
            double max       = Max <double>(distance1, distance2);

            Console.WriteLine("Min distance: {0:F2}", min);
            Console.WriteLine("Max distance: {0:F2}", max);
            Console.WriteLine(new string('=', 20) + "\r\nTesting Mtarix class\r\n" + new string('=', 20));
            Matrix <int> firstMatrix  = new Matrix <int>(3, 3);
            Matrix <int> secondMatrix = new Matrix <int>(3, 3);

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    firstMatrix[i, j]  = (i + 3) + (j + 1) + 2;
                    secondMatrix[i, j] = (i + 1) + (j + 2);
                }
            }
            Console.WriteLine("First Matrix:\r\n{0}", firstMatrix);
            Console.WriteLine("Second Matrix:\r\n{0}", secondMatrix);
            Console.WriteLine(new string('=', 8) + "\r\nAddition\r\n" + new string('=', 8));
            Console.WriteLine("First Matrix + Second Matrix =\n{0}", firstMatrix + secondMatrix);
            Console.WriteLine(new string('=', 12) + "\r\nSubstraction\r\n" + new string('=', 12));
            Console.WriteLine("First Matrix - Second Matrix =\n{0}", firstMatrix - secondMatrix);
            Console.WriteLine(new string('=', 14) + "\r\nMultiplycation\r\n" + new string('=', 14));
            Console.WriteLine("First Matrix * Second Matrix =\n{0}", firstMatrix * secondMatrix);
            Console.WriteLine(new string('=', 13) + "\r\nTrue operator\r\n" + new string('=', 13));
            Matrix <int> anotherMatrix = new Matrix <int>(3, 3);

            Console.WriteLine("This matrix: ");
            if (anotherMatrix)
            {
                Console.WriteLine("{0}\r\ndoesn't containt zero elements", anotherMatrix);
            }
            else
            {
                Console.WriteLine("{0}\r\ncontaints zero elements", anotherMatrix);
            }

            object[] attributes = typeof(DefinigClassesMain).GetCustomAttributes(false);
            Console.WriteLine();
            Console.WriteLine("Version: {0}", attributes[0]);
        }
Beispiel #3
0
        static void Main()
        {
            //Problem 1 test
            Console.WriteLine("Point:");
            Point3D point = new Point3D(2.5, 3, 4);

            Console.WriteLine(point.ToString());
            //Problem 2 test
            Console.WriteLine("\nPoint O:");
            Console.WriteLine(Point3D.getPointO.ToString());
            //Problem 3 test
            Console.WriteLine("\nDistance between two points:");
            Console.WriteLine(Distance.CalculateDistance(Point3D.getPointO, point));

            //Problem 4 test
            Console.WriteLine("\nPath:");
            Path.PointsPath.Add(new Point3D(7, 3, 1));
            Path.PointsPath.Add(new Point3D(3, 1, 9));
            Path.PointsPath.Add(new Point3D(5, 5, 9));
            PathStorage.SavePath();
            Console.WriteLine(PathStorage.LoadPaths());

            //Problems 5, 6 test
            var numbers = new GenericList <int>(4);

            numbers.Add(5);     //Test Add method
            numbers.Add(45);
            numbers.Add(34);
            numbers.Add(3);
            numbers.Add(423);
            numbers.Add(3247);
            Console.WriteLine(numbers);

            var people = new GenericList <string>(2);    //Test GenericList with strings

            people.Add("Ivan");
            people.Add("Petar");
            people.Add("Dimitar");
            people.Add("Georgi");
            Console.WriteLine(people);
            Console.WriteLine();

            numbers.RemoveAtGivenPosition(3);   //Test RemoveAtGivenPosition method
            numbers.RemoveAtGivenPosition(1);
            Console.WriteLine(numbers);
            people.RemoveAtGivenPosition(0);
            Console.WriteLine(people);
            Console.WriteLine();

            numbers[0] = 555;   //Test indexer
            Console.WriteLine(numbers);
            people[2] = "Mariya";
            Console.WriteLine(people);
            Console.WriteLine();

            numbers.AddAtGivenPosition(999, 0);     //Test AddAtGivenPosition method
            numbers.AddAtGivenPosition(11, 0);
            Console.WriteLine(numbers);
            people.AddAtGivenPosition("Petya", 0);
            Console.WriteLine(people);
            Console.WriteLine();

            Console.WriteLine("Index of 555: " + numbers.IndexOf(555) + ", Index of 3247: " + numbers.IndexOf(3247)); // Test IndexOf method
            Console.WriteLine("Index of Mariya: " + people.IndexOf("Mariya"));
            Console.WriteLine();

            //Problem 7 test
            Console.WriteLine("Min element of numbers: " + numbers.Min());   //Test Min method
            Console.WriteLine("Max element of numbers: " + numbers.Max());   //Test Max method
            Console.WriteLine("Min element of names:" + people.Min());
            Console.WriteLine("Max element of names:" + people.Max());
            Console.WriteLine();

            numbers.RemoveAll();    //Test RemoveAll method(Problem 6)
            Console.WriteLine(numbers);
            people.RemoveAll();
            Console.WriteLine(people);
            Console.WriteLine();

            //Problems 8,9
            var matrix = new Matrix <double>();

            matrix[2, 1] = 2.4;
            matrix[4, 2] = 10.3;
            matrix[1, 0] = 45.4;
            Console.WriteLine(matrix[2, 1] + ", " + matrix[4, 2] + ", " + matrix[1, 0]);

            var numberMatrix = new Matrix <int>();

            matrix[3, 1] = 2;
            matrix[1, 2] = 10;
            matrix[2, 0] = 45;
            Console.WriteLine(matrix[3, 1] + ", " + matrix[1, 2] + ", " + matrix[2, 0] + "\n");

            //Problem 10
            var firstMatrix = new Matrix <int>();

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    firstMatrix[i, j] = i + 1;
                }
            }

            var secondMatrix = new Matrix <int>();

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    secondMatrix[i, j] = i + 2;
                }
            }
            Console.WriteLine(firstMatrix);
            Console.WriteLine();
            Console.WriteLine(secondMatrix);
            Console.WriteLine();
            Console.WriteLine(firstMatrix + secondMatrix);
            Console.WriteLine();
            Console.WriteLine(secondMatrix - firstMatrix);
            Console.WriteLine();
            Console.WriteLine(firstMatrix * secondMatrix);
            Console.WriteLine();

            //Problem 11
            VersionAttribute vs = new VersionAttribute();

            vs.getVersion();
        }
        static void Main(string[] args)
        {
            /*
             * 11. Version attribute
             *
             *  Create a [Version] attribute that can be applied to structures, classes, interfaces, enumerations and methods and holds a version in the format major.minor (e.g. 2.11).
             *  Apply the version attribute to a sample class and display its version at runtime.
             *
             */
            Type type = typeof(TestHomework);

            object[] vers = type.GetCustomAttributes(false);
            foreach (VersionAttribute version in vers)
            {
                Console.WriteLine("The version is [{0}.{1}]",
                                  version.Version, version.SubVersion);
            }

            /*
             * 1. Structure
             *
             *  Create a structure Point3D to hold a 3D-coordinate {X, Y, Z} in the Euclidian 3D space.
             *  Implement the ToString() to enable printing a 3D point.
             *
             */
            Console.WriteLine(new String('-', 73));
            Console.WriteLine();
            Point3D point1 = new Point3D(3, 4, 5);

            Console.WriteLine("Point: " + point1);

            /*
             * 2. Static read-only field
             *
             *  Add a private static read-only field to hold the start of the coordinate system – the point O{0, 0, 0}.
             *  Add a static property to return the point O.
             *
             */
            Console.WriteLine(new String('-', 73));
            Console.WriteLine("Center O : " + Point3D.O);

            /*
             * 3. Static class
             *
             *  Write a static class with a static method to calculate the distance between two points in the 3D space.
             *
             */
            Console.WriteLine(new String('-', 73));
            Point3D point2 = new Point3D(5, 4, 3);

            Console.WriteLine("Distance between two points {0} and {1} is {2:F3}", point1, point2, DistanceBetweenPoints.Distance(point1, point2));

            /*
             * 4. Path
             *
             *  Create a class Path to hold a sequence of points in the 3D space.
             *  Create a static class PathStorage with static methods to save and load paths from a text file.
             *  Use a file format of your choice.
             *
             */
            Console.WriteLine(new String('-', 73));
            List <Point3D> path = PathStorage.Load(@"..\..\fileWithPoints_test.txt");

            Console.WriteLine("Path with poinst loaded from a .txt file :");
            foreach (Point3D p in path)
            {
                Console.WriteLine(p);
            }
            PathStorage.Save(@"..\..\TestSavePath.txt", path);
            Console.WriteLine("Those points were saved to ' " + @"..\..\TestSavePath.txt" + " ' ");

            /*
             * 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 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.
             *
             */
            Console.WriteLine(new String('-', 73));

            Console.WriteLine("Creating Generic list");
            GenericList <int> genericListInt = new GenericList <int>(3);

            genericListInt.Add(1);
            genericListInt.Add(2);
            genericListInt.Add(3);
            Console.WriteLine("Added to list : " + genericListInt);
            Console.WriteLine("Element on index [1] is : " + genericListInt[1]);
            genericListInt.RemoveAt(1);
            Console.WriteLine("Removed element on index [1], list is : " + genericListInt);
            genericListInt.InsertAt(1, 100);
            Console.Write("Inserted element '{0}' ot position {1}. ", genericListInt[1], 1);
            Console.WriteLine("List is: " + genericListInt);

            /*
             * 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(new String('-', 73));
            Console.WriteLine("Before adding elements list is {0} and has capacity: {1}",
                              genericListInt, genericListInt.Capacity);
            genericListInt.Add(101);
            genericListInt.Add(102);
            genericListInt.Add(103);
            genericListInt.Add(104);
            Console.WriteLine("After adding a few elements the list is {0} \nand it's capacity is : {1}",
                              genericListInt, genericListInt.Capacity);

            /*
             * 7. Min and Max
             *
             *  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.
             *
             */
            Console.WriteLine(new String('-', 73));
            Console.WriteLine("Minimal element is: {0} ", genericListInt.Min());
            Console.WriteLine("Maximal element is: {0} ", genericListInt.Max());
            Console.WriteLine("List before clear: \n" + genericListInt);
            genericListInt.Clear();
            Console.WriteLine("List after clear: \n" + genericListInt + "  it's empty :( ");

            /*
             * 8. Matrix
             *
             *  Define a class Matrix<T> to hold a matrix of numbers (e.g. integers, floats, decimals).
             *
             * 9. Matrix indexer
             *
             *  Implement an indexer this[row, col] to access the inner matrix cells.
             *
             * 10. Matrix operations
             *
             *  Implement the operators + and - (addition and subtraction of matrices of the same size) and * for matrix multiplication.
             *  Throw an exception when the operation cannot be performed.
             *  Implement the true operator (check for non-zero elements).
             *
             */

            Console.WriteLine(new String('-', 73));
            Matrix <int> matr1 = new Matrix <int>(3, 2);

            matr1[0, 0] = 1;
            matr1[0, 1] = 1;
            matr1[1, 0] = 1;
            matr1[1, 1] = 1;
            matr1[2, 0] = 1;
            matr1[2, 1] = 1;
            Console.WriteLine("Matrix1 is : \n" + matr1);
            Matrix <int> matr2 = new Matrix <int>(3, 2);

            matr2[0, 0] = 2;
            matr2[0, 1] = 2;
            matr2[1, 0] = 2;
            matr2[1, 1] = 2;
            matr2[2, 0] = 2;
            matr2[2, 1] = 2;
            Console.WriteLine("Matrix2 is : \n" + matr2);
            Console.WriteLine("Matrix1 + Matrix2 : \n" + (matr1 + matr2));
            Console.WriteLine("Matrix1 - Matrix2 : \n" + (matr1 - matr2));

            Matrix <int> multMatr = new Matrix <int>(2, 5);

            multMatr[0, 0] = 0;
            multMatr[0, 1] = 0;
            multMatr[0, 2] = 0;
            multMatr[0, 3] = 0;
            multMatr[0, 4] = 0;
            multMatr[1, 0] = 0;
            multMatr[1, 1] = 0;
            multMatr[1, 2] = 0;
            multMatr[1, 3] = 0;
            multMatr[1, 4] = 0;
            Console.WriteLine("Matrix 3 : \n" + multMatr);
            Console.WriteLine("Matrix1 * Matrix3 : ");
            Console.WriteLine(matr1 * multMatr);
            if (matr1 * multMatr)
            {
                Console.WriteLine("The matrix: \n" + (matr1 * multMatr));
                Console.WriteLine("DOES NOT CONTAIN ZERO");
            }
            else
            {
                Console.WriteLine("The matrix: \n" + (matr1 * multMatr));
                Console.WriteLine("CONTAINS ZERO");
            }
        }