Ejemplo n.º 1
0
        /// <summary>
        /// Parse any string into an array of numbers with their optional unit markers.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="returnUnits"></param>
        /// <param name="validunits"></param>
        /// <param name="validSeparators"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static double[] ParseArrayOfNumbersWithUnits(string input, [Optional, DefaultParameterValue(null)] ref string[] returnUnits, string[] validunits = null, string validSeparators = ",x;:")
        {
            if (validunits is null)
            {
                validunits = new[] { "in", "mm", "'", "\"", "ft" };
            }

            var    vc = validSeparators.ToCharArray();
            int    i;
            int    c;
            var    sb     = new System.Text.StringBuilder();
            var    retVal = new List <double>();
            var    ru     = new List <string>();
            string s;
            var    exp = new MathExpressionParser();

            input = input.ToLower();
            c     = vc.Count();

            for (i = 1; i < c; i++)
            {
                input = input.Replace(vc[i], vc[0]);
            }

            var parse = TextTools.Split(input, vc[0].ToString());

            c = parse.Count();

            string su = null;

            for (i = 0; i < c; i++)
            {
                s = parse[i].Trim();

                s = StripUnit(s, ref su);

                ru.Add(su);

                string argErrorText = null;

                retVal.Add(exp.ParseExpression(s, ErrorText: ref argErrorText));
            }

            returnUnits = ru.ToArray();
            return(retVal.ToArray());
        }
Ejemplo n.º 2
0
        public bool ParseOperations(string value = null)
        {
            string[] st = null;
            int      i;
            int      c;
            int      n = 0;

            string sFar;

            MathExpressionParser objOp;

            if (value == null)
            {
                value = m_Expression;
            }

            value        = TextTools.OneSpace(TextTools.SpaceOperators(value.Trim()).ToLower());
            m_Expression = value;
            c            = 0;
            i            = value.IndexOf("(");
            if (i == -1)
            {
                i = value.IndexOf(")");
                if (i != -1)
                {
                    m_Error = "Unexpected closing parenthesis at column " + i;
                    return(false);
                }

                m_Value = ParseExpression(value, ref m_Error);
                if (m_Value == default)
                {
                    return(false);
                }
                return(true);
            }

            while (c < value.Length)
            {
                i = value.IndexOf("(", c);
                if (i == -1)
                {
                    Array.Resize(ref st, n + 1);
                    st[n] = "@" + value.Substring(c);
                    n    += 1;
                    break;
                }

                Array.Resize(ref st, n + 1);
                if (i - c > 0)
                {
                    st[n] = "@" + value.Substring(c, i - c);
                    n    += 1;
                }

                sFar = TextTools.Bracket(value, ref i, ref c);
                Array.Resize(ref st, n + 1);
                st[n] = sFar;
                n    += 1;
                i     = value.IndexOf("(", c);
            }

            sFar = "";
            m_Col.Clear();

            for (i = 0; i < n; i++)
            {
                ErrorText = "";
                if (st[i].Substring(0, 1) == "@")
                {
                    sFar += st[i].Substring(1);
                    objOp = new MathExpressionParser();
                    string argErrorText = null;
                    objOp.ParseExpression(sFar, ErrorText: ref argErrorText);
                    m_Col.Add(objOp);
                }
                else
                {
                    objOp = new MathExpressionParser();
                    objOp.ParseOperations(st[i]);
                    if (!string.IsNullOrEmpty(objOp.ErrorText))
                    {
                        ErrorText = objOp.ErrorText;
                        return(false);
                    }

                    m_Col.Add(objOp);
                    sFar += " " + objOp.Value;
                }
            }

            sFar = TextTools.OneSpace(TextTools.SpaceOperators(sFar.Trim()).ToLower());
            double localParseExpression()
            {
                string argErrorText = ErrorText; var ret = ParseExpression(sFar, ref argErrorText); ErrorText = argErrorText; return(ret);
            }

            m_Value = localParseExpression();
            if (!string.IsNullOrEmpty(ErrorText))
            {
                return(false);
            }
            return(true);
        }