Example #1
0
        /// <summary>
        /// This is the main function of the console application.
        /// </summary>
        private static void Main()
        {
            int            result            = 0;
            int            quit              = 0;
            string         variableName      = null;
            string         currentExpression = null;
            double         declaredValue     = 0.0;
            ExpressionTree demoTree          = new ExpressionTree(null);

            while (quit == 0)
            {
                Console.WriteLine("menue current expression: {0}", currentExpression);
                Console.WriteLine("1. enter a new expression");
                Console.WriteLine("2. set a variable value");
                Console.WriteLine("3. Evalute Tree");
                Console.WriteLine("4. Quit");
                result = Convert.ToInt32(Console.ReadLine());
                if (result == 1)
                {
                    Console.WriteLine("enter the expression");
                    currentExpression = Console.ReadLine();
                    demoTree          = new ExpressionTree(currentExpression);
                }
                else if (result == 2)
                {
                    Console.WriteLine("enter the variable");
                    variableName = Console.ReadLine();
                    Console.WriteLine("enter the value");
                    declaredValue = Convert.ToDouble(Console.ReadLine());
                    demoTree.SetVariable(variableName, declaredValue);
                }
                else if (result == 3)
                {
                    Console.WriteLine("result for Evaluation");
                    Console.WriteLine(demoTree.Evaluate());
                }
                else if (result == 4)
                {
                    quit = 1;
                }
            }
        }
Example #2
0
        /// <summary>
        /// This is event handler for spreedsheet when a property is changed.
        /// </summary>
        /// <param name="sender">
        /// Represents the cell object being changed.
        /// </param>
        /// <param name="e">
        /// Represents the event arguements.
        /// </param>
        public void CellPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SpreadSheetCell eventCell = sender as SpreadSheetCell;

            if (eventCell.Text.Length == 0)
            {
                eventCell.ValueStr = string.Empty;
            }
            else if (eventCell.Text.StartsWith("="))
            {
                int    ifSelf = 0;
                string text   = eventCell.Text.Substring(1);
                this.spreadSheetTree = new ExpressionTree(text);
                string variableStr       = string.Empty;
                string variableStrParser = string.Empty;
                double declaredValue     = 0.0;
                int    tempCol           = 0;
                int    tempRow           = 0;
                string combiner          = string.Empty;
                if (this.DetermineBadReference(text) == true)
                {
                    eventCell.ValueStr = "!bad reference";
                }
                else if (this.DetermineSelfReference(text, eventCell) == true)
                {
                    eventCell.ValueStr = "!self reference";
                    ifSelf             = 1;
                }
                else
                {
                    if (text[text.Length - 1] == ')')
                    {
                        text = text.Substring(0, text.Length - 1);
                    }

                    for (int textIndex = 0; textIndex < text.Length; textIndex++)
                    {
                        if (this.columnSymbols.Contains(text[textIndex]))
                        {
                            if (textIndex + 2 == text.Length)
                            {
                                variableStr = text.Substring(textIndex);
                                tempCol     = variableStr[0] - 65;
                                tempRow     = variableStr[1] - 49;
                                variableStr = variableStr.Substring(0, 2);
                                try
                                {
                                    if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                    {
                                        eventCell.ValueStr = "!self reference";
                                        ifSelf             = 1;
                                        break;
                                    }
                                    else
                                    {
                                        declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                    }
                                }
                                catch (Exception)
                                {
                                    declaredValue = 0.0;
                                }

                                this.spreadSheetTree.SetVariable(variableStr, declaredValue);
                                this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                            }
                            else if (textIndex + 3 == text.Length)
                            {
                                variableStr       = text.Substring(textIndex);
                                tempCol           = variableStr[0] - 65;
                                variableStrParser = variableStr.Substring(1);
                                int.TryParse(variableStrParser, out tempRow);
                                tempRow--;
                                try
                                {
                                    if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                    {
                                        eventCell.ValueStr = "!self reference";
                                        ifSelf             = 1;
                                        break;
                                    }
                                    else
                                    {
                                        declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                    }
                                }
                                catch (Exception)
                                {
                                    declaredValue = 0.0;
                                }

                                this.spreadSheetTree.SetVariable(variableStr, declaredValue);
                                this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                            }
                            else
                            {
                                variableStr = text.Substring(textIndex);
                                if (this.operations.Contains(variableStr[2]))
                                {
                                    tempCol  = variableStr[0] - 65;
                                    tempRow  = variableStr[1] - 49;
                                    combiner = variableStr[0].ToString() + variableStr[1].ToString();
                                    try
                                    {
                                        if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                        {
                                            eventCell.ValueStr = "!self reference";
                                            ifSelf             = 1;
                                            break;
                                        }
                                        else
                                        {
                                            declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        declaredValue = 0.0;
                                    }

                                    this.spreadSheetTree.SetVariable(combiner, declaredValue);
                                    this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                                }
                                else
                                {
                                    tempCol           = variableStr[0] - 65;
                                    variableStrParser = variableStr.Substring(1, 2);
                                    int.TryParse(variableStrParser, out tempRow);
                                    tempRow--;
                                    combiner = variableStr[0].ToString() + variableStr[1].ToString() + variableStr[2].ToString();
                                    try
                                    {
                                        if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                        {
                                            eventCell.ValueStr = "!self reference";
                                            ifSelf             = 1;
                                            break;
                                        }
                                        else
                                        {
                                            declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        declaredValue = 0.0;
                                    }

                                    this.spreadSheetTree.SetVariable(combiner, declaredValue);
                                    this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                                }
                            }
                        }
                    }

                    if (ifSelf == 0 && this.DetermineCircularReference(text, eventCell) == true)
                    {
                        eventCell.ValueStr = "!circular reference";
                    }
                    else if (ifSelf == 0)
                    {
                        eventCell.ValueStr = this.spreadSheetTree.Evaluate().ToString();
                    }
                }
            }
            else
            {
                eventCell.ValueStr = eventCell.Text;
            }

            this.PropertyChanged?.Invoke(sender, new PropertyChangedEventArgs("ValueStr"));
        }
Example #3
0
        public void TestingTreeExpressionMult()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("6*5");

            Assert.AreEqual(expressionTreeTest.Evaluate(), 30.0);
        }
Example #4
0
        public void TestingTreeExpressionSubract()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("6-5");

            Assert.AreEqual(expressionTreeTest.Evaluate(), 1.0);
        }
Example #5
0
        public void TestingTreeExpressionAdd()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("6+5+7");

            Assert.AreEqual(expressionTreeTest.Evaluate(), 18.0);
        }
Example #6
0
        public void TestingTreeExpression()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("5+5");

            Assert.AreEqual(expressionTreeTest.Expression, "5+5");
        }
Example #7
0
        public void TestingEvaluateParentheses()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("(5*(2+2))");

            Assert.AreEqual(expressionTreeTest.Evaluate(), 20.0);
        }
Example #8
0
        public void TestingInitailVariable()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("A1");

            Assert.AreEqual(expressionTreeTest.Evaluate(), 0.0);
        }
Example #9
0
        public void TestingTreeExpressionDivide()
        {
            ExpressionTree expressionTreeTest = new ExpressionTree("10/5");

            Assert.AreEqual(expressionTreeTest.Evaluate(), 2.0);
        }