示例#1
0
        private static void Implementation1()
        {
            double initialValue = 1;
            double endValue     = 3;

            double[] stepsFunctionCalculator = { 0.1, 0.2, 0.5, 1 };

            //Validate step Values for function value calculation and area calculation as well as starting and
            // end value.
            var stepAreaCalculator = AreaCalculator.stepSize;

            //LogManager.DisableLogging();

            try
            {
                ProgramLogger.Info("Starting Function Calculation");
                InputValidation.ValidateStepSize(stepsFunctionCalculator, initialValue, endValue, stepAreaCalculator);

                var inputs = FunctionValueCalculator.CalculateFunctionValues(initialValue, endValue, stepsFunctionCalculator, x => x);
                var square = FunctionValueCalculator.CalculateFunctionValues(initialValue, endValue, stepsFunctionCalculator, x => x * x);
                var sin    = FunctionValueCalculator.CalculateFunctionValues(initialValue, endValue, stepsFunctionCalculator, x => Math.Sin(x));

                // Print inputs and function values on console for all step sizes
                IPrinter consolePrinter = new ConsolePrinter();
                consolePrinter.Print(inputs, square, sin, stepsFunctionCalculator);

                // Save results to csv file
                IPrinter csvPrinter = new CsvWriter();
                csvPrinter.Print(inputs, square, sin, stepsFunctionCalculator);

                ProgramLogger.Info("Function Calculation was successful");
            }
            catch (Exception)
            {
                ProgramLogger.Error("Step size was zero. Check inputs");
                Console.WriteLine();
                throw;
            }

            //Calculate area under curve with left point of rectangle as height
            AreaCalculatorLeftPointAsHeight leftPoint = new AreaCalculatorLeftPointAsHeight();

            ProgramLogger.Info("Starting Area Calculator");

            var areaLeftPoint = leftPoint.CalculateArea();

            Console.WriteLine();
            Console.WriteLine($"The area for left point as height is {areaLeftPoint}");

            //Calculate area under curve with right point of rectangle as height
            AreaCalculatorRightPointAsHeight rightPoint = new AreaCalculatorRightPointAsHeight();
            var areaRightPoint = rightPoint.CalculateArea();

            Console.WriteLine();
            Console.WriteLine($"The area for right point as height is {areaRightPoint}");

            //Calculate area under curve with middle point of rectangle as height
            AreaCalculatorMiddlePointAsHeight middlePoint = new AreaCalculatorMiddlePointAsHeight();
            var areaMiddlePoint = middlePoint.CalculateArea();

            Console.WriteLine();
            Console.WriteLine($"The area for middle point as height is {areaMiddlePoint}");

            //Calculate area under curve with trapezoids
            AreaCalculatorTrapezoids trapezoids = new AreaCalculatorTrapezoids();
            var areaTrapezoids = trapezoids.CalculateArea();

            Console.WriteLine();
            Console.WriteLine($"The area approximated with trapezoids is {areaTrapezoids}");

            //Print number of iterations performed for area calculation
            var numRectangles = AreaCalculator.CalculateNumberOfRectangles();

            Console.WriteLine();
            Console.WriteLine($"Iteration count: {numRectangles}");

            ProgramLogger.Info("Area Calculation was successful");
        }