Exemple #1
0
        private Node <OperateObj> buildParseTreeHelp()//保证调用前栈顶一定是运算符
        {
            if (parseStack2.Count == 0)
            {
                return(null);
            }
            if (parseStack2.Peek().objType != 1)
            {
                return(new Node <OperateObj>(parseStack2.Pop()));
            }
            OperateObj        parentValue = parseStack2.Pop();
            Node <OperateObj> parentNode  = null;
            Node <OperateObj> leftNode    = null;
            Node <OperateObj> rightNode   = null;//其实是指向一个parent的指针

            if (parseStack2.Count == 0)
            {
                currErrorType = 4;
                return(null);
            }
            switch (parseStack2.Peek().objType) //为右孩子操心
            {
            case 1:                             //操作符
                rightNode = buildParseTreeHelp();
                break;

            case 2:                                                   //操作数
                rightNode = new Node <OperateObj>(parseStack2.Pop()); //左子的左右子为null
                break;

            default:
                break;
            }
            if (parseStack2.Count == 0)
            {
                currErrorType = 4;
                return(null);
            }
            switch (parseStack2.Peek().objType) //为左孩子操心
            {
            case 1:                             //操作符
                leftNode = buildParseTreeHelp();
                break;

            case 2:    //操作数
                leftNode = new Node <OperateObj>(parseStack2.Pop());
                break;

            default:
                break;
            }
            parentNode = new Node <OperateObj>(parentValue, leftNode, rightNode);
            return(parentNode);
        }
Exemple #2
0
        //构造解析树 迭代!    递归自下至上递归?  没见过  大概不行
        //迭代要遍历+操作符栈+操作数栈   递归只管当下
        //建树最好从顶向下建
        private bool buildParseTree()  //先将RPN整到一个解析栈中  再通过栈递归建树
        {
            if (l.Count == 0)
            {
                return(false);
            }
            if (currErrorType != 0)
            {
                return(false);
            }
            parseStack.Clear();
            parseStack2.Clear();
            //    parseTree.clear();

            int index = 0;

            while (index != l.Count)//如果表达式格式非法 (应该在界面层防止这个问题发生)  建栈会如何?
            {
                OperateObj obj = l.ElementAt <OperateObj>(index);
                switch (obj.objType)
                {
                case 1:    //操作符
                    Operator optTemp = (Operator)obj;
                    //Operator optStackTop;
                    //if (parseStack.Count==0)
                    //    optStackTop=null;
                    //else
                    //    optStackTop=(Operator)parseStack.Peek();

                    if (parseStack.Count == 0)
                    {
                        parseStack.Push(obj);
                    }
                    else
                    {
                        while (parseStack.Count != 0 && optTemp.getPriority() <= parseStack.Peek().getPriority())
                        {
                            parseStack2.Push(parseStack.Pop());
                        }
                        parseStack.Push(obj);
                    }

                    break;

                case 2:    //操作数
                    parseStack2.Push(obj);
                    break;

                case 3:                 //括号
                    Bracket objTemp = (Bracket)obj;
                    if (objTemp.isLeft) //左括号压栈
                    {
                        parseStack.Push(obj);
                    }
                    else                                       //右括号一直弹栈直到左括号出栈
                    {
                        while (parseStack.Peek().objType != 3) //不停弹栈
                        {
                            parseStack2.Push(parseStack.Pop());
                            // Operator objTemp2 = (Operator)parseStack.Pop();
                            //leftNode=new Node<OperateObj>()
                        }
                        parseStack.Pop();
                    }

                    break;

                default:
                    break;
                }
                index++;

                //return new BinaryTree<OperateObj>(ln);   //这一行似乎没有必要
                //parse
            }
            while (parseStack.Count != 0)
            {
                parseStack2.Push(parseStack.Pop());
            }
            parseTree = new BinaryTree <OperateObj> (buildParseTreeHelp());
            if (CurrentErrorType == 4)
            {
                return(false);
            }
            return(true);
        }