//Hier werden vorbereitende Arbeiten durchgeführt. protected virtual string PrepareFormular(string Formular, string OperatorRegEx) { int locBracketCounter = 0; //Klammern überprüfen foreach (char locChar in Formular.ToCharArray()) { if (locChar == '(') { locBracketCounter += 1; } if (locChar == ')') { locBracketCounter -= 1; if (locBracketCounter < 0) { SyntaxErrorException up = new SyntaxErrorException("Too many closing brackets."); throw up; } } } if (locBracketCounter > 0) { SyntaxErrorException up = new SyntaxErrorException("An open bracket was not closed."); throw up; } //White-Spaces entfernen Formular = Regex.Replace(Formular, "\\s", ""); //Vorzeichen verarbeiten if (Formular.StartsWith("-") || Formular.StartsWith("+")) { Formular = Formular.Insert(0, "0"); } //Sonderfall negative Klammer Formular = Regex.Replace(Formular, "\\(-\\(", "(0-("); return(Regex.Replace(Formular, "(?<operator>[" + OperatorRegEx + "\\(])-(?<zahl>[\\d\\.\\,]*)", "${operator}((0-1)*${zahl})")); }
//Überschreibbare Funktion, die die Formelauswertung steuert. protected virtual string Parse(string Formular, string OperatorRegEx) { string locTemp = null; Match locTerm = null; Match locFuncName = null; MatchCollection locMoreInnerTerms = null; List <double> locPreliminaryResult = new List <double>(); bool locFuncFound = false; string locOperatorRegEx = "\\([\\d\\;" + OperatorRegEx + "]*\\)"; FormulaEvaluatorFunction adf = null; locTerm = Regex.Match(Formular, locOperatorRegEx); if (!string.IsNullOrEmpty(locTerm.Value)) { locTemp = Formular.Substring(0, locTerm.Index); //Befindet sich ein Funktionsname davor? locFuncName = Regex.Match(locTemp, "[a-zA-Z]*", RegexOptions.RightToLeft); //Gibt es mehrere, durch ; getrennte Parameter? locMoreInnerTerms = Regex.Matches(locTerm.Value, "[\\d" + OperatorRegEx + "]*[;|\\)]"); //Jeden Parameterterm auswerten und zum Parameter-Array hinzufügen foreach (Match locMatch in locMoreInnerTerms) { locTemp = locMatch.Value; locTemp = locTemp.Replace(";", "").Replace(")", ""); locPreliminaryResult.Add(ParseSimpleTerm(locTemp)); } //Möglicher Syntaxfehler: Mehrere Parameter, aber keine Funktion if (string.IsNullOrEmpty(locFuncName.Value) && locMoreInnerTerms.Count > 1) { SyntaxErrorException up = new SyntaxErrorException("Found bracket pairs without a function!"); throw up; } if (!string.IsNullOrEmpty(locFuncName.Value)) { //Funktionsnamen suchen locFuncFound = false; foreach (FormulaEvaluatorFunction adfWithinLoop in myFunctions) { adf = adfWithinLoop; if (adfWithinLoop.FunctionName.ToUpper() == locFuncName.Value.ToUpper()) { locFuncFound = true; break; } } if (locFuncFound == false) { SyntaxErrorException up = new SyntaxErrorException("Function not found!"); throw up; } else { Formular = Formular.Replace(locFuncName.Value + locTerm.Value, myConstEnumCounter.ToString("000")); double[] locArgs = new double[locPreliminaryResult.Count]; locPreliminaryResult.CopyTo(locArgs); //Diese Warnung bezieht sich auf einen hypothetischen Fall, //der aber nie eintreten kann! :-) myConsts.Add(adf.Operate(locArgs)); myConstEnumCounter += 1; } } else { Formular = Formular.Replace(locTerm.Value, myConstEnumCounter.ToString("000")); myConsts.Add(Convert.ToDouble(locPreliminaryResult[0])); myConstEnumCounter += 1; } } else { return(Formular); } Formular = Parse(Formular, OperatorRegEx); return(Formular); }