Exemplo n.º 1
0
        //https://www.geeksforgeeks.org/expression-tree/
        //1) If character is operand push that into stack
        //2) If character is operator pop two values from stack make them its child and push current node again.
        //At the end only element of stack will be root of expression tree.
        public ExpTree(string expression)
        {
            root = new opNode();
            opNode  opTemp;
            varNode varTemp;
            numNode numTemp;

            regex = expression;
            Queue <string> post = new Queue <string>();

            post = postfix();

            Stack <Node> expbuilder = new Stack <Node>();
            string       item       = "";
            char         op;
            double       num;

            var isNumeric = double.TryParse(item, out num);

            while (post.Count > 0)
            {
                item = post.Dequeue();
                if (item == "+" || item == "-" || item == "*" || item == "/" || item == "^")
                {
                    opTemp        = new opNode();
                    op            = item[0];
                    opTemp.eexp   = op;
                    opTemp.lleft  = expbuilder.Pop();
                    opTemp.rright = expbuilder.Pop();
                    expbuilder.Push(opTemp);
                }
                else
                {
                    isNumeric = double.TryParse(item, out num);
                    if (isNumeric)
                    {
                        numTemp         = new numNode();
                        numTemp.nnumber = num;
                        expbuilder.Push(numTemp);
                    }
                    else
                    {
                        varTemp       = new varNode();
                        varTemp.nname = item;
                        expbuilder.Push(varTemp);
                    }
                }
            }
            root = expbuilder.Pop();
        }
Exemplo n.º 2
0
        //https://www.geeksforgeeks.org/expression-tree/
        //1) If character is operand push that into stack
        //2) If character is operator pop two values from stack make them its child and push current node again.
        //At the end only element of stack will be root of expression tree.
        public ExpTree(string expression)
        {
            opNode  opTemp  = new opNode();
            varNode varTemp = new varNode();
            numNode numTemp = new numNode();

            regex = expression;
            Queue <string> post = new Queue <string>();

            post = postfix();

            Stack <Node> expbuilder = new Stack <Node>();
            string       item       = "";
            char         op;
            double       num;

            var isNumeric = double.TryParse(item, out num);

            while (post.Count > 0)
            {
                item = post.Dequeue();
                if (item == "+" || item == "-" || item == "*" || item == "/" || item == "^")
                {
                    opTemp        = new opNode();
                    op            = item[0];
                    opTemp.eexp   = op;
                    opTemp.lleft  = expbuilder.Pop();
                    opTemp.rright = expbuilder.Pop();
                    expbuilder.Push(opTemp);
                }
                else
                {
                    isNumeric = double.TryParse(item, out num);
                    if (isNumeric)
                    {
                        numTemp         = new numNode();
                        numTemp.nnumber = num;
                        expbuilder.Push(numTemp);
                    }
                    else
                    {
                        varTemp       = new varNode();
                        varTemp.nname = item;
                        expbuilder.Push(varTemp);
                    }
                }
            }
            //Added to handle if the expression is just a number or variable like "A1" or just "5"
            if (expbuilder.Peek().GetType() == varTemp.GetType())
            {
                root = new varNode();
                root = expbuilder.Pop();
            }
            else if (expbuilder.Peek().GetType() == numTemp.GetType())
            {
                root = new numNode();
                root = expbuilder.Pop();
            }
            else if (expbuilder.Peek().GetType() == opTemp.GetType())
            {
                root = new opNode();
                root = expbuilder.Pop();
            }
            else
            {
                //ERROR!
            }
        }