コード例 #1
0
        public void TestEvaluateTree()
        {
            string         expression1 = "A1+B1+12+12";
            ExpressionTree test1       = new ExpressionTree(expression1);

            test1.CreateExpressionTree(expression1);
            Assert.That(24, Is.EqualTo(test1.Evaluate()), "Failed at test1 (24)");
            test1.SetVariable("A1", 12);
            Assert.That(36, Is.EqualTo(test1.Evaluate()), "Failed at test1 (36)");
            test1.SetVariable("B1", 12);
            Assert.That(48, Is.EqualTo(test1.Evaluate()), "Failed at test1 (48)");
            string         expression2 = "World-7+8+8";
            ExpressionTree test2       = new ExpressionTree(expression2);

            test2.CreateExpressionTree(expression2);
            Assert.That(9, Is.EqualTo(test2.Evaluate()), "Failed at test2 (9)");
            test2.SetVariable("World", 12);
            Assert.That(21, Is.EqualTo(test2.Evaluate()), "Failed at test2 (21)");
        }
コード例 #2
0
        /// <summary>
        /// This will print out the main menu and ask the user to input an expression and evaluate the expression
        /// using the ExpressionTree class.
        /// </summary>
        public static void RunApplication()
        {
            int            option = 0, exit = -1;
            string         expression = "A1-12-C1";
            string         name       = string.Empty;
            double         value      = 0;
            ExpressionTree expTree    = new ExpressionTree(expression);

            while (exit == -1)
            {
                PrintMainMenu(expression);
                option = GetOptionForMainMenu();

                switch (option)
                {
                case 1:
                    System.Console.Write("Enter a new expression: ");
                    expression = System.Console.ReadLine();
                    break;

                case 2:
                    System.Console.Write("Enter variable name: ");
                    name = System.Console.ReadLine();
                    System.Console.Write("Enter variable value: ");
                    value = Convert.ToDouble(System.Console.ReadLine());
                    expTree.SetVariable(name, value);
                    break;

                case 3:
                    System.Console.Write("Evaluation of Tree: ");
                    expTree.CreateExpressionTree(expression);
                    System.Console.WriteLine(expTree.Evaluate());
                    break;

                case 4:
                    System.Console.Write("Quit.");
                    exit = 1;     // Leaves loop and ends application
                    break;
                }
            }
        }