Exemplo n.º 1
0
        public static ExprVarValue Parse(string s)
        {
            ExprVarValue res;

            int     i = 0;
            Buttons ButtonType;
            string  button;

            button = ButtonFunctions.ParseNextButton(s, ref i, out ButtonType);
            if (ButtonType != Buttons.Letter)
            {
                throw new Exception();
            }
            res.Name = button;

            button = ButtonFunctions.ParseNextButton(s, ref i, out ButtonType);
            if (button != "=")
            {
                throw new Exception();
            }

            res.Value = ExpressionTree.Build(s, ref i);

            return(res);
        }
Exemplo n.º 2
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);
        }