C# 字符串函数计算

2021-02-15 00:16

阅读:485

标签:替换   one   分析   字符串函数   容器   foreach   ima   private   ==   

仅供参考:

#region 字符串函数计算
        /// 
        /// 字符串函数运算
        /// 格式1:@函数名(参数1,参数2...)
        /// 格式2:@函数名(参数1,参数2...)+@函数名(参数1,参数2...)-@函数名(参数1,参数2...)....
        /// 
        /// 
        /// 
        private decimal StringToFunction(string str)
        {
            decimal result = 0;

            //是否带有运算符
            if (Regex.IsMatch(str, @"[\+|\-|\*|\/]"))
            {
                //替换所有运算符,方便分析函数(参数)
                var newstr = Regex.Replace(str, @"[\+|\-|\*|\/]", "(*_*)");
                //函数结果容器
                Listdecimal> list_decimal = new Listdecimal>();
                foreach (string hs in newstr.Split(new string[] { "(*_*)" }, StringSplitOptions.None).ToList())
                {
                    var dec = GetData(hs);
                    list_decimal.Add(dec);
                }

                //运算符
                var matches = Regex.Matches(str, @"[\+|\-|\*|\/]");
                for (int de = 0; de )
                {
                    if (de == 0)
                    {
                        result += list_decimal[de];
                    }
                    else
                    {
                        switch (matches[de - 1].Value)
                        {
                            case "+": result += list_decimal[de]; break;
                            case "-": result -= list_decimal[de]; break;
                            case "*": result *= list_decimal[de]; break;
                            case @"/": result /= list_decimal[de]; break;
                        }
                    }
                }
            }
            else //不带运算符
            {
                result = GetData(str);
            }
            return result;
        }
        /// 
        /// 数据获取
        /// 
        /// 
        /// 
        private decimal GetData(string str)
        {
            decimal result = 0;
            //解析函数
            var modelFunction = GetFunction(str);

            switch (modelFunction.funname.ToLower())
            {
                case "xj_je":

                    break;
                case "jqy":
                    //result = JQY(modelFunction.parameter);
                    break;
                case "-jqy":
                    //result = JQY(modelFunction.parameter);
                    break;
                case "nc":
                    result = NC(modelFunction.parameter);
                    break;
                case "ye":
                    result = YE(modelFunction.parameter);
                    break;
            }
            return result;
        }
        #endregion



        /// 
        /// 获取函数名
        /// 
        /// 字符串
        /// 
        private ModelFunction GetFunction(string str)
        {
            string funname = Regex.Replace(str, @"@|(\(.+\))", "");
            Liststring> parameter = new Liststring>();
            var p = Regex.Matches(str, @"\d+");
            for (int i = 0; i )
            {
                parameter.Add(p[i].Value);
            }
            ModelFunction model = new ModelFunction()
            {
                funname = funname,
                parameter = parameter
            };
            return model;
        }

        private class ModelFunction
        {
            /// 
            /// 函数名称
            /// 
            public string funname { get; set; }
            /// 
            /// 参数
            /// 
            public Liststring> parameter { get; set; }
        }

 

C# 字符串函数计算

标签:替换   one   分析   字符串函数   容器   foreach   ima   private   ==   

原文地址:https://www.cnblogs.com/OleRookie/p/8441905.html


评论


亲,登录后才可以留言!