c#数学公式拆分成list集合

public static class SplitExp
    {
        static string Pattern = @"^[0-9.]*$";

        static char[] Tokens = { '+', '-', '*', '/', '(', ')', '>', '<', '=', '!', '|', '&', '^', '?', ':', 'Δ', '%' };

        static HashSet<string> KeyWordsSet = new HashSet<string>() { "true", "false", "TRUE", "FALSE", "int", "double", "bool", "abs" };

        public static List<string> GetParas(string Expression)
        {
            List<string> RT = new List<string>();

            if (!string.IsNullOrEmpty(Expression))
            {
                foreach (string Item in Expression.Split(Tokens))
                {
                    if ((!Regex.IsMatch(Item, Pattern)) && (!KeyWordsSet.Contains(Item)))
                    {
                        RT.Add(Item);
                    }
                }
            }
            return RT;
        }
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }
        public static bool IsScope(string value)
        {
            var result = Regex.IsMatch(value, @"^(\[|\()| $(\)|\])");
            return result;
        }


        public static List<string> GetParasAll(string Expression)
        {
            List<string> RT = new List<string>();
            try
            {
                //取出数组
                List<string> ExpList = new List<string>();
                if (!string.IsNullOrEmpty(Expression))
                {
                    foreach (string Item in Expression.Split(Tokens))
                    {
                        ExpList.Add(Item);
                    }
                }


                if (ExpList.Count > 0)
                {
                    int startIndex = 0;
                    int length = 0;
                    string lastString = Expression;
                    if (!string.IsNullOrEmpty(lastString))
                    {
                        for (int i = 0; i < ExpList.Count; i++)
                        {
                            if (!string.IsNullOrWhiteSpace(ExpList[i]))
                            {
                                var stringIndex = lastString.IndexOf(ExpList[i]);
                                //先取第一块数据
                                length = stringIndex - startIndex;
                                string string1 = lastString.Substring(startIndex, length);
                                if (!string.IsNullOrWhiteSpace(string1))
                                {
                                    var ls = MatchFunText(string1);
                                    if (ls.Count > 0)
                                    {
                                        RT.AddRange(ls);
                                    }
                                }
                                lastString = lastString.Substring(length, lastString.Length - length);

                                //取第二块数据
                                RT.Add(ExpList[i]);
                                lastString = lastString.Substring(ExpList[i].Length, lastString.Length - ExpList[i].Length);
                            }
                            //最后一个处理
                            if (i == ExpList.Count - 1)
                            {
                                if (lastString.Length > startIndex)
                                {
                                    RT.Add(lastString.Substring(startIndex, lastString.Length - startIndex));
                                }
                            }

                            length = 0;
                        }

                    }
                }
            }
            catch (Exception ex) {
                return RT;
            }
            return RT;
        }

        public static List<string> FuncText = new List<string> { "+", "-", "*", "/", "(", ")", ">", "<", "=", "!", "||", "&&", "^", "?", ":", "Δ","%",
            "tan", "aver", "sum", "min", "max", "abs","rhs" };
        private static List<string> MatchFunText(string text)
        {
            var chars = text.ToCharArray();
            var returnInfo = new List<string>();
            for (int i = 0; i < chars.Length; i++)
            {
                if (Regex.IsMatch(chars[i].ToString(), Pattern))
                {
                    returnInfo.Add(chars[i].ToString());
                }
                else
                if (FuncText.Contains(chars[i].ToString()))
                {
                    returnInfo.Add(chars[i].ToString());
                }
                else
                if (FuncText.Contains(chars[i].ToString() + chars[i + 1].ToString()))
                {
                    returnInfo.Add(chars[i].ToString() + chars[i + 1].ToString());
                    i = i + 1;
                }
                else
                if (FuncText.Contains(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString()))
                {
                    returnInfo.Add(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString());
                    i = i + 2;
                }
                else
                if (FuncText.Contains(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString() + chars[i + 3].ToString()))
                {
                    returnInfo.Add(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString() + chars[i + 3].ToString());
                    i = i + 3;
                }
                else
                {
                    returnInfo.Add(chars[i].ToString());
                }
            }
            return returnInfo;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值