Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string  userInput;
            string  value;
            string  currentExpression = "a+b+c";
            double  result;
            bool    exit = false;
            ExpTree tree = new ExpTree(currentExpression);

            while (exit == false)
            {
                Console.WriteLine("Menu (Current Expression = " + currentExpression + ")");
                Console.WriteLine("1 - Enter a new expression");
                Console.WriteLine("2 - Set a variable value");
                Console.WriteLine("3 - Evaluate tree");
                Console.WriteLine("4 - Quit");
                userInput = Console.ReadLine();

                switch (userInput)
                {
                case "1":
                    Console.WriteLine("Write new expression with no spaces: ");
                    currentExpression = Console.ReadLine();
                    tree = new ExpTree(currentExpression);
                    Console.WriteLine("\r\n \r\n");

                    break;

                case "2":
                    Console.WriteLine("What variable do you want to set?");
                    userInput = Console.ReadLine();
                    Console.WriteLine("What value do you want to set: " + userInput + " as?");
                    value = Console.ReadLine();
                    tree.SetVar(userInput, Convert.ToDouble(value));
                    Console.WriteLine("\r\n \r\n");

                    break;

                case "3":
                    Console.WriteLine("Evaluating: " + currentExpression);
                    result = tree.Eval();
                    Console.WriteLine("Result = " + result.ToString());
                    Console.WriteLine("\r\n \r\n");

                    break;

                case "4":
                    Console.WriteLine("Exit");
                    exit = true;
                    break;

                default:
                    Console.WriteLine("Invalid input");
                    Console.WriteLine("Please input '1', '2', '3', or '4'");
                    Console.WriteLine("\r\n \r\n");

                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public void printMenu() //menu
        {
            bool   done = false, accepted = true;
            string temp;
            double value;

            while (done == false)
            {
                do
                {
                    Console.WriteLine("Choose an option:");
                    Console.WriteLine("1. Enter an expression");
                    Console.WriteLine("2. Set a variable value");
                    Console.WriteLine("3. Evaluate the expression to a numerical value");
                    Console.WriteLine("4. Quit");
                    option = Console.ReadLine(); //get the user's choice
                } while ((option != "1") && (option != "2") && (option != "3") && (option != "4"));

                if (option == "1")                        //enter an expression
                {
                    expTree.clearVar();                   //clear old variables before starting a new expression
                    Console.WriteLine("Enter a expression with at most one kind of operator:");
                    expression = Console.ReadLine();      //read in the user's expression
                    expTree    = new ExpTree(expression); //create a new expression tree with user entered expression
                }
                else if (option == "2")                   //set a variable value
                {
                    do
                    {
                        accepted = true;
                        Console.WriteLine("Enter a variable name:");
                        temp = Console.ReadLine(); //read in the variable name
                        if ((temp[0] < 'A') || (temp[0] > 'z'))
                        {
                            Console.WriteLine("The variable name must start with a letter");
                            accepted = false;
                        }
                    } while (accepted == false);
                    Console.WriteLine("Enter the variable value:");
                    value = Convert.ToDouble(Console.ReadLine()); //read in variable value as double
                    expTree.SetVar(temp, value);                  //call set var function to add the name and value to variable dictionary
                }
                else if (option == "3")                           //evaluate an expression
                {
                    if (expTree.getDictionarySize() > 0)          //there are variable values that need to be added to the tree
                    {
                        expTree.resetTree(expression);
                    }
                    value = expTree.Eval();              //call eval function
                    Console.WriteLine("The result of the evaluation was: ");
                    Console.WriteLine(value.ToString()); //print out result of eval function
                }
                else if (option == "4")                  //quit
                {
                    done = true;
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            ExpTree tree = new ExpTree("A+B+3-(E3-54)"); //default starting value of our Expression tree.

            int number = 0;

            tree.SetVar("A", 2); //setting some example values to the variables
            tree.SetVar("B", 5);
            do
            {
                Console.WriteLine("Menu: (current expression: {0})", tree.ToString()); //menu
                Console.WriteLine("\t1 = Enter a new expression");
                Console.WriteLine("\t2 = Set a variable value");
                Console.WriteLine("\t3 = Evaluate tree");
                Console.WriteLine("\t4 = Quit");
                bool isInt = int.TryParse(Console.ReadLine(), out number); //gets user input and stres it in number if it is an integer.
                if (isInt)
                {
                    switch (number)
                    {
                    case 1:
                        Console.Write("Enter new expression: ");
                        string userInput = Console.ReadLine();
                        tree = new ExpTree(userInput);     //makes a new tree with the new expression
                        break;

                    case 2:
                        Console.Write("Enter variable name: ");
                        string userInputVariable = Console.ReadLine();
                        Console.Write("Enter variable value: ");
                        string userInputValue = Console.ReadLine();
                        bool   isDouble       = double.TryParse(userInputValue, out double parsedDouble); //stores the user input as a double if it is a double
                        if (isDouble)                                                                     //if the value inputted was a double
                        {
                            tree.SetVar(userInputVariable, parsedDouble);                                 //set the variable to that double
                        }
                        break;

                    case 3:
                        Console.WriteLine(tree.Eval());
                        break;
                    }
                }
            } while (number != 4); //runs until 4 is typed in and then falls out to main.
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            string  input;
            string  variable;
            string  value;
            string  equation;
            double  result;
            bool    quit = false;
            ExpTree tree = new ExpTree("1+1");

            while (quit == false)
            {
                Console.WriteLine("Menu (Current Expression = " + tree.equation + ") \n");
                Console.WriteLine("\t 1 = Enter a new expression \n");
                Console.WriteLine("\t 2 = Set a variable value \n");
                Console.WriteLine("\t 3 = Evaluate Tree \n");
                Console.WriteLine("\t 4 = Quit");
                input = Console.ReadLine();

                switch (input)
                {
                case "1":
                    Console.WriteLine("Please enter new expression to be evauluated \r\n");
                    equation = Console.ReadLine();
                    tree     = new ExpTree(equation);
                    Console.WriteLine("\r\n \r\n");
                    break;

                case "2":
                    Console.WriteLine("Enter variable name: ");
                    variable = Console.ReadLine();

                    Console.WriteLine("Enter the new value for: " + variable);
                    value = Console.ReadLine();

                    tree.SetVar(variable, Convert.ToDouble(value));
                    break;

                case "3":
                    Console.WriteLine("Evaluating current expression \r\n");
                    result = tree.Eval();
                    Console.WriteLine("Answer = " + result.ToString() + "\r\n \r\n");
                    break;

                case "4":
                    Console.WriteLine("Quitting program");
                    quit = true;
                    break;

                default:
                    Console.WriteLine("Invalid input \r\n \r\n");
                    break;
                }
            }

            Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void menu()
        {
            bool end = false;

            while (end == false)
            {
                Console.WriteLine("Menu (Current Expression = \"" + exp + "\")");
                Console.WriteLine("1 = Enter a new expression");
                Console.WriteLine("2 = Set a variable value");
                Console.WriteLine("3 = Evaluate tree");
                Console.WriteLine("4 = Quit");
                string userInput = Console.ReadLine();


                if (int.TryParse(userInput, out var data))
                {
                    switch (data)
                    {
                    case 1:
                        Console.Write("Enter new expression: ");
                        expTree.Clear();
                        exp     = Console.ReadLine();
                        expTree = new ExpTree(exp);
                        break;

                    case 2:
                        Console.Write("Enter variable name: ");
                        string name  = Console.ReadLine();
                        bool   isInt = false;
                        while (!isInt)
                        {
                            Console.Write("Enter variable value: ");
                            string value = Console.ReadLine();
                            if (double.TryParse(value, out var v))
                            {
                                expTree.SetVar(name, v);
                                isInt = true;
                            }
                            else
                            {
                                Console.WriteLine("Invalid Input");
                            }
                        }
                        break;

                    case 3:
                        double result = expTree.Eval();
                        Console.WriteLine("{0}", result);
                        break;

                    case 4:
                        end = true;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void SetVarNoChangeTest()
        {
            string variable      = "A34";
            double assignedValue = 1326857.19;

            ExpTree testTree = new ExpTree(variable);

            testTree.SetVar(variable + '1', assignedValue);
            Assert.AreNotEqual(testTree.Eval(), assignedValue);
        }
Ejemplo n.º 7
0
        private void setVariable(ExpTree exp, string var)
        {
            Cell   c = GetCell(var);
            double value;

            if (var == "" || var == null) //cant be a variable if empty or null
            {
                exp.SetVar(c.name, 0);
            }

            //not a value
            else if (!double.TryParse(c.value, out value))
            {
                exp.SetVar(var, 0);
            }

            else
            {
                exp.SetVar(var, value);
            }
        }
Ejemplo n.º 8
0
        public void SetVarNodeNonRootTest()
        {
            // Arrange
            string testVariable = "hhhhhhhhhhh1";
            double testValue    = 1384.34;

            ExpTree testTree = new ExpTree(testVariable + "+3454-2348*34/0.5");

            testTree.SetVar(testVariable, testValue);

            // Assert
            Assert.AreEqual(-154825.66, testTree.Eval());
        }
Ejemplo n.º 9
0
        public void SetVarNodeRootTest()
        {
            // Arrange
            string testVariable = "hhhhhhhhhhh1";
            double testValue    = 1384.34;

            ExpTree testTree = new ExpTree(testVariable);

            testTree.SetVar(testVariable, testValue);

            // Assert
            Assert.AreEqual(testValue, testTree.Eval());
        }
Ejemplo n.º 10
0
        public void TestMultivariableExpression()
        {
            const string testVarName1   = "a1";
            const string testVarName2   = "Z50";
            const double testVarValue1  = 123.45d;
            const double testVarValue2  = double.MaxValue;
            const string testExpression = "a1-Z50";
            var          testTree       = new ExpTree(testExpression);

            testTree.SetVar(testVarName1, testVarValue1);
            testTree.SetVar(testVarName2, testVarValue2);
            var isValidExpression = testTree.IsValidExpression();
            var value             = testTree.Eval();

            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (testVarValue1 - testVarValue2));
            Console.WriteLine("TestMultiVariableExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: -1.79769313486232E+308");
            Console.WriteLine("  Actual value:  {0}", value);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            int     i = 0, flag = 0;
            string  expression = "A2+(10+B5)";
            ExpTree newExpTree = new ExpTree(expression);

            while (flag != 1)
            {
                i = 0;
                while ((i < 1) || (i > 4))
                {
                    Console.WriteLine("================== MENU ==================");
                    Console.WriteLine("Current Expression: " + expression);
                    Console.WriteLine(" 1 = Enter a new expression");
                    Console.WriteLine(" 2 = Set a variable value");
                    Console.WriteLine(" 3 = Evaluate tree");
                    Console.WriteLine(" 4 = Quit");
                    Console.WriteLine("==========================================");
                    string input = Console.ReadLine();
                    i = Int32.Parse(input);
                }
                if (i == 1) //create new exptree
                {
                    Console.WriteLine("Enter a new expression: ");
                    expression = Console.ReadLine();
                    ExpTree temp = new ExpTree(expression);
                    newExpTree = temp;
                }
                else if (i == 2) //set exptree variables
                {
                    Console.Write("Enter variable name: ");
                    string variable = Console.ReadLine();
                    Console.Write("Enter variable value: ");
                    double value = Int32.Parse(Console.ReadLine());
                    newExpTree.SetVar(variable, value);
                }
                else if (i == 3) //evaluate exptree
                {
                    Console.WriteLine(newExpTree.Eval());
                }
                else if (i == 4) //quit
                {
                    flag = 1;
                    Console.WriteLine("Done");
                }
            }
        }
Ejemplo n.º 12
0
        //=====================================================================
        // Menu2() -- get a variable name/value pair from user
        //=====================================================================
        private static void Menu2()
        {
            Console.Write("Enter a variable name: ");
            string name = Console.ReadLine();

            Console.Write("Enter a variable value: ");
            string valstr = Console.ReadLine();

            if (double.TryParse(valstr, out var val))
            {
                _expTree.SetVar(name, val);
            }
            else
            {
                Console.WriteLine("Could not interpret value");
            }
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            ExpTree et         = new ExpTree();
            string  expression = "";

            while (true)
            {
                Console.WriteLine("Menu (current expression=\"" + expression + "\")");
                Console.WriteLine("  1 = Enter a new expression");
                Console.WriteLine("  2 = Set a variable value");
                Console.WriteLine("  3 = Evaluate tree");
                Console.WriteLine("  4 = Quit");
                Console.Write("Enter a number: ");
                string input = Console.ReadLine();
                switch (input)
                {
                case "1":
                    Console.Write("Enter new expression: ");
                    expression = Console.ReadLine();
                    et         = new ExpTree(expression);
                    break;

                case "2":
                    Console.Write("Enter variable name: ");
                    string varName = Console.ReadLine();
                    Console.Write("Enter variable value: ");
                    string varStr   = Console.ReadLine();
                    double varValue = Convert.ToDouble(varStr);
                    et.SetVar(varName, varValue);
                    break;

                case "3":
                    Console.WriteLine("Answer = " + et.Eval());
                    break;

                case "4":
                    Console.WriteLine("Done");
                    System.Environment.Exit(0);
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            ExpTree et     = new ExpTree("A1-12-C1");
            int     choice = 0;

            while (choice != 4)
            {
                Console.WriteLine("Menu (current expression: {0})", et.expression);
                Console.WriteLine("\t1 = Enter a new expression");
                Console.WriteLine("\t2 = Set a variable value");
                Console.WriteLine("\t3 = Evaluate tree");
                Console.WriteLine("\t4 = Quit");
                var input = Console.ReadLine();

                switch (Convert.ToInt32(input))
                {
                case 1:
                    Console.Write("Enter new expression: ");
                    var temp = Console.ReadLine();
                    et = new ExpTree(temp);     //not good, but cant figure out another way as of now
                    break;

                case 2:
                    Console.Write("Enter variable name: ");
                    var old = Console.ReadLine();
                    Console.Write("Enter variable value: ");
                    var newv = Console.ReadLine();
                    et.SetVar(old, Convert.ToDouble(newv));
                    break;

                case 3:
                    Console.WriteLine("{0}", et.Eval());
                    break;

                case 4:
                    choice = 4;
                    break;

                default:
                    Console.WriteLine("// This is a command that the app will ignore");
                    break;
                }
            }
            Console.WriteLine("Done");
        }
Ejemplo n.º 15
0
        public void TestSingleVariableExpression()
        {
            const string testVarName    = "a1";
            const double testVarValue   = 123.45d;
            const string testExpression = testVarName;
            var          testTree       = new ExpTree(testExpression);

            testTree.SetVar(testVarName, testVarValue);
            var isValidExpression = testTree.IsValidExpression();
            var value             = testTree.Eval();

            Console.WriteLine("TestSingleVariableExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 123.45");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, testVarValue);
        }
Ejemplo n.º 16
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;
                }
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// UI program to test expression tree
        /// </summary>
        static void RunExpTreeTester()
        {
            string  new_expr = "5+5", menu_option = String.Empty;
            ExpTree tree   = new ExpTree(new_expr);
            int     option = 0;

            do
            {
                PrintMenu(new_expr);                     // print menu each time through the loop
                menu_option = Console.ReadLine();
                Int32.TryParse(menu_option, out option); // if this fails, we'll go to the default case in the switch statement
                switch ((MenuOptions)option)
                {
                case MenuOptions.NEW_EXPR:
                    Console.Write("Enter new expression: ");                 // Prompt for input.
                    new_expr = Console.ReadLine();                           // Grab user input (new expression).
                    try
                    {
                        tree = new ExpTree(new_expr); // Instantiate new expression tree
                    }
                    catch (Exception ex)              // error can be thrown if construction fails
                    {
                        Console.WriteLine(ex.Message);
                    }

                    break;

                case MenuOptions.SET_VAR_VAL:
                    string variable_name = String.Empty, variable_value = String.Empty;
                    Console.Write("Enter variable name: ");                     // Prompt for input.
                    variable_name = Console.ReadLine();                         // Grab variable name.
                    Console.Write("Enter variable value: ");                    // Prompt for input.
                    variable_value = Console.ReadLine();                        // Grab variable value as string.
                    bool success = Int32.TryParse(variable_value, out int val); // Parse value into integer.
                    if (success)
                    {
                        try
                        {
                            tree.SetVar(variable_name, val);      // Set variable within expression tree if successful parse.
                        }
                        catch (KeyNotFoundException ex)           // if variable not found, we need to handle the error gracefully
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid variable value.");
                    }
                    break;

                case MenuOptions.EVAL:
                    try
                    {
                        Console.WriteLine("Result: {0}", tree.Eval());
                    }
                    catch (NullReferenceException ex)          // if not all variables are set, then null reference exception thrown
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case MenuOptions.QUIT:
                    Console.WriteLine("Done.");
                    break;

                default:
                    Console.WriteLine("Not a valid menu option.");
                    break;
                }
            } while ((MenuOptions)option != MenuOptions.QUIT);
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            bool    endVar      = false;
            string  menuOptionS = "";
            string  expression  = "A1-12-C1";
            string  varName     = "";
            string  tempVal     = "";
            double  varVal      = 0.0;
            int     menuOptionV = 0;
            ExpTree newExpTree  = new ExpTree(expression);

            do //a do while loop tha continues going through the menu and different functions till quit
            {
                Console.WriteLine(">>>>>>>>>> Menu <<<<<<<<<<");
                Console.WriteLine("(Shunting Yard expression = \"" + newExpTree.shuntingYardAlgo(expression) + "\")");
                Console.WriteLine("(Current expression = \"" + expression + "\")");
                Console.WriteLine("\t 1 = Enter a new expression");
                Console.WriteLine("\t 2 = Set a variable value");
                Console.WriteLine("\t 3 = Evaluate tree");
                Console.WriteLine("\t 4 = Quit");
                menuOptionS = Console.ReadLine();
                if (!int.TryParse(menuOptionS, out menuOptionV))
                {
                    Console.WriteLine("Error: Only ints for menu selection\n");
                }
                else
                {
                    menuOptionV = Convert.ToInt32(menuOptionS);
                    switch (menuOptionV)
                    {
                    case 1:
                        Console.Write("Enter new expression: ");
                        expression = Console.ReadLine();
                        Console.WriteLine();
                        newExpTree = new ExpTree(expression);
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("New Expression Has Been Chosen");
                        Console.ResetColor();
                        break;

                    case 2:
                        Console.Write("Enter variable name: ");
                        varName = Console.ReadLine();
                        Console.WriteLine();
                        Console.Write("Enter variable value: ");
                        tempVal = Console.ReadLine();
                        varVal  = Convert.ToDouble(tempVal);
                        newExpTree.SetVar(varName, varVal);
                        Console.WriteLine();
                        break;

                    case 3:
                        Console.WriteLine(newExpTree.Eval());
                        break;

                    case 4:
                        endVar = true;
                        break;
                    }
                }
            } while (!endVar);
            Console.WriteLine("Done");
        }
Ejemplo n.º 19
0
        public void mainMenu()
        {
            bool   running = true;
            string userExp = "A1+B1+C1";
            string userVarName;
            string userVarStringValue;
            double userVarDoubleValue;

            // give the ExpTree a default value
            userExpTree = new ExpTree(userExp);

            while (running)
            {
                // prompt the user
                Console.WriteLine("Menu (current expression = \"" + userExp + "\")");
                Console.WriteLine("1. Enter a new expression");
                Console.WriteLine("2. Set a variable value");
                Console.WriteLine("3. Evaluate tree");
                Console.WriteLine("4. Quit");

                // read in their menu choice
                string userChoice = Console.ReadLine().ToString();

                // call the correct function based on the user's choice
                if (userChoice == "1")
                {
                    // prompt the user for an expression
                    Console.Write("Enter a new expression: ");
                    userExp = Console.ReadLine();

                    userExpTree = new ExpTree(userExp);
                }
                else if (userChoice == "2")
                {
                    // prompt the user for a variable name and value
                    Console.Write("Enter a variable name: ");
                    userVarName = Console.ReadLine();

                    Console.Write("Enter a variable value: ");
                    userVarStringValue = Console.ReadLine();
                    userVarDoubleValue = Convert.ToDouble(userVarStringValue);

                    userExpTree.SetVar(userVarName, userVarDoubleValue);
                }
                else if (userChoice == "3")
                {
                    Console.WriteLine(userExpTree.Eval());
                }
                else if (userChoice == "4")
                {
                    // stop running
                    running = false;
                }
                else
                {
                    continue;
                }
            }

            Console.WriteLine("Done");
        }