示例#1
0
            /// <summary>
            ///
            /// Cast the splitted string into a list of integers and operators.
            /// </summary>
            protected bool castElements()
            {
                IList <Object> expression = new List <Object>();

                foreach (String str in splitedexpression)
                {
                    if (str.Length == 0)
                    {
                        continue;                  // Skip empty string.
                    }
                    bool isinteger  = Regex.IsMatch(str, @"\d+") && str.Length < 13;
                    bool OptOrParen = str.Length == 1
                                      &&
                                      (
                        OPERATORSlist.Contains((char)str.ElementAt(0)) ||
                        (char)str.ElementAt(0) == '(' || (char)str.ElementAt(0) == ')'
                                      );
                    if (isinteger)
                    {
                        expression.Add(Int64.Parse(str));
                    }
                    else if (OptOrParen)
                    {
                        expression.Add(str.ElementAt(0));
                    }
                    else
                    {
                        return(false);  // something is wrong is this line is executed.
                    }
                }
                this.castplistedexpression = expression;
                return(true);
            }
示例#2
0
            protected bool checkNumberAndOperator()
            {
                Int32 sum = 1;

                foreach (char c in this.sourceexpression)
                {
                    if (c <= '9' || c >= '0')
                    {
                        sum++;                      //add one if this is a number.
                    }
                    else if (OPERATORSlist.Contains(c))
                    {
                        sum--;                                //else minus one if this is a operator.
                    }
                    else
                    {
                        sum = 1; //else this must be parenthesis, it will clear the sum to 1.
                    }
                    if (sum < 0)
                    {
                        return(false);         //if it's smaller than zero, there is something wrong with the expression.
                    }
                }
                return(true);
            }
示例#3
0
 /// <summary>
 /// <para>
 /// Only integers, parenthesis and elementary operators are allowed.
 /// </para>
 /// <para>
 /// The prenthesis must be matched.
 /// </para>
 /// </summary>
 /// <returns></returns>
 protected bool checkChar()
 {
     foreach (char c in sourceexpression)
     {
         if (c < '0' || c > '9')             //not a number
         {
             if (!OPERATORSlist.Contains(c)) //not a operator.
             {
                 return(c == ')' || c == '(');
             }
         }
     }
     return(true);
 }
示例#4
0
            /// <summary>
            ///
            /// This method modify the current expression by adding space to left side and right side of the
            /// operators or parenthesis making it easier to parse using split.
            /// </summary>
            protected void addSpace()
            {
                StringBuilder sb = new StringBuilder();

                foreach (char c in sourceexpression)
                {
                    if (!OPERATORSlist.Contains(c) && c != ')' && c != '(')//if it's not an operator or parenthesis
                    {
                        sb.Append(c);
                    }
                    else//It is a operator or prenthesis or something unexpected.
                    {
                        sb.Append(' ');
                        sb.Append(c);
                        sb.Append(' ');
                    }
                }
                this.diagestedexpression = sb.ToString();
            }