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
        //evaluate cell accepts a cell as an argument and checks to see if it is an expression
        // or just a string. if it begins with '=' it is an expression and will be evaluated as such.
        //otherwise the text will just be reassigned.
        private void EvaluateCell(Cell cell)
        {
            ss_cell c = cell as ss_cell;

            //cell represents an expression
            if (c.text[0] == '=')
            {
                //chop off  '='
                string   expression = c.text.Substring(1);
                string[] ABC        = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

                //creates an expression tree based off of the expression
                ExpTree exp = new ExpTree(expression);

                //get a list of the variables
                string[] vars = exp.getVars();

                foreach (string variableName in vars)
                {
                    if (GetCell(variableName) == null) //does not reference an actual cell
                    {
                        break;
                    }
                    setVariable(exp, variableName);
                }
                c.SetValue(exp.Eval().ToString());
                CellPropertyChanged(cell, new PropertyChangedEventArgs("value"));



                //trigger cellpropertychanged
                CellPropertyChanged(cell, new PropertyChangedEventArgs("text"));
            }
            //not an expression
            else
            {
                c.SetValue(c.text);
                CellPropertyChanged(cell, new PropertyChangedEventArgs("value"));
            }

            if (DependencyDict.ContainsKey(c.name))
            {
                foreach (string dependentCell in DependencyDict[c.name])
                {
                    EvaluateCell(GetCell(dependentCell));
                }
            }
            //else
            //{
            //    //just set the value to the text of the node passed in
            //    c.SetValue(c.text);

            //    //event trigger.
            //    CellPropertyChanged(cell, new PropertyChangedEventArgs("value"));

            //}
        }
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 EvalMultiplyTest()
        {
            double Value1 = 1326857.19;
            double Value2 = 38497.509;

            ExpTree testTree = new ExpTree(Value1.ToString() + '*' + Value2.ToString());

            Assert.AreEqual(testTree.Eval(), Value1 * Value2);
        }
Ejemplo n.º 7
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.º 8
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.º 9
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.º 10
0
        public void EvalMultipleOpNodes_WithParenthesisTest()
        {
            double[] Values = { 1326857.19, 38497.509, 89314871.9874, 879.4, 484, 3, 4579.7777, 7453, 8794 };

            ExpTree testTree = new ExpTree(Values[0].ToString() + "-(" + Values[1].ToString() + '+' + Values[2].ToString() + '+'
                                           + Values[3].ToString() + '-' + Values[4].ToString() + ")/" + Values[5].ToString() + "+("
                                           + Values[6].ToString() + '+' + Values[7].ToString() + ")*" + Values[8].ToString());

            double expectedOutput = Values[0] - (Values[1] + Values[2] +
                                                 Values[3] - Values[4]) / Values[5] + (
                Values[6] + Values[7]) * Values[8];

            Assert.AreEqual(testTree.Eval(), expectedOutput);
        }
Ejemplo n.º 11
0
        public void TestCosineFunctionExpression()
        {
            const string testExpression    = "COS(2)*2";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestCosineFunctionExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: -0.832293673094285");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (Math.Cos(2) * 2));
        }
Ejemplo n.º 12
0
        public void TestDivideExpression()
        {
            const string testExpression    = "3/2";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestDividExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 1.5");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (3d / 2d));
        }
Ejemplo n.º 13
0
        public void TestExponentiateExpression()
        {
            const string testExpression    = "2^4";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestExponentiateExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 16");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (Math.Pow(2, 4)));
        }
Ejemplo n.º 14
0
        public void TestParentheseExpression()
        {
            const string testExpression    = "(2+2)^(4/2)";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestParentheseExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 16");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, Math.Pow((2 + 2), (4d / 2d)));
        }
Ejemplo n.º 15
0
        public void TestWhitespaceSeparatedExpression()
        {
            const string testExpression    = " 1 + 1 ";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestWhitespaceSeparatedExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 2");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (1 + 1));
        }
Ejemplo n.º 16
0
        public void TestSingleConstantExpression()
        {
            const string testExpression    = "pi";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestSingleConstantExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 3.14159265358979");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, Math.PI);
        }
Ejemplo n.º 17
0
        public void TestIncompleteOperatorExpression()
        {
            const string testExpression    = "1+";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestNonsenseStringExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be false");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 0");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsFalse(isValidExpression);
            Assert.AreEqual(value, 0.0d);
        }
Ejemplo n.º 18
0
        public void TestSineFunctionExpression()
        {
            const string testExpression    = "SIN(1+1)";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestSineFunctionExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 0.909297426825682");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (Math.Sin(1 + 1)));
        }
Ejemplo n.º 19
0
        public void TestLogFunctionExpression()
        {
            const string testExpression    = "LOG(10^2,10)";
            var          testTree          = new ExpTree(testExpression);
            var          isValidExpression = testTree.IsValidExpression();
            var          value             = testTree.Eval();

            Console.WriteLine("TestLogFunctionExpression:");
            Console.WriteLine(" ExpTree.IsValidExpression() should be true");
            Console.WriteLine("  IsValidExpression() returned {0}", isValidExpression);
            Console.WriteLine(" Expected value: 2");
            Console.WriteLine("  Actual value:  {0}", value);
            Assert.IsTrue(isValidExpression);
            Assert.AreEqual(value, (Math.Log(100, 5 + 5)));
        }
Ejemplo n.º 20
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.º 21
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.º 22
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.º 23
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.º 24
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.º 25
0
 //=====================================================================
 // Menu3() -- evaluate the expression tree
 //=====================================================================
 private static void Menu3()
 {
     if (null != _expTree && null != _expStr)
     {
         if (_expTree.IsValidExpression())
         {
             string result = _expTree.Eval().ToString();
             //Console.WriteLine(_expStr + " = " + result);
             Console.WriteLine(result);
         }
         else
         {
             Console.WriteLine("Invalid expression");
         }
     }
     else
     {
         Console.WriteLine("No expresion entered");
     }
 }
        //Used to update a cell's evaluated text value, defined recursively to allow for nested references (e.g. B1 = B2, B2 = B3, B3 = "5")
        private string changedCellStringContents(AbstractCell cell)
        {
            if (cell.TextValue.First() != '=')
            {
                return(cell.TextValue);
            }
            else
            {
                ExpTree expTree = new ExpTree(cell.TextValue.Substring(1));
                List <ExpTree.VarNode> variableNodeList = expTree.variablesNodes();
                foreach (ExpTree.VarNode varNode in variableNodeList)
                {
                    int    column = ((int)varNode.getName()[0] - 64) - 1;
                    int    row    = int.Parse(varNode.getName().Substring(1)) - 1;
                    string temp   = cells[row, column].EvaluatedValue;
                    if (temp.Length == 0)
                    {
                        return("#REF!"); //Refernce Error
                    }
                    varNode.setValue(Double.Parse(cells[row, column].EvaluatedValue));
                }
                //Unsubscribe from the following Cells
                foreach (AbstractCell subCell in cell.SubscribedToCells)
                {
                    subCell.PropertyChangedValue -= (PropertyChangedEventHandler)cell.HandleValuePropertyChanged;
                }
                //Executed separately to ensure no cells are subrscribed if a reference error occurs
                foreach (ExpTree.VarNode varNode in variableNodeList)
                {
                    int column = ((int)varNode.getName()[0] - 64) - 1;
                    int row    = int.Parse(varNode.getName().Substring(1)) - 1;
                    cells[row, column].PropertyChangedValue += new PropertyChangedEventHandler(cell.HandleValuePropertyChanged);
                    //Record which cells we're subbed to
                    cell.SubscribedToCells.Add(cells[row, column]);
                }
                return(expTree.Eval().ToString());

                //return changedCellStringContents(cells[row, column]);
            }
        }
Ejemplo n.º 27
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.º 28
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.º 29
0
        static void Main(string[] args)
        {
            void Menu()
            {
                StringBuilder menu = new StringBuilder();

                menu.AppendLine("Select an option: ");
                menu.AppendLine("\t a.Enter in variables(must be done before evaluating)");
                menu.AppendLine("\t b.Enter equation ");
                menu.AppendLine("\t c.Evaluate");
                menu.AppendLine("\t d.Clear Equation");
                menu.AppendLine("\t e.Clear Variables");
                menu.AppendLine("\t q.Quit");
                Console.WriteLine(menu);
            }

            Dictionary <string, double> variables = new Dictionary <string, double>();
            string equation  = string.Empty;
            char   selection = '0';

            while (selection != 'q')
            {
                if (equation != string.Empty)
                {
                    Console.WriteLine("Current equation = " + equation);
                }
                Menu();
                selection = char.Parse(Console.ReadLine());
                selection = char.ToLower(selection);
                switch (selection)
                {
                case ('a'):
                    string varname = string.Empty;
                    int    value;
                    Console.Clear();
                    Console.WriteLine("Enter in the variable name: ");
                    varname = Console.ReadLine();
                    varname = varname.Replace(" ", string.Empty);
                    Console.WriteLine("Enter in the variable value: ");
                    value = int.Parse(Console.ReadLine());
                    Console.WriteLine("New Variable " + varname + " = " + value.ToString());
                    try { variables.Add(varname, value); }
                    catch { variables.Remove(varname); variables.Add(varname, value); }
                    Console.Clear();
                    break;

                case ('b'):
                    Console.Clear();
                    variables.Clear();
                    Console.WriteLine("Enter equation: ");
                    equation = Console.ReadLine();
                    equation = equation.Replace(" ", string.Empty);
                    Console.Clear();
                    break;

                case ('c'):
                    Console.WriteLine("Equations evaluates to : ");
                    ExpTree exptree = new ExpTree(equation);
                    foreach (string key in variables.Keys)
                    {
                        exptree.SetVAr(key, variables[key]);
                    }
                    Console.WriteLine(exptree.Eval());
                    Console.WriteLine("Enter any key to continue...");
                    Console.ReadLine();
                    Console.Clear();
                    break;

                case ('d'):
                    equation = string.Empty;
                    Console.Clear();
                    break;

                case ('e'):
                    variables.Clear();
                    Console.Clear();
                    break;

                default:
                    Console.Clear();
                    break;
                }
            }
        }
Ejemplo n.º 30
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);
        }