Пример #1
0
 /// <summary>
 /// 使用double对建立一个区间
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <param name="leftIsOpen">左边是否是开区间</param>
 /// <param name="rightIsOpen">右边是否是开区间</param>
 public IntervalDoublePair(double left, double right, bool leftIsOpen = false, bool rightIsOpen = false)
 {
     if (left > right)
     {
         throw new Exception("区间double不可以左边大于右边");
     }
     LeftInterval  = new IntervalDouble(left);
     RightInterval = new IntervalDouble(right);
 }
Пример #2
0
        /// <summary>
        /// 尝试从字符串中获取区间
        /// </summary>
        /// <param name="str"></param>
        /// <param name="intervalDoublePair"></param>
        /// <param name="force">若是能转为double但无法转为区间,强制转换为区间(即left=right值)</param>
        /// <returns></returns>
        public static bool TryGetIntervalDoublePair(string str, out IntervalDoublePair intervalDoublePair, bool force = false)
        {
            if (IntervalDouble.GetInterval(str, out IntervalDouble left, out IntervalDouble right, force))
            {
                intervalDoublePair = new IntervalDoublePair(left, right);
                return(true);
            }

            intervalDoublePair = default;
            return(false);
        }
Пример #3
0
        /// <summary>
        /// 获取区间值(左到右)返回IntervalDouble类型,默认闭区间
        /// </summary>
        /// <param name="str"></param>
        /// <param name="intervalLeft">左区间 (、[</param>
        /// <param name="intervalRight">右区间 )、]</param>
        /// <param name="force">若是能转为double但无法转为区间,强制转换为区间(即left=right值)</param>
        /// <returns></returns>
        public static bool GetInterval(string str, out IntervalDouble intervalLeft, out IntervalDouble intervalRight, bool force = false)
        {
            intervalLeft  = new IntervalDouble(0);
            intervalRight = new IntervalDouble(0);
            if (str.IsNullOrWhiteSpace())
            {
                return(false);
            }
            Regex regex = new Regex(@"[\[\(](-?\d*\.?\d+)?.+?(-?\d*\.?\d+)?[\]\)]");//匹配区间(浮点数以及负数支持)

            if (regex.IsMatch(str))
            {
                var groups = regex.Match(str).Groups;

                if (groups.Count == 3 && !groups[0].Value.IsNullOrWhiteSpace()) //第一组捕获组是全部
                {
                    if (double.TryParse(groups[1].Value, out double left))
                    {
                        if (groups[0].Value.StartsWith("("))
                        {
                            intervalLeft.IsOpen = true;
                        }
                        intervalLeft.Value = left;
                    }
                    else
                    {
                        intervalLeft.NumType = NumberType.Infinitesimal; //无法判断或空则是认为是无穷小
                    }
                    if (double.TryParse(groups[2].Value, out double right))
                    {
                        if (groups[0].Value.EndsWith(")"))
                        {
                            intervalRight.IsOpen = true;
                        }
                        intervalRight.Value = right;
                    }
                    else
                    {
                        intervalRight.NumType = NumberType.Infinity; //无法判断或空则是认为是无穷大
                    }
                }

                if (intervalLeft > intervalRight)
                {
                    return(false);
                }
                return(true);
            }
            else if (GetInterval(str, out double left, out double right))
            {
                intervalLeft.Value  = left;
                intervalRight.Value = right;
                return(true);
            }
Пример #4
0
 /// <summary>
 /// 使用IntervalDouble对建立一个区间
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 public IntervalDoublePair(IntervalDouble left, IntervalDouble right)
 {
     LeftInterval  = left;
     RightInterval = right;
 }
Пример #5
0
 /// <summary>
 /// 建立初始区间[0,0]
 /// </summary>
 public IntervalDoublePair()
 {
     LeftInterval  = new IntervalDouble(0);
     RightInterval = new IntervalDouble(0);
 }
Пример #6
0
 /// <summary>
 /// 指示是否是无穷数值(无穷大或无穷小)
 /// </summary>
 /// <param name="intervalDouble"></param>
 /// <returns></returns>
 public static bool IsInfinite(IntervalDouble intervalDouble)
 {
     return(intervalDouble != null && intervalDouble.NumType != NumberType.RealNumber);
 }