示例#1
0
        public void Tree_Test()
        {
            var initialEquation = "3+2*4/5";
            var checkPostfix    = ShuntingYardAlgorithm.PostfixString(initialEquation);

            checkPostfix.Should().Be("324*5/+");
            var arithmeticTree = ExpressionTree.BuildArithmeticTree(checkPostfix);

            arithmeticTree.Data.Should().Be('+');
        }
示例#2
0
        public void PostfixEvaluatorTest()
        {
            // 3 + 4 * 5 = 23
            var queue = ShuntingYardAlgorithm.ShuntingYard("3+4*5");

            PostfixEvaluator.EvaluatePostfix(queue).Should().Be(23);

            // 3 * (4 + 5) = 27
            queue = ShuntingYardAlgorithm.ShuntingYard("3*(4+5)");
            PostfixEvaluator.EvaluatePostfix(queue).Should().Be(27);
        }
示例#3
0
        public void InfixToPostfixTest()
        {
            var queue = ShuntingYardAlgorithm.ShuntingYard("3+2*4/5");

            queue.Dequeue().Should().Be('3');
            queue.Dequeue().Should().Be('2');
            queue.Dequeue().Should().Be('4');
            queue.Dequeue().Should().Be('*');
            queue.Dequeue().Should().Be('5');
            queue.Dequeue().Should().Be('/');
            queue.Dequeue().Should().Be('+');

            queue = ShuntingYardAlgorithm.ShuntingYard("3+4*5");
            queue.Dequeue().Should().Be('3');
            queue.Dequeue().Should().Be('4');
            queue.Dequeue().Should().Be('5');
            queue.Dequeue().Should().Be('*');
            queue.Dequeue().Should().Be('+');

            queue = ShuntingYardAlgorithm.ShuntingYard("3*(4+5)");
            queue.Dequeue().Should().Be('3');
            queue.Dequeue().Should().Be('4');
            queue.Dequeue().Should().Be('5');
            queue.Dequeue().Should().Be('+');
            queue.Dequeue().Should().Be('*');

            queue = ShuntingYardAlgorithm.ShuntingYard("3+4*2/(1-5)^2^3");
            // 3 4 2 × 1 5 − 2 3 ^ ^ ÷ +
            queue.Dequeue().Should().Be('3');
            queue.Dequeue().Should().Be('4');
            queue.Dequeue().Should().Be('2');
            queue.Dequeue().Should().Be('*');
            queue.Dequeue().Should().Be('1');
            queue.Dequeue().Should().Be('5');
            queue.Dequeue().Should().Be('-');
            queue.Dequeue().Should().Be('2');
            queue.Dequeue().Should().Be('3');
            queue.Dequeue().Should().Be('^');
            queue.Dequeue().Should().Be('^');
            queue.Dequeue().Should().Be('/');
            queue.Dequeue().Should().Be('+');

            queue = ShuntingYardAlgorithm.ShuntingYard("3-2+1");
            queue.Dequeue().Should().Be('3');
            queue.Dequeue().Should().Be('2');
            queue.Dequeue().Should().Be('-');
            queue.Dequeue().Should().Be('1');
            queue.Dequeue().Should().Be('+');
        }
示例#4
0
        static void Main(string[] args)
        {
            while (true)
            {
                string input = PromptGetResponse(Messages.Prompt_EnterExpression);
                if (input.ToLower() == QuitKeyword.ToLower())
                {
                    break;
                }

                string postfix;
                int    result;

                try
                {
                    postfix = ShuntingYardAlgorithm.Convert(input);
                }
                catch (Exception)
                {
                    DisplayMessage(Messages.ErrorMessage_PostfixParseFailed, isError: true);
                    continue;
                }

                try
                {
                    result = ReversePolishNotation.Evaluate(postfix);
                }
                catch (Exception ex)
                {
                    // special case in that it's very clear what the problem is
                    if (ex is DivideByZeroException)
                    {
                        DisplayMessage(Messages.ErrorMessage_EvaluationGeneratesDivideByZero, isError: true);
                    }
                    else
                    {
                        DisplayMessage(Messages.ErrorMessage_EvaluationFailed, isError: true);
                    }

                    continue;
                }

                DisplayMessage($"{Messages.Message_Result} {result}");
            }
        }
示例#5
0
        private static void Main()
        {
            const string infix = "3+2*4/5";

            Console.WriteLine($"Initial infix: {infix}");
            // 324*5/+
            var postfix = ShuntingYardAlgorithm.PostfixString(infix);

            Console.WriteLine($"Postfix by Shunting-yard: {postfix}");
            var arithmeticTree = ExpressionTree.BuildArithmeticTree(postfix);

            Console.WriteLine("Infix by In-Order traversal: ");
            DisplayTree.InOrderTraversal(arithmeticTree);
            Console.WriteLine();
            Console.WriteLine("Postfix notation by Post-Order traversal: ");
            DisplayTree.PostOrderTraversal(arithmeticTree);
            Console.WriteLine();
            Console.WriteLine("Prefix notation by Pre-Order traversal: ");
            DisplayTree.PreOrderTraversal(arithmeticTree);
            Console.WriteLine();
            Console.WriteLine("Infix: ");
            ExpressionTree.PrintInfixExpression(arithmeticTree);
        }
 public void WhenConvert_InvalidExpression_ThrowsException(string input)
 {
     Assert.ThrowsException <FormatException>(() => ShuntingYardAlgorithm.Convert(input));
 }
        public void WhenConvert_ValidExpression_Returns_CorrectResult(string input, string expectedOutput)
        {
            var output = ShuntingYardAlgorithm.Convert(input);

            Assert.AreEqual(expectedOutput, output);
        }
示例#8
0
        private void Given(string expression)
        {
            var test = new ShuntingYardAlgorithm();

            _result = test.Transform(expression);
        }