示例#1
0
        private static void SecondExpressionTree()
        {
            Console.WriteLine("SECOND EXPRESSION TREE: \n  \n");
            Console.WriteLine("Type into the console the following expression '18 – 26 / 10 + 50 * 4' :\n");
            string input = Console.ReadLine();

            Console.WriteLine("\nTurning expression to an expression tree...\n",
                              input);

            ExpressionParser parser          = new ExpressionParser();
            ExpressionTree   expression_tree = parser.Parse(input);

            string traverseError = "EXPRESSION TREE: Order doesn't match!";
            string resultError   = "EXPRESSION TREE: Result is not correct!";
            string preExpected   = "+, -, 18, /, 26, 10, *, 50, 4";
            string inExpected    = "18, -, 26, /, 10, +, 50, *, 4";
            string postExpected  = "18, 26, 10, /, -, 50, 4, *, +";

            string preResult = expression_tree.TraversePre();

            TestTraversals(preResult, preExpected, traverseError);
            Console.WriteLine("Pre-order Traversal: [{0}]\n", preResult);


            string inResult = expression_tree.TraverseIn();

            TestTraversals(inResult, inExpected, traverseError);
            Console.WriteLine("In-order Traversal: [{0}]\n", inResult);

            string postResult = expression_tree.TraversePost();

            TestTraversals(postResult, postExpected, traverseError);
            Console.WriteLine("Post-order Traversal: [{0}]\n", postResult);

            Console.WriteLine("Evaluating {0}...\n", input);
            expression_tree.Evaluate(expression_tree.Root);
            TestResult(double.Parse(expression_tree.Root.Data), 215.4, resultError);
            Console.WriteLine("Result = {0}\n", expression_tree.Root.Data);

            Console.WriteLine("-------------------------------------------------------\n");
        }
示例#2
0
        private static void ThirdExpressionTree()
        {
            Console.WriteLine("THIRD EXPRESSION TREE: \n  \n");
            Console.WriteLine("Type into the console the following expression '22 ^ 2 + 263 * 2 - 649 / 100' :\n");
            string input = Console.ReadLine();

            Console.WriteLine("\nTurning expression to an expression tree...\n",
                              input);

            ExpressionParser parser          = new ExpressionParser();
            ExpressionTree   expression_tree = parser.Parse(input);

            string traverseError = "EXPRESSION TREE: Order doesn't match!";
            string resultError   = "EXPRESSION TREE: Result is not correct!";
            string preExpected   = "-, +, ^, 22, 2, *, 263, 2, /, 649, 100";
            string inExpected    = "22, ^, 2, +, 263, *, 2, -, 649, /, 100";
            string postExpected  = "22, 2, ^, 263, 2, *, +, 649, 100, /, -";

            string preResult = expression_tree.TraversePre();

            TestTraversals(preResult, preExpected, traverseError);
            Console.WriteLine("Pre-order Traversal: [{0}]\n", preResult);


            string inResult = expression_tree.TraverseIn();

            TestTraversals(inResult, inExpected, traverseError);
            Console.WriteLine("In-order Traversal: [{0}]\n", inResult);

            string postResult = expression_tree.TraversePost();

            TestTraversals(postResult, postExpected, traverseError);
            Console.WriteLine("Post-order Traversal: [{0}]\n", postResult);

            Console.WriteLine("Evaluating {0}...\n", input);
            expression_tree.Evaluate(expression_tree.Root);
            TestResult(double.Parse(expression_tree.Root.Data), 1003.51, resultError);
            Console.WriteLine("Result = {0}\n", expression_tree.Root.Data);

            Console.WriteLine("-------------------------------------------------------\n");
        }
示例#3
0
        private static void FirstExpressionTree()
        {
            Console.WriteLine("FIRST EXPRESSION TREE: \n  \n");
            Console.WriteLine("Type into the console the following expression '5 + 2 * 8 – 6 / 4' :\n");
            string input = Console.ReadLine();

            Console.WriteLine("\nTurning expression to an expression tree...\n",
                              input);

            ExpressionParser parser          = new ExpressionParser();
            ExpressionTree   expression_tree = parser.Parse(input);

            string traverseError = "EXPRESSION TREE: Order doesn't match!";
            string resultError   = "EXPRESSION TREE: Result is not correct!";
            string preExpected   = "-, +, 5, *, 2, 8, /, 6, 4";
            string inExpected    = "5, +, 2, *, 8, -, 6, /, 4";
            string postExpected  = "5, 2, 8, *, +, 6, 4, /, -";

            string preResult = expression_tree.TraversePre();

            TestTraversals(preResult, preExpected, traverseError);
            Console.WriteLine("Pre-order Traversal: [{0}]\n", preResult);


            string inResult = expression_tree.TraverseIn();

            TestTraversals(inResult, inExpected, traverseError);
            Console.WriteLine("In-order Traversal: [{0}]\n", inResult);

            string postResult = expression_tree.TraversePost();

            TestTraversals(postResult, postExpected, traverseError);
            Console.WriteLine("Post-order Traversal: [{0}]\n", postResult);

            Console.WriteLine("Evaluating {0}...\n", input);
            expression_tree.Evaluate(expression_tree.Root);
            TestResult(double.Parse(expression_tree.Root.Data), 19.5, resultError);
            Console.WriteLine("Result = {0}\n", expression_tree.Root.Data);

            Console.WriteLine("-------------------------------------------------------\n");
        }