Esempio n. 1
0
        private static void Main(string[] args)
        {
            bool           endProgram = false;
            string         userInput;
            ExpressionTree expressionTree = new ExpressionTree("A1-B1-C1");

            while (endProgram != true)
            {
                Console.WriteLine(
                    "Menu (Current Expression: {0})\n" +
                    "1=Enter a new Expression\n" +
                    "2=Set a variable value\n" +
                    "3=Evaluate tree\n" +
                    "4=Quit", expressionTree.Expression);
                userInput = Console.ReadLine();
                if (userInput == "4")
                {
                    endProgram = true;
                }
                else if (userInput == "3")
                {
                    Console.WriteLine(expressionTree.Evaluate());
                }
                else if (userInput == "2")
                {
                    Console.WriteLine("Enter variable name:");
                    string variable = Console.ReadLine();
                    Console.WriteLine("Enter new variable value");
                    expressionTree.SetVariable(variable, Convert.ToDouble(Console.ReadLine()));
                }
                else if (userInput == "1")
                {
                    Console.WriteLine("Enter new expression");
                    expressionTree.Expression = Console.ReadLine();
                }
            }
        }
Esempio n. 2
0
        public void TreeTest()
        {
            // normal test
            ExpressionTree setVariableTestTree = new ExpressionTree("A1+B1+C1");

            setVariableTestTree.SetVariable("A1", 1.0);
            setVariableTestTree.SetVariable("B1", 2.0);
            setVariableTestTree.SetVariable("C1", 3.0);
            Assert.AreEqual(6.0, setVariableTestTree.Evaluate());
            setVariableTestTree = new ExpressionTree("A3+B3+C3");
            Assert.AreEqual(0.0, setVariableTestTree.Evaluate());
            setVariableTestTree = new ExpressionTree("A1-B1-C1");
            setVariableTestTree.SetVariable("A1", 1.0);
            setVariableTestTree.SetVariable("B1", 2.0);
            setVariableTestTree.SetVariable("C1", 3.0);
            Assert.AreEqual(-4.0, setVariableTestTree.Evaluate());

            // Variable not in expression
            setVariableTestTree.SetVariable("A2", 1.0);
            Assert.AreEqual(-4.0, setVariableTestTree.Evaluate());
            // Varible and numerical value
            setVariableTestTree.Expression = "A1+B1+10";
            Assert.AreEqual(13.0, setVariableTestTree.Evaluate());
        }
    public void DistortCenterMiddle(ExpressionTree tree)
    {
        // Create new image for the distoring
        targetTexture = new Texture2D(texture.width * 10, texture.height * 10, TextureFormat.RGBA32, false);

        Number newCoord;

        // Go through each pixel
        for (int i = -texture.width / 2; i < texture.width / 2; i++)
        {
            for (int k = -texture.height / 2; k < texture.height / 2; k++)
            {
                newCoord = tree.Evaluate(i, k);
                // Debug.Log(i + " " + k + " -> " + newCoord.number + " " + newCoord.i);

                targetTexture.SetPixel((int)newCoord.number + targetTexture.width / 2, (int)newCoord.i + targetTexture.height / 2,
                                       texture.GetPixel(i + texture.width / 2, k + texture.height / 2));
            }
        }

        // Apply modifications to distorted image and set the target
        targetTexture.Apply();
        distortTarget.texture = targetTexture;
    }
Esempio n. 4
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)");
            string         expression3 = "7*(8*4)/(1-2-34)";
            ExpressionTree test3       = new ExpressionTree(expression3);

            test3.CreateExpressionTree(expression3);
            Assert.That(-6.4, Is.EqualTo(test3.Evaluate()), "Failed at test3 (-6.4)");
        }
Esempio n. 5
0
        public void ExpTreeStatementPrecedence()
        {
            ExpressionTree t1 = new ExpressionTree("100-(10*2)+8/2");

            Assert.AreEqual(84, t1.Evaluate());
        }
Esempio n. 6
0
        public void EvaluateTest10()
        {
            ExpressionTree tree = new ExpressionTree("3+(2*(3+2))*2");

            Assert.AreEqual(23, tree.Evaluate());
        }
Esempio n. 7
0
        public static Bitmap Draw(ExpressionTree function, double zoom, double cameraX, double cameraY, double width = 300, double height = 300)
        {
            Bitmap   graphic = new Bitmap((int)width, (int)height);
            Graphics g       = Graphics.FromImage(graphic);

            g.FillRectangle(new SolidBrush(Color.Gray), 0, 0, (int)width, (int)height);
            double graphWidth  = (double)width / zoom;
            double graphHeight = (double)height / zoom;



            List <PointF> functionPoints = new List <PointF>();

            for (double sampler = cameraX - graphWidth / 2; sampler <= cameraX + graphWidth / 2; sampler += 0.5)
            {
                try
                {
                    functionPoints.Add(new PointF(
                                           (float)sampler - (float)cameraX + (float)graphWidth / 2,
                                           (float)Math.Min(height + 50, Math.Max(-50,

                                                                                 ((float)graphHeight - ((float)function.Evaluate(
                                                                                                            new Dictionary <string, double>()
                    {
                        { "x", sampler }
                    }) + (float)graphHeight / 2 + cameraY))))));
                }
                catch {
                }
            }

            List <PointF> derivativePoints = new List <PointF>();
            var           derivative       = function.GetDerivative();

            for (double sampler = cameraX - graphWidth / 2; sampler <= cameraX + graphWidth / 2; sampler += 0.6)
            {
                try
                {
                    derivativePoints.Add(new PointF(
                                             (float)sampler - (float)cameraX + (float)graphWidth / 2,
                                             (float)Math.Min(height + 50, Math.Max(-50,

                                                                                   ((float)graphHeight - ((float)derivative.Evaluate(
                                                                                                              new Dictionary <string, double>()
                    {
                        { "x", sampler }
                    }) + (float)graphHeight / 2 + cameraY))))));
                }
                catch
                {
                }
            }

            g.DrawCurve(new Pen(Color.Red, 2), functionPoints.ToArray());
            g.DrawCurve(new Pen(Color.Blue, 2), derivativePoints.ToArray());

            g.DrawLine(
                new Pen(Color.Black, 2),
                new Point((int)(graphWidth / 2 - cameraX), 0),
                new Point((int)(graphWidth / 2 - cameraX), (int)height - 1));
            g.DrawLine(
                new Pen(Color.Black, 2),
                new Point(0, (int)(graphHeight / 2 - cameraY)),
                new Point((int)width - 1, (int)(graphHeight / 2 - cameraY)));
            g.Dispose();
            return(graphic);
        }
Esempio n. 8
0
        public void ExpressionTreeMult()
        {
            ExpressionTree t1 = new ExpressionTree("5*2");

            Assert.AreEqual(10, t1.Evaluate());
        }
Esempio n. 9
0
        public void ExpressionTreeAdd()
        {
            ExpressionTree t1 = new ExpressionTree("5+2");

            Assert.AreEqual(7, t1.Evaluate());
        }
Esempio n. 10
0
        public double TestConstantEvaluation(string expression)
        {
            ExpressionTree tree = new ExpressionTree(expression);

            return(tree.Evaluate());
        }
Esempio n. 11
0
        public void EvaluateTest4()
        {
            ExpressionTree tree = new ExpressionTree("2");

            Assert.AreEqual(2, tree.Evaluate());
        }
Esempio n. 12
0
        public void EvaluateTest1()
        {
            ExpressionTree tree = new ExpressionTree("A1-12-B1");

            Assert.AreEqual(-12, tree.Evaluate());
        }
Esempio n. 13
0
        /// <summary>
        /// Event handler for cell text property change.
        /// </summary>
        /// <param name="sender">The cell that been changed.</param>
        private void TextChangeHandler(object sender)
        {
            // cell that been changed in UI.
            BaseCell spreadSheetCell = sender as BaseCell;

            // the spreadsheet cell that corresponds to the changed cell.
            string newFormula = spreadSheetCell.Text;

            if (!string.IsNullOrEmpty(newFormula))
            {
                newFormula = Regex.Replace(newFormula, @"s", string.Empty);
            }

            ExpressionTree expTree;

            if (string.IsNullOrEmpty(newFormula))
            {
                spreadSheetCell.ChangeValue(string.Empty);
            }
            else if (newFormula[0] != '=')
            {
                // if the new formula is a  value
                // change the cell value to changed cell text.
                double number;
                string newValue = string.Empty;
                if (double.TryParse(newFormula, out number))
                {
                    // if the new value is a double
                    newValue = newFormula;
                }
                else
                {
                    // if the new value is not a double
                    newValue = "0";
                }

                spreadSheetCell.ChangeValue(newValue);
            }
            else
            {
                // if the new formula is an expression.
                // pass the new formula into the expression tree to evaluate it
                string expression = newFormula.Substring(1);

                // if the text reference to cell itself, throw an exception and set text to "circular Reference".
                string spreadSheetCellIndex = Convert.ToChar(spreadSheetCell.ColumnIndex + 65) + (spreadSheetCell.RowIndex + 1).ToString();

                if (expression.Contains(spreadSheetCellIndex))
                {
                    spreadSheetCell.Text = "CircularRef";
                    throw new System.ArgumentException("Circular reference occurs", "text");
                }

                Queue <string> varQueue = this.GetVarNames(expression);
                expTree = new ExpressionTree(expression);
                this.UpdateExpTree(spreadSheetCell, expTree, varQueue);

                // change cell value to the evaluated result of given expression.
                spreadSheetCell.ChangeValue(expTree.Evaluate().ToString());
            }

            // update cells that subscribe to this cell in spreadsheet
            this.UpdateSubCells(spreadSheetCell);
        }
        /// <summary>
        /// DeisplayMenu:
        ///     this will display the test menu in an infinite loop until "4" is entered
        ///     by the user.
        /// </summary>
        public void DisplayMenu()
        {
            string infixExpression = string.Empty;

            do
            {
                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();
                if (infixExpression != string.Empty)
                {
                    Console.WriteLine("Expression: " + infixExpression);
                }

                option = Console.ReadLine();

                switch (option)
                {
                case "1":
                    Console.Write("Enter new expression: ");
                    infixExpression = Console.ReadLine();
                    expressionTree  = new ExpressionTree(infixExpression);
                    break;

                case "2":
                    Console.Write("Enter variable name: ");     //get variable name
                    input = Console.ReadLine();

                    bool setValue = false;                //setValue flag
                    do
                    {
                        Console.Write("Set value: ");     //get vriable value
                        string value = Console.ReadLine();
                        double res   = 0;

                        if (double.TryParse(value, out res))
                        {
                            expressionTree.SetVariable(input, res);
                            setValue = true;
                        }
                        Console.WriteLine();
                    } while (!setValue);
                    break;

                case "3":
                    double result = expressionTree.Evaluate();
                    if (result.CompareTo(double.NaN) == 0)
                    {
                        Console.WriteLine("_err: can't evaluate, not all variables are declared.");
                        Console.WriteLine();
                    }
                    else if (result == double.PositiveInfinity)
                    {
                        Console.WriteLine("_err: can't evaluate, DivideByZeroException.");
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("result = " + result);
                    }
                    Console.ReadLine();

                    break;

                default:
                    Console.WriteLine("Goodbye");
                    break;
                }

                Console.Clear();
            } while (option != "4");
        }
Esempio n. 15
0
        /// <summary>
        /// Cell property changed event handler. change of a value of a cell.
        /// </summary>
        /// <param name="sender">Sender of event.</param>
        /// <param name="e">What is changed.</param>
        private void HandleCellPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // cell that been changed in UI.
            BaseCell uICell = sender as BaseCell;

            // the spreadsheet cell that corresponds to the changed cell.
            BaseCell       spreadSheetCell = this.spreadSheet[uICell.RowIndex, uICell.ColumnIndex];
            string         newFormula      = spreadSheetCell.Text;
            ExpressionTree expTree;

            // remove all whitespace in the input formula:
            newFormula = Regex.Replace(newFormula, @"s", string.Empty);
            if (newFormula[0] != '=')
            {
                // if the newformula is a  value
                // change the cell value to changed cell text.
                double number;
                string newValue = string.Empty;
                if (double.TryParse(newFormula, out number))
                {
                    // if the new value is a double
                    newValue = newFormula;
                }
                else
                {
                    // if the new value is not a double
                    newValue = "0";
                }

                spreadSheetCell.ChangeValue(newValue);

                // refresh subscriber cells
                for (int i = 0; i < spreadSheetCell.Subscriber.Count; ++i)
                {
                    string   curSubCellIndex      = spreadSheetCell.Subscriber[i];
                    int      col                  = Convert.ToInt32(curSubCellIndex[0]) - 65;
                    int      row                  = Convert.ToInt32(curSubCellIndex.Substring(1)) - 1;
                    BaseCell curSubscriber        = this.spreadSheet[row, col];
                    string   spreadSheetCellIndex = Convert.ToChar(spreadSheetCell.ColumnIndex + 65) + Convert.ToString(spreadSheetCell.RowIndex + 1);

                    if (curSubscriber.Text.Contains(spreadSheetCellIndex))
                    {
                        // if spread sheet cell is part of subscriber expression.
                        Queue <string> varQueue = this.GetVarNames(curSubscriber.Text.Substring(1));
                        expTree = new ExpressionTree(curSubscriber.Text.Substring(1));

                        // get the variable name and corresponding value in the spreadsheet
                        this.UpdateExpTree(spreadSheetCell, expTree, ref col, ref row, varQueue);
                        curSubscriber.ChangeValue(expTree.Evaluate().ToString());
                    }
                }
            }
            else
            {
                // if the newformula is an expression.
                // pass the newformula into the expression tree to evaulate it
                string         expression = newFormula.Substring(1);
                Queue <string> varQueue   = this.GetVarNames(expression);
                expTree = new ExpressionTree(expression);
                this.UpdateExpTree(spreadSheetCell, expTree, varQueue);

                // change cell value to the evaluated result of given expression.
                spreadSheetCell.ChangeValue(expTree.Evaluate().ToString());
            }
        }
Esempio n. 16
0
        public void ExpTreeStatementParentheses()
        {
            ExpressionTree t1 = new ExpressionTree("((10+7-2)*(2+10)-7+(2-1))");

            Assert.AreEqual(174, t1.Evaluate());
        }
Esempio n. 17
0
        public void EvaluateTest5()
        {
            ExpressionTree tree = new ExpressionTree(string.Empty);

            Assert.AreEqual(0, tree.Evaluate());
        }
Esempio n. 18
0
        public void EvaluateTest7()
        {
            ExpressionTree tree = new ExpressionTree("12+13*2");

            Assert.AreEqual(38, tree.Evaluate());
        }
Esempio n. 19
0
        public void UnidentifiedVariablesExpressionTree()
        {
            ExpressionTree t1 = new ExpressionTree("T1+T2");

            Assert.AreEqual(0, t1.Evaluate());
        }
Esempio n. 20
0
        public void EvaluateTest8()
        {
            ExpressionTree tree = new ExpressionTree("3*(13-2)");

            Assert.AreEqual(33, tree.Evaluate());
        }
Esempio n. 21
0
        public void ExpressionTreeSub()
        {
            ExpressionTree t1 = new ExpressionTree("5-2");

            Assert.AreEqual(3, t1.Evaluate());
        }
Esempio n. 22
0
        public void EvaluateTest9()
        {
            ExpressionTree tree = new ExpressionTree("3+(13-2)*2");

            Assert.AreEqual(25, tree.Evaluate());
        }
Esempio n. 23
0
        public void ExpressionTreeDiv()
        {
            ExpressionTree t1 = new ExpressionTree("10/2");

            Assert.AreEqual(5, t1.Evaluate());
        }
Esempio n. 24
0
 public decimal Evaluate()
 {
     return(ExpressionTree.Evaluate());
 }