Esempio n. 1
0
        private FormulaNode Parse(List <string> replacedFormulas, Dictionary <string, string> subFormulas)
        {
            //string
            var c = Formula[0];
            var s = c == '\'' || c == '"' ? Formula.IndexOf(c, 1) : 0;
            //(x,y)
            var i = Formula.IndexOf(',', s);

            if (i > 0)
            {
                Operation = ",";
                Left      = new FormulaNode(this, Formula.Substring(0, i).Trim()).Parse(replacedFormulas, subFormulas);
                Right     = new FormulaNode(this, Formula.Substring(i + 1).Trim()).Parse(replacedFormulas, subFormulas);
                return(this);
            }
            //<,>,<=,>=,=,!=,+,-,*,/
            foreach (var oper in new[] { "&&", "&", "||", "|", "<>", "<=", ">=", "!=", "<", ">", "=", "+", "-", "*", "/", "^" }) //важна последовательность
            {
                i = Formula.IndexOf(oper, s, StringComparison.Ordinal);
                if (i > 0)
                {
                    Operation = oper;
                    Left      = new FormulaNode(this, Formula.Substring(0, i).Trim()).Parse(replacedFormulas, subFormulas);
                    Right     = new FormulaNode(this, Formula.Substring(i + oper.Length).Trim()).Parse(replacedFormulas, subFormulas);
                    return(this);
                }
            }
            if (Formula.Length > 0 && (Formula[0] == '!' || Formula[0] == '+' || Formula[0] == '-'))
            {
                Operation = "f";
                Left      = new FormulaNode(this, Formula[0] + "");
                Right     = new FormulaNode(this, Formula.Substring(1)).Parse(replacedFormulas, subFormulas);
                return(this);
            }
            //subformulas
            i = Formula.IndexOf('#', s);
            if (i == 0)
            {
                Formula = replacedFormulas[int.Parse(Formula.Substring(s + 1))];
                Parse(subFormulas);
            }
            else if (i > 0)
            {
                Operation = "f";
                Left      = new FormulaNode(this, Formula.Substring(0, i).Trim());
                Right     = new FormulaNode(this, replacedFormulas[int.Parse(Formula.Substring(i + 1))]).Parse(subFormulas);
            }
            else if (subFormulas != null && subFormulas.ContainsKey(Formula))
            {
                Formula = subFormulas[Formula];
                Parse(subFormulas);
            }
            else
            {
                AddOperands(Formula);
            }
            return(this);
        }
Esempio n. 2
0
 private FormulaNode(FormulaNode parent, string formula)
 {
     if (formula.IsEmpty())
     {
         throw new Exception("Incorrect formula");
     }
     Parent  = parent;
     Formula = formula;
 }