//Evaluate the question tree to get the answer public double evaluate() { // Debug.Log ("Evaluation: " + value + " " + type); switch (type) { case Type.Null: return(value); case Type.Add: return(left.evaluate() + right.evaluate()); case Type.Sub: return(left.evaluate() - right.evaluate()); case Type.Mul: return(left.evaluate() * right.evaluate()); case Type.Div: Assert.IsTrue(right.evaluate() != 0.0d); return(left.evaluate() / right.evaluate()); } return(double.NaN); }
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); }