Beispiel #1
0
 // Standard evaluation for single value.
 private static bool evaluateSingle(Bracket b)
 {
     if (b == null)
     {
         return(false);
     }
     if (!b.Valid)
     {
         return(false);
     }
     if (b.contains(LIST_SEPARATOR.ToString()))
     {
         return(false);
     }
     return(true);
 }
Beispiel #2
0
 public override object calculate(Bracket b)
 {
     if (evaluate(b))
     {
         if (b.contains(LIST_SEPARATOR.ToString().Trim()))
         {
             double a       = convertToNumber(b.Childs[0]);
             double newBase = convertToNumber(b.Childs[2]);
             return(Math.Log(a, newBase));
         }
         else
         {
             double a = convertToNumber(b.Childs[0]);;
             return(Math.Log(a));
         }
     }
     return(null);
 }
Beispiel #3
0
 public override bool evaluate(Bracket b)
 {
     if (evaluateSingle(b))
     {
         if (b.contains(UNLIMITED_VALUE))
         {
             return(false);
         }
         return(true);
     }
     else
     {
         if (b.contains(LIST_SEPARATOR.ToString()))
         {
             if (b.Childs.Length == 3)
             {
                 return(b.contains(UNLIMITED_VALUE.ToString().Trim()));
             }
             return(false);
         }
     }
     return(false);
 }
Beispiel #4
0
            /// <summary>
            /// Parses a string that conatins the formula of a bracket.
            /// </summary>
            /// <param name="value">A string value represent the formula of the bracket.</param>
            /// <param name="start">Starting index where the formula will be parsed.</param>
            /// <returns>Returns the zero-based index where the bracket is closed within the formula.</returns>
            public int parseString(string value, int start)
            {
                int i = start;

                _openIndex = start - 1;
                bool quit = false;

                _childs.Clear();
                _valid = true;
                string strNode = "";

                while (!quit)
                {
                    if (value[i] == '(')
                    {
                        Bracket child = new Bracket(this);
                        i = child.parseString(value, i + 1);
                        if (strNode != "")
                        {
                            if (OPERATORS.Contains(strNode.Trim()))
                            {
                                // The last node was an operator.
                                _childs.Add(strNode.Trim());
                            }
                            else if (strNode == "~")
                            {
                                // The last node was the unlimited number sign.
                                _childs.Add(strNode.Trim());
                            }
                            else if (isNumber(strNode))
                            {
                                // The last node was a number.
                                _childs.Add(strNode.Trim());
                            }
                            else
                            {
                                if (strNode[strNode.Length - 1] == '%')
                                {
                                    // Check if the last node was a percentage number.
                                    if (!isNumber(strNode.Substring(0, strNode.Length - 1)))
                                    {
                                        _valid = false;
                                    }
                                    _childs.Add(strNode);
                                }
                                else
                                {
                                    // Check if the last node was a code of registered function's code.
                                    // Build up functions.
                                    bool found = false;
                                    foreach (CalcFunction fx in _builtin_functions)
                                    {
                                        if (fx.evaluateCode(strNode))
                                        {
                                            child.Function = fx;
                                            found          = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        // User defined functions.
                                        foreach (CalcFunction fx in CalcFunctions.UserFunctions)
                                        {
                                            if (fx.evaluateCode(strNode))
                                            {
                                                child.Function = fx;
                                                found          = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (!found)
                                    {
                                        // No functions were found.
                                        _valid = false;
                                        _childs.Add(strNode);
                                    }
                                }
                            }
                        }
                        _childs.Add(child);
                        strNode = "";
                    }
                    else
                    {
                        if (value[i] == ')')
                        {
                            quit        = true;
                            _closeIndex = i;
                        }
                        else
                        {
                            if (OPERATORS.Contains(value[i].ToString().Trim()))
                            {
                                if (_childs.Count == 0)
                                {
                                    if (value[i] != '-' && strNode == "")
                                    {
                                        _valid = false;
                                    }
                                }
                                else
                                {
                                    if (strNode == LIST_SEPARATOR.ToString().Trim() && value[i] != '-')
                                    {
                                        _valid = false;
                                    }
                                }
                                if (strNode != "")
                                {
                                    _childs.Add(strNode);
                                }
                                strNode = value[i].ToString().Trim();
                            }
                            else
                            {
                                if (strNode == "~")
                                {
                                    _childs.Add(strNode);
                                    strNode = "";
                                }
                                if (value[i] == '.' || (value[i] >= 48 && value[i] <= 57) ||
                                    (value[i] >= 65 && value[i] <= 90) || (value[i] >= 97 && value[i] <= 122) ||
                                    value[i] == '_' || value[i] == '%')
                                {
                                    if (OPERATORS.Contains(strNode) && strNode != "")
                                    {
                                        if (_childs.Count > 0)
                                        {
                                            _childs.Add(strNode);
                                            strNode = "";
                                        }
                                    }
                                    strNode += value[i];
                                }
                                else if (value[i] == LIST_SEPARATOR)
                                {
                                    if (strNode != "")
                                    {
                                        _childs.Add(strNode);
                                    }
                                    else if (_childs.Count == 0)
                                    {
                                        _valid = false;
                                    }
                                    strNode = LIST_SEPARATOR.ToString().Trim();
                                }
                                else if (value[i] == ' ')
                                {
                                    if (strNode != "")
                                    {
                                        _childs.Add(strNode);
                                    }
                                    strNode = "";
                                }
                                else if (value[i] == UNLIMITED_VALUE)
                                {
                                    if (strNode != "")
                                    {
                                        _childs.Add(strNode);
                                    }
                                    strNode = "~";
                                }
                            }
                        }
                    }
                    i++;
                    if (i >= value.Length)
                    {
                        quit = true;
                    }
                }
                if (strNode != "")
                {
                    _childs.Add(strNode);
                }
                return(i);
            }