public Function(string src, int start, EquationElement root) { m_startIndex = start; m_func = Term.ExtractName(src, start).ToLower(); start += m_func.Length; // sauter tous les espaces, mais le prochain caractère doit être une parenthèse ouvrante if (start == src.Length) throw new ParseException(src, m_startIndex, "La fonction doit commencer par '('"); while (src[start] == ' ') start++; if (src[start] != '(') throw new ParseException(src, m_startIndex, "La fonction doit commencer par '('"); var termstart = start; var end = Term.FindMatchingEnd('(', src, termstart); while (end < termstart) { src += ')'; end = Term.FindMatchingEnd('(', src, termstart); } /*if (end < termstart) { throw new ParseException(src, m_startIndex, "Pas de parenthèse fermante correspondante trouvée"); }*/ m_endindex = end; var allterms = src.Substring(termstart + 1, end - termstart - 1); //string[] terms = allterms.Split(','); var terms = GetTerms(allterms); m_terms = new List<Term>(); foreach (var term in terms) { var newterm = new Term(); newterm.Parse(term, root); newterm.Parent = this; m_terms.Add(newterm); } }
public void Parse(string equation, EquationElement root) { Stack.Clear(); Parse(equation, 0, root); CombineTerms(new[] {"^"}); CombineTopDown(); CombineTerms(new[] {"*", "/", "%"}); }
private int Parse(string equation, int index, EquationElement root) { while (index < equation.Length) { var ch = equation[index]; if (char.IsWhiteSpace(ch)) { index++; continue; } if (Operator.IsValidOperator(equation, index)) { var n = Add(new Operator(equation, index)); index += n.Length; continue; } if (Number.IsValidDigit(equation, index)) { var n = Add(new Number(equation, index)); index += n.Length; continue; } if (Function.IsValidDigit(equation, index)) { var n = Add(new Function(equation, index, root)); index += n.Length; continue; } if (Constant.IsValidDigit(equation, index)) { var n = Add(new Constant(equation, index)); index += n.Length; continue; } if (Variable.IsValidDigit(equation, index, root)) { var n = Add(new Variable(equation, index, root)); index += n.Length; continue; } index++; if (IsValidDigit(ch)) { var endindex = FindMatchingEnd(ch, equation, index - 1); if (endindex > index) { var len = endindex - index; var s = equation.Substring(index, len); var g = Add(new Term()) as Term; len = g.Parse(s, 0, root) + 1; index += len; continue; } throw new ParseException(equation, index - 1, "Pas de parenthèses fermantes correspondantes trouvées"); } } return index; }
private EquationElement Add(EquationElement item) { item.Parent = this; if (item is EquationValue) { // insérer l'opérateur '*' entre les valeurs if (LastElement is EquationValue) Stack.Add(new Operator("*")); var c = PopSignOperator(); if (c != null && (c as Operator).Value == "-") { ((EquationValue) item).Signed = true; } if ((c != null && (c as Operator).Value == "~") || (Stack.Count > 1 && Stack[Stack.Count - 2] is Operator && (Stack[Stack.Count - 2] as Operator).Value == "~")) { ((EquationValue) item).Value = ~(int) (((EquationValue) item).Value); } Stack.Add(item); return item; } if (item is Operator) { // seul - peut démarrer un terme if (((Operator) item).CanStartTerm == false && Stack.Count == 0) throw new ParseException(item.ToString(), 0, "Le terme ne peut pas commencer par un opérateur autre que + ou -"); // si il y a un second opérateur s'assurer que c'est un signe var op = LastElement as Operator; if (op != null && ((Operator) item).IsSign == false) throw new ParseException(ToString().Replace("(", "").Replace(")", ""), 0, "Plusieurs opérateurs à la suite"); if (OperatorCount >= 2) throw new ParseException(ToString().Replace("(", "").Replace(")", ""), 0, "Plusieurs opérateurs à la suite"); Stack.Add(item); return item; } return null; }