Ejemplo n.º 1
0
    public static Question generate()
    {
        List <RandNode> opdNodes    = new List <RandNode> ();
        List <RandNode> oprNodes    = new List <RandNode> ();
        string          questionStr = "";
        RandNode        rn          = getOpd(GlobalSettings.largestNum);

        opdNodes.Add(rn);
        questionStr += rn.number.ToString();

        for (int i = 0; i < GlobalSettings.numberOfOperators - 1; i++)
        {
            RandNode opr = getOps();

            /** Deal with divide, we only want integer for dividen **/
            RandNode opd;
            if (opr.type == QTree.Type.Div)
            {
                opd = getValidDivisor(getPrevDiv(oprNodes, opdNodes, opdNodes.Count - 1));
            }
            else
            {
                opd = getOpd(GlobalSettings.largestNum);
            }

            //Add them in
            opdNodes.Add(opd);
            oprNodes.Add(opr);
            questionStr += " " + QTree.getOperatorStr(opr.type) + " " + opd.number.ToString();
        }


        //Debug.Log (questionStr);
        QTree root        = QTreeBuilder.build(opdNodes, oprNodes);
        int   rightAnswer = (int)root.evaluate();

        int    score        = getScore(root);
        string explaination = getExplaination(root);

        Debug.Log("Explaination " + explaination + ", " + score);
        Question question = new Question(questionStr, rightAnswer, score, explaination);


        //Debug.Log ("The correct answer is " + question.answer.ToString ());
        return(question);
    }
Ejemplo n.º 2
0
    /*
     * 5 + 6 * 3 / 2 - 3
     * =5 + 18 / 2 - 3
     * =5 + 9 -3
     * =14 - 3
     * =11
     *  BFS to a stack, then pop out
     *
     */


    private static string getExplaination(QTree root)
    {
        Stack <string> exp = new Stack <string> ();
        //bfs
        Stack <QTreeExt> nodes = new Stack <QTreeExt>();

        nodes.Push(new QTreeExt(root, false));
        while (nodes.Count > 0)
        {
            Stack <QTreeExt> nextLevel = new Stack <QTreeExt> ();
            string           levelRes  = "";
            while (nodes.Count > 0)
            {
                QTreeExt qt = nodes.Pop();
                if (qt.isVisited)
                {
                    levelRes += QTree.getOperatorStr(qt.type);
                }
                else
                {
                    levelRes += qt.evaluate();
                    if (qt.left != null && qt.right != null)
                    {
                        nextLevel.Push(new QTreeExt(qt.left, false));
                        nextLevel.Push(new QTreeExt(qt, true));                          //make sure we don't visit this again
                        nextLevel.Push(new QTreeExt(qt.right, false));
                    }
                }
            }
            nodes = nextLevel;
            exp.Push(levelRes);
        }

        //pop the result stack
        string result = " ";

        while (exp.Count > 0)
        {
            result += exp.Pop();
            result += "\n=";
        }

        return(result);
    }