Пример #1
0
        public void EvaluatesCorrectly(string expressionString, float expectedResult)
        {
            // arrange
            var expressionNodeFactory   = new ExpressionNodeFactory();
            var expressionTokenizer     = new ExpressionTokenizer();
            var infixToPostfixConverter = new InfixToPostfixConverter();
            var expressionFactory       = new ExpressionFactory(expressionNodeFactory, expressionTokenizer, infixToPostfixConverter);

            // act
            var expression = expressionFactory.MakeExpression(expressionString);
            var result     = expression.Evaluate();

            // assert
            Assert.Equal(expectedResult, result);
        }
Пример #2
0
        public static float Evaluate(string str, Dictionary <string, float> variables)
        {
            var expressionNodeFactory   = new ExpressionNodeFactory();
            var expressionTokenizer     = new ExpressionTokenizer();
            var infixToPostfixConverter = new InfixToPostfixConverter();
            var expressionFactory       = new ExpressionFactory(expressionNodeFactory, expressionTokenizer, infixToPostfixConverter);
            var expression = expressionFactory.MakeExpression(str);

            foreach (var(symbol, value) in variables)
            {
                expression.EvaluationContext.GetVariable(symbol).Value = value;
            }

            return(expression.Evaluate());
        }
Пример #3
0
        public void EvaluatesCorrectlyWithAVariableAndSomeStuff()
        {
            // arrange
            var expressionNodeFactory   = new ExpressionNodeFactory();
            var expressionTokenizer     = new ExpressionTokenizer();
            var infixToPostfixConverter = new InfixToPostfixConverter();
            var expressionFactory       = new ExpressionFactory(expressionNodeFactory, expressionTokenizer, infixToPostfixConverter);

            // act
            var expression = expressionFactory.MakeExpression("(bananas + 1) / 5");

            expression.EvaluationContext.GetVariable("bananas").Value = 4.0f;
            var result = expression.Evaluate();

            // assert
            Assert.Equal(1.0f, result);
        }