Esempio n. 1
0
        public void ConsiderTheSum()
        {
            var sumSixTwo        = new Binary(new Literal(6), new Token(TokenType.Plus, "+", null), new Literal(2));
            var productSumSixTwo = new Binary(new Literal(3), new Token(TokenType.Star, "*", null), sumSixTwo);
            var diffProduct      = new Binary(productSumSixTwo, new Token(TokenType.Minus, "-", null), new Literal(4));
            var leftGroup        = new Grouping(diffProduct);
            var sumThreeSeven    = new Binary(new Literal(3), new Token(TokenType.Plus, "+", null), new Literal(7));
            var final            = new Binary(leftGroup, new Token(TokenType.Slash, "/", null), sumThreeSeven);

            var result = new RpnPrinter().Print(final);

            Assert.AreEqual(result, "3 6 2 + * 4 - 3 7 + /");
            Console.WriteLine(result);
        }
Esempio n. 2
0
        public void TransformToRPNNotation()
        {
            var expr = new Binary(
                new Grouping(
                    new Binary(
                        new Literal("1"),
                        new Token(TokenType.PLUS, "+", null, 1),
                        new Literal("2")
                        )
                    ),
                new Token(TokenType.STAR, "*", null, 1),
                new Grouping(
                    new Binary(
                        new Literal("4"),
                        new Token(TokenType.MINUS, "-", null, 1),
                        new Literal("3")
                        )
                    )
                );

            var sut = new RpnPrinter();

            Assert.Equal("1 2 + 4 3 - *", sut.Print(expr));
        }
Esempio n. 3
0
        public void PrinterTest()
        {
            var expression = new Binary
                             (
                new Grouping(
                    new Binary(
                        new Literal(1),
                        new Token(TokenType.Plus, "+", null),
                        new Literal(2))
                    ),
                new Token(TokenType.Star, "*", null),
                new Grouping(
                    new Binary(
                        new Literal(4),
                        new Token(TokenType.Minus, "-", null),
                        new Literal(3)
                        )
                    )
                             );
            var result = new RpnPrinter().Print(expression);

            Assert.AreEqual(result, "1 2 + 4 3 - *");
            Console.WriteLine(expression);
        }