Exemplo n.º 1
0
        private Expression GetExpression(String function)
        {
            var operatorFound = false;

            for (var i = function.Length - 1; i >= 0; i--)
            {
                operatorFound |= IsOperator(function[i]);

                if (ShouldEvaluateMultiplicativeExpression(function, operatorFound, i))
                {
                    var multiplicativeExpressionIndex = i;
                    while (InMultiplicativeExpression(function, multiplicativeExpressionIndex))
                        multiplicativeExpressionIndex--;

                    if (multiplicativeExpressionIndex > 0)
                    {
                        var multiplicativeExpression = GetExpression(function.Substring(multiplicativeExpressionIndex + 1));

                        var nonMultiplicativeFunction = function.Substring(0, multiplicativeExpressionIndex);
                        var left = GetExpression(nonMultiplicativeFunction);
                        var right = new Expression() { Value = multiplicativeExpression.Evaluate() };
                        var nonMultiplicativeOperator = function[multiplicativeExpressionIndex];
                        var nonMultiplicativeExpression = ExpressionFactory.Get(nonMultiplicativeOperator, left, right);

                        return nonMultiplicativeExpression;
                    }
                }

                if (CanBuildExpression(function, operatorFound, i))
                    return BuildExpression(function, i);
            }

            return new Expression() { Value = Convert.ToDouble(function) };
        }
Exemplo n.º 2
0
        private Expression BuildExpression(String function, Int32 i)
        {
            var number = function.Substring(i + 2);
            var left = GetExpression(function.Substring(0, i + 1));
            var right = new Expression() { Value = Convert.ToDouble(number) };

            return ExpressionFactory.Get(function[i + 1], left, right);
        }
 public static Expression Get(Char @operator, Expression left, Expression right)
 {
     switch (@operator)
     {
         case '/': return new DivisionExpression(left, right);
         case '*': return new MultiplicationExpression(left, right);
         case '-': return new SubtractionExpression(left, right);
         case '+':
         default: return new AdditionExpression(left, right);
     }
 }
 public AdditionExpression(Expression left, Expression right)
 {
     this.left = left;
     this.right = right;
 }
 public MultiplicationExpression(Expression left, Expression right)
 {
     this.left = left;
     this.right = right;
 }
 public SubtractionExpression(Expression left, Expression right)
 {
     this.left = left;
     this.right = right;
 }
 public DivisionExpression(Expression left, Expression right)
 {
     this.left = left;
     this.right = right;
 }