c# 多数值区间判断是否有重叠

2021-02-17 02:19

阅读:650

标签:func   private   with   交集   sum   表示   amp   special   only   

/// 
    /// 金额区间判断帮助类 
    /// 
    public static class DecimalRangeHelper
    {
        /// 
        /// 是否有交集
        /// 
        /// 
        /// 
        /// 
        public static bool IsIntersectionWith(this DecimalRange currentRange, DecimalRange otherRange)
        {
            return currentRange.Min.In(otherRange.Min, otherRange.Max) || currentRange.Max.In(otherRange.Min, otherRange.Max) && otherRange.Min != currentRange.Max;
        }

        /// 
        /// 判断金额区间存在交集
        /// 
        /// 
        /// 
        public static bool ExistsIntersectionRange(this List currentRanges)
        {
            return currentRanges.Any(p => currentRanges.Where(q => !object.ReferenceEqual(p,q)).Any(z => p.IsIntersectionWith(z)));
        }
    }

 

/// 
    /// 金额区间对应类 
    /// 
    public class DecimalRange
    {
        /// 
        /// 最大
        /// 
        private decimal max;

        /// 
        /// 最小值
        /// 
        public decimal Min { get; set; }

        /// 
        /// 最大值
        /// 
        public decimal Max
        {
            get
            {
                return (max == 0) ? Decimal.MaxValue : max;
            }
            set
            {
                max = value;
            }
        }

        /// 
        /// ToString
        /// 
        /// 
        public override string ToString()
        {
            return Min.ToString()+"-"+Max.ToString();
        }
    }

 

 /// 
    /// 金额帮助类
    /// 
    public static class DecimalHelper
    {
        /// 
        /// 判断指定金额是否在指定金额范围内
        /// 
        public static readonly Funcdecimal, decimal, decimal, bool> IsInDecimalPeriodByMomney = (current, min, max) => min  current;

        /// 
        /// 判断指定金额是否在指定金额范围内
        /// 
        public static bool In(this decimal current, decimal min, decimal max)
        {
            return IsInDecimalPeriodByMomney(current, min, max);
        }

        /// 
        /// 判断指定金额范围是否包含指定金额范围内(max=0时表示不限制)
        /// 
        public static bool InSpecial(this decimal currentMin, decimal currentMax, decimal min, decimal max)
        {
            if (max == 0)
            {
                max = Decimal.MaxValue;
            }
            if (currentMax == 0)
            {
                currentMax = Decimal.MaxValue;
            }
            return currentMin.In(min, max);
        }
    }

 

c# 多数值区间判断是否有重叠

标签:func   private   with   交集   sum   表示   amp   special   only   

原文地址:https://www.cnblogs.com/zhshlimi/p/8393387.html


评论


亲,登录后才可以留言!