示例#1
0
        /**
         * 获取每个节点的运算结果,并检验除法
         * 1)除数为0
         * 2)不能整除
         * 出现以上两种情况的话将该运算符转换成其他三种运算符
         *
         * @return result
         */
        public String getResult()
        {
            if (hasChild())
            {
                switch (str)
                {
                case "+":
                    return((int.Parse(getLchild().getResult()) + int.Parse(getRchild().getResult())).ToString());

                case "-":
                    return((int.Parse(getLchild().getResult()) - int.Parse(getRchild().getResult())).ToString());

                case "*":
                    return((int.Parse(getLchild().getResult()) * int.Parse(getRchild().getResult())).ToString());

                case "/":
                    if (getRchild().getResult().Equals("0"))
                    {
                        while (str.Equals("/"))
                        {
                            str = (Ran.getOperator().ToString());
                        }
                        return(this.getResult());
                    }
                    else if (int.Parse(getLchild().getResult()) % int.Parse(getRchild().getResult()) != 0)
                    {
                        while (str.Equals("/"))
                        {
                            str = (Ran.getOperator()).ToString();
                        }
                        return(this.getResult());
                    }
                    else
                    {
                        return((int.Parse(getLchild().getResult()) / int.Parse(getRchild().getResult())).ToString());
                    }
                }
            }
            return(str);
        }
示例#2
0
        /**
         * 生成二叉树
         */
        public void createBTree()
        {
            Tree lchild, rchild, lnode, rnode;

            if (num == 1)
            {
                lchild = new Tree((Ran.getNumber(30)).ToString(), null, null);
                rchild = new Tree((Ran.getNumber(40)).ToString(), null, null);
                root   = new Tree((Ran.getOperator()).ToString(), lchild, rchild);
            }
            else
            {
                int    num1  = 0;
                int    n     = getDeep() - 3;
                bool[] place = Ran.getChildPlace(num);
                root = new Tree((Ran.getOperator()).ToString(), null, null);
                opeList.Add(root);

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < (int)Math.Pow(2, i); j++, num1++)
                    {
                        lchild = new Tree((Ran.getOperator()).ToString(), null, null);
                        rchild = new Tree((Ran.getOperator()).ToString(), null, null);
                        opeList[j + num1].setChild(lchild, rchild);
                        opeList.Add(lchild);
                        opeList.Add(rchild);
                    }
                }

                for (int i = 0; i < place.Length; i++)
                {
                    if (place[i])
                    {
                        lnode = new Tree((Ran.getNumber(40)).ToString(), null, null);
                        rnode = new Tree((Ran.getNumber(60)).ToString(), null, null);
                        if (i % 2 == 0)
                        {
                            lchild = new Tree((Ran.getOperator()).ToString(), lnode, rnode);
                            opeList.Add(lchild);
                            opeList[num1].setLchild(lchild);
                        }
                        else
                        {
                            rchild = new Tree((Ran.getOperator()).ToString(), lnode, rnode);
                            opeList.Add(rchild);
                            opeList[num1].setRchild(rchild);
                        }
                    }
                    else
                    {
                        if (i % 2 == 0)
                        {
                            lchild = new Tree((Ran.getNumber(15)).ToString(), null, null);
                            opeList[num1].setLchild(lchild);
                        }
                        else
                        {
                            rchild = new Tree((Ran.getNumber(100)).ToString(), null, null);
                            opeList[num1].setRchild(rchild);
                        }
                    }
                    num1 = num1 + i % 2;
                }
            }
        }