Exemplo n.º 1
0
        /// <summary>
        /// 去除比例值为 0 的元素,并重新调整比例
        /// </summary>
        /// <param name="ratios"></param>
        /// <returns></returns>
        private List <AutoRatio> AdjustRatios(List <AutoRatio> ratios)
        {
            Decimal          totalRatio, tmpRatio;
            List <AutoRatio> tmpRatios;

            totalRatio = 0;
            tmpRatios  = new List <AutoRatio>();

            foreach (var r in ratios)
            {
                if (0 != r.percent)
                {
                    tmpRatios.Add(r);
                    totalRatio += r.percent;
                }
            }

            if (tmpRatios.Count == 0)
            {
                throw new Exception("未设置出题比例。");
            }
            else if ((Double)totalRatio < 0.5)
            {
                throw new Exception("出题比例小于 50% 。");
            }
            else if (totalRatio > 1)
            {
                throw new Exception("出题比例大于 100% 。");
            }

            tmpRatio = 0;
            ratios   = tmpRatios;
            foreach (var r in ratios)
            {
                // 重新计算比例,且舍为较小的数
                r.percent = StaticHelper.MathRound((Double)r.percent / (Double)totalRatio, 2);
                tmpRatio += r.percent;
            }

            // 当调整后的百分比仍小于 1 时,将差值累加至最后一个元素
            if (tmpRatio < 1)
            {
                ratios[ratios.Count - 1].percent += 1 - tmpRatio;
            }

            return(ratios);
        }