Exemplo n.º 1
0
 public ExpressionTree(ExprOperations Operation = null, Expression Left = null, Expression Right = null, ExpressionTree Parent = null)
 {
     if (Operation != null)
     {
         this.Operation = Operation;
     }
     else
     {
         this.Operation = ExprOperations.NoneOperations;
     }
     this.Left   = Left;
     this.Right  = Right;
     this.Parent = Parent;
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // sort out more complex examples
            string         s = "2+2";
            ExpressionTree e = ExpressionTree.Build(s);

            ExprVarValue[] vars = { new ExprVarValue("x", 2), new ExprVarValue("y", 3) };
            Console.WriteLine(s);
            foreach (ExprVarValue var in vars)
            {
                Console.WriteLine(var);
            }
            Console.Write(e.ToString() + " = ");
            Console.WriteLine(e.getValue(vars));

            Console.ReadKey();
        }
Exemplo n.º 3
0
        public static ExpressionTree Build(string s, ref int startPos)
        {
            ExpressionTree tree  = new ExpressionTree();
            ExpressionTree tRoot = tree;

            int i = startPos;

            while (i < s.Length)
            {
                Buttons ButtonType;
                string  button = ButtonFunctions.ParseNextButton(s, ref i, out ButtonType);
                Console.WriteLine(button);

                if (ButtonType == Buttons.Number)
                {
                    tree.AddValue(double.Parse(button));
                }

                else if (ButtonType == Buttons.Letter && button.Length == 1)
                {
                    tree.AddValue(button);
                }
                else
                {
                    ExprOperations newOperation = ExprOperations.NoneOperations;

                    if (ButtonType == Buttons.Letter && button.Length > 1)
                    {
                        newOperation = new ExprOperations(button);
                    }
                    else if (ButtonType == Buttons.Operator)
                    {
                        newOperation = new ExprOperations(button[0]);
                    }

                    if (ButtonType == Buttons.Operator || ButtonType == Buttons.Letter)
                    {
                        if (tree.Operation == ExprOperations.NoneOperations)
                        {
                            tree.Operation = newOperation;
                        }
                        else if (newOperation.Priority > tree.Operation.Priority)
                        {
                            tree.Right = new ExpressionTree(newOperation, tree.Right, null, tree);
                        }
                        tree = (ExpressionTree)tree.Right;
                    }
                    else
                    {
                        tree.Left      = tree.CopyPartial();
                        tree.Operation = newOperation;
                        tree.Right     = null;
                    }

                    if (ButtonType == Buttons.Bracket)
                    {
                        if (button[0] == '(')
                        {
                            if (tree.Left == null)
                            {
                                tree.Left = new ExpressionTree(Parent: tree);
                                tree      = (ExpressionTree)tree.Left;
                            }
                            else
                            {
                                tree.Right = new ExpressionTree(Parent: tree);
                                tree       = (ExpressionTree)tree.Right;
                            }
                        }
                        else if (button[0] == ')')
                        {
                            tree = tree.Parent;
                        }
                    }
                }
            }
            return(tRoot);
        }
Exemplo n.º 4
0
 public ExprVarValue(string Name, ExpressionTree Value)
 {
     this.Name  = Name;
     this.Value = Value;
 }
Exemplo n.º 5
0
 public ExprVarValue(string Name, double Value)
 {
     this.Name  = Name;
     this.Value = new ExpressionTree(null, new ExprValue(Value));
 }