예제 #1
0
        public void displayMenu()
        {
            while (true)
            {
                Console.WriteLine("Menu");
                Console.WriteLine("-----------");
                Console.WriteLine("1: Enter a new expression");
                Console.WriteLine("2: Set a variable value");
                Console.WriteLine("3: Evaluate expression");
                Console.WriteLine("4: Quit");
                Console.WriteLine("-----------");
                this.menuSelection = Int32.Parse(Console.ReadLine());
                switch (this.menuSelection)
                {
                case 1:                                             //Enter a new expression
                    expTree.ClearVariables();                       //clear all variables for new expression
                    this.expression = getExpression();              //gets an expression from the user to be evaluated
                    expTree         = new ExpTree(this.expression); //builds an expression tree with the user entered expression
                    break;

                case 2:                                          //Set a variable value
                    setVariable();
                    expTree.SetVar(this.varName, this.varValue); //sets the variable in the dictionary of variables for the tree
                    break;

                case 3:                      //Evaluate an expression
                    double result = 0;
                    result = expTree.Eval(); //returns the evaluated expression result
                    Console.WriteLine("Evaluated expression is: {0}", result);
                    break;

                case 4:
                    Environment.Exit(0);
                    break;
                }
            }
        }