コード例 #1
0
        public double CalculateAverage(List <double> values, AveragingMethod averagingMethod)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            switch (averagingMethod)
            {
            case AveragingMethod.Mean:
                return(values.Sum() / values.Count);

            case AveragingMethod.Median:
                var sortedValues = values.OrderBy(x => x).ToList();

                int n = sortedValues.Count;

                if (n % 2 == 1)
                {
                    return(sortedValues[(n - 1) / 2]);
                }

                return((sortedValues[sortedValues.Count / 2 - 1] + sortedValues[n / 2]) / 2);

            default:
                throw new ArgumentException("Invalid averagingMethod value");
            }
        }
コード例 #2
0
        public double CalculateAverage(List <double> values, AveragingMethod averagingMethod)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (averagingMethod == null)
            {
                throw new ArgumentNullException(nameof(averagingMethod));
            }

            return(averagingMethod(values));
        }
コード例 #3
0
        public double CalculateAverage(List <double> values, AveragingMethod averagingMethod)
        {
            if (values == null)
            {
                throw  new ArgumentNullException(nameof(values));
            }

            switch (averagingMethod)
            {
            case AveragingMethod.Mean:
                return(CalculateMean(values));

            case AveragingMethod.Median:
                return(CalculateMedian(values));

            default:
                throw new ArgumentException("Invalid averagingMethod value");
            }
        }
コード例 #4
0
        public double CalculateAverage(List <double> values, AveragingMethod averagingMethod)
        {
            switch (averagingMethod)
            {
            case AveragingMethod.Mean:
                return(values.Sum() / values.Count);

            case AveragingMethod.Median:
                var sortedValues = values.OrderBy(x => x).ToList();
                if (sortedValues.Count % 2 == 1)
                {
                    return(sortedValues[(sortedValues.Count - 1) / 2]);
                }
                return((sortedValues[(sortedValues.Count / 2) - 1] +
                        sortedValues[(sortedValues.Count / 2)]) / 2);

            default:
                throw new ArgumentException("Invalid averagingMethod value");
            }
        }
コード例 #5
0
        public void AverageCalculatorTests_ValidEnum(AveragingMethod method, double result)
        {
            Calculator calculator = new Calculator();

            Assert.AreEqual(result, calculator.CalculateAverageWithEnum(values, method), 0.000001);
        }
コード例 #6
0
ファイル: Mathematician.cs プロジェクト: AlexHoha/EPAM
 public Mathematician(AveragingMethod averagingMethod)
 {
     this.averagingMethod = averagingMethod;
 }