Exemple #1
0
        public decimal Evaluate(BasicMathExpression expression)
        {
            Clear();

            //left
            if (expression.Left.StartsWith("-") || expression.Left.StartsWith("m"))
            {
                EnterNumber(expression.Left.Substring(1));
                Negate();
            }
            else
            {
                EnterNumber(expression.Left);
            }

            //operation
            EnterOperation(expression.Operation);

            //reght
            if (expression.Right.StartsWith("-") || expression.Right.StartsWith("m"))
            {
                EnterNumber(expression.Right.Substring(1));
                Negate();
            }
            else
            {
                EnterNumber(expression.Right);
            }

            RunEvaluate();

            var res = GetResultingValue();

            return(res);
        }
Exemple #2
0
        public void ExpressionParseBasic(string exprString, decimal left, decimal right, string operationSign)
        {
            var expr = new BasicMathExpression(exprString);

            expr.OriginalExpression.Should().Be(exprString, $"expression should be {exprString}");
            expr.Left.Should().Be(left.ToString(CultureInfo.InvariantCulture), $"left should be {left}");
            expr.LeftValue.Should().Be(left, $"left value should be {left}");
            expr.Right.Should().Be(right.ToString(CultureInfo.InvariantCulture), $"right should be {right}");
            expr.RightValue.Should().Be(right, $"right value should be {right}");
            expr.Operation.Sign.Should().Be(operationSign, $"operation should be {operationSign}");
        }
        public decimal Evaluate(BasicMathExpression expression)
        {
            if (expression.Operation.Equals(SupportedOperations.Plus))
            {
                return(expression.LeftValue + expression.RightValue);
            }

            if (expression.Operation.Equals(SupportedOperations.Minus))
            {
                return(expression.LeftValue - expression.RightValue);
            }

            if (expression.Operation.Equals(SupportedOperations.Multiply))
            {
                return(expression.LeftValue * expression.RightValue);
            }

            if (expression.Operation.Equals(SupportedOperations.Divide))
            {
                return(expression.LeftValue / expression.RightValue);
            }

            throw  new NotSupportedException($"The operation '{expression.Operation.Sign}' is not supported");
        }
        private string EvaluateBasicExpressionAndReplace(string expression, Match m)
        {
            var simpleExpression = new BasicMathExpression(m.Groups[0].ToString());

            return($"{expression.Substring(0, m.Groups[0].Index)}{_expressionEvaluator.Evaluate(simpleExpression)}{expression.Substring(m.Groups[0].Index + m.Groups[0].Length)}");
        }