Exemplo n.º 1
0
        public string clsHandle(string exp)
        {
            string result = "exp";

            char[]        compair = { '+', '-', '*', '/' };
            string[]      exptmp  = exp.Split(',');
            List <string> rescom  = new List <string>();

            foreach (var item in exptmp)
            {
                #region 进入四则运算判断
                if (item.IndexOfAny(compair, 0) > -1)
                {
                    try
                    {
                        GrammerAnalyzer ga = new GrammerAnalyzer(item);
                        ga.Analyze();
                        Token[]        toks = ga.TokenList;
                        SyntaxAnalyzer sa   = new SyntaxAnalyzer(toks);
                        sa.Analyze();
                        Calculator calc  = new Calculator(sa.SyntaxTree);
                        double     value = calc.Calc();
                        rescom.Add(value.ToString());//加入到list中
                    }
                    catch
                    {
                    }
                }
                else
                {
                    rescom.Add(item.ToString());
                }
                #endregion
            }
            result = string.Join(",", rescom);
            return(result);
        }