예제 #1
0
        public void NoMatchingRParenProducesError()
        {
            const string    expression = "1+(2+3";
            Result <string> result     = ShuntingYard.InfixToPostfixStr(expression);

            Assert.IsFalse(result.HasValue);
            Assert.True(result.ErrorMessage.StartsWith("No matching right parenthesis"));
        }
예제 #2
0
        public void CanProducePostfixNotation()
        {
            const string    expression = "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3";
            Result <string> result     = ShuntingYard.InfixToPostfixStr(expression);

            if (!result.HasValue)
            {
                Debug.LogError("ShuntingYard.InfixToPostfix(" + expression + ") Error: " + result.ErrorMessage);
            }
            Assert.True(result.HasValue);
            Assert.AreEqual("3 4 2 * 1 5 - 2 3 ^ ^ / +", result.Value);
        }
예제 #3
0
        public void CanProducePostfixNotationWithFunction()
        {
            const string    expression = "sin(max(2, 3) / 3 * 3.1415)";
            Result <string> result     = ShuntingYard.InfixToPostfixStr(expression);

            if (!result.HasValue)
            {
                Debug.LogError("ShuntingYard.InfixToPostfix(" + expression + ") Error: " + result.ErrorMessage);
            }
            Assert.True(result.HasValue);
            Assert.AreEqual("2 3 max 3 / 3.1415 * sin", result.Value);
        }
예제 #4
0
        /// <summary>
        /// Called from button wired through the Unity inspector
        /// </summary>
        public void Evaluate()
        {
            Result <string> shuntingYardResult = ShuntingYard.InfixToPostfixStr(InputField.text);

            RpnField.text = shuntingYardResult.HasValue
                ? shuntingYardResult.Value
                : "<error>";

            Result <Expression <Func <double> > > rpnExpTreeResult =
                rpnEvaluator.TryEvaluateToExpressionTree(InputField.text);

            RpnExpressionTreeField.text = rpnExpTreeResult.HasValue
                ? rpnExpTreeResult.Value.ToString()
                : "<error>";

            Result <Expression <Func <double> > > combinatorExpTreeResult =
                combinatorEvaluator.TryEvaluateToExpressionTree(InputField.text);

            CombinatorExpressionTreeField.text = combinatorExpTreeResult.HasValue
                ? combinatorExpTreeResult.Value.ToString()
                : "<error>";


            Result <double> rpnResult = rpnEvaluator.TryEvaluate(InputField.text);

            if (!rpnResult.HasValue)
            {
                RpnErrorsField.text = rpnResult.ErrorMessage;
                RpnResultField.text = "<error>";
            }
            else
            {
                RpnErrorsField.text = string.Empty;
                RpnResultField.text = rpnResult.Value.ToString(CultureInfo.InvariantCulture);
            }

            Result <double> combinatorResult = combinatorEvaluator.TryEvaluate(InputField.text);

            if (!combinatorResult.HasValue)
            {
                CombinatorErrorsField.text = combinatorResult.ErrorMessage;
                CombinatorResultField.text = "<error>";
            }
            else
            {
                CombinatorErrorsField.text = string.Empty;
                CombinatorResultField.text = combinatorResult.Value.ToString(CultureInfo.InvariantCulture);
            }

            InputField.ActivateInputField();
        }