/// <summary>
 /// Fügt einer der untergeordneten Auflistungen einen neuen Operator hinzu,
 /// in Abhängigkeit von seiner Priorität.
 /// </summary>
 /// <param name="Function"></param>
 /// <remarks></remarks>
 public void AddFunction(FormulaEvaluatorFunction Function)
 {
     //Feststellen, ob es schon eine Auflistung für diese Operator-Priorität gibt
     if (this.Contains(Function.Priority))
     {
         //Ja - dieser hinzufügen,
         this[Function.Priority].Add(Function);
     }
     else
     {
         //Nein - anlegen und hinzufügen.
         ADOperatorsOfSamePriority locOperatorsOfSamePriority = new ADOperatorsOfSamePriority();
         locOperatorsOfSamePriority.Add(Function);
         this.Add(locOperatorsOfSamePriority);
     }
 }
Exemple #2
0
        //Ü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);
        }