コード例 #1
0
ファイル: solEval.php プロジェクト: axomar/ITS
function evalStatement($data, $input, $answer)
{
    $m = new EvalMath();
    $ops = array("==", "!=", "GE", "LE", "GT", "LT");
    $numStatement = 0;
    $numOperator = 0;
    $statement1 = "";
    $statement2 = "";
    for ($j = 0; $j <= count($data) - 1; $j++) {
        if (in_array($data[$j], $ops)) {
            $numStatement++;
            $numOperator = array_search($data[$j], $ops);
        } else {
            if ($numStatement == 0) {
                $statement1 .= $data[$j] . " ";
            } else {
                $statement2 .= $data[$j] . " ";
            }
        }
    }
    $equation = genEquation($statement1, $input, $answer);
    $value1 = $m->evaluate($equation);
    if ($value1 == "") {
        $value1 = $equation;
    }
    $equation = genEquation($statement2, $input, $answer);
    $value2 = $m->evaluate($equation);
    if ($value2 == "") {
        $value2 = $equation;
    }
    if (condicional($value1, $ops[$numOperator], $value2)) {
        return TRUE;
    } else {
        return FALSE;
    }
}
コード例 #2
0
ファイル: solEval v1.1.php プロジェクト: axomar/ITS
function Evaluation($id, $value, $input, $exercise_id, $operatorData, $answer)
{
    $m = new EvalMath();
    $xml = simplexml_load_file($exercise_id) or die("Error: Cannot create object");
    $count_options = $xml->input[$id]->count();
    for ($i = 0; $i < $count_options; $i++) {
        $operator = $xml->input[$id]->option[$i]->operator;
        $equation = trim($xml->input[$id]->option[$i]->equation);
        $data = explode(" ", $equation);
        $equation = "";
        $operatorDT = "";
        if ($operator == 'IF') {
            // retrieve elements from de conditional
            $cont = 0;
            foreach ($data as $element) {
                if ($element[0] == 'v') {
                    $trimmed = intval(ltrim($element, "v"));
                    $value[$cont] = strval($input[$trimmed]);
                } elseif ($element[0] == 'a') {
                    $trimmed = intval(ltrim($element, "a"));
                    $value[$cont] = strval($answer[$trimmed - 1]);
                } else {
                    $value[$cont] = strval($element);
                }
                //if($value[$cont] == "==")
                //  return "ccc";
                $cont++;
            }
            // evaluate one or two conditional statements
            if ($cont <= 3) {
                if (condicional($value[0], $data[1], $value[2])) {
                    return $xml->input[$id]->option[$i]->result;
                    $i = $count_options;
                }
            } else {
                if ($data[3] == "and") {
                    if (condicional($value[0], $data[1], $value[2]) and condicional($value[4], $data[5], $value[6])) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                } else {
                    if ($data[3] == "or") {
                        if (condicional($value[0], $data[1], $value[2]) or condicional($value[4], $data[5], $value[6])) {
                            return $xml->input[$id]->option[$i]->result;
                            $i = $count_options;
                        }
                    }
                }
            }
            /*
            switch($data[1]) 
            {
             case "==": 
            			if($equation == $data[2]) 
            			  {return $xml->input[$id]->option[$i]->result;
            			    $i=$count_options;
            			  }
            			  break;
             case "!=": if($equation != $data[2]) 
            			   {return $xml->input[$id]->option[$i]->result;
            			    $i=$count_options;			
            			   }
            			  break;
             case "GT": if($equation > $data[2]) 
            			   {return $xml->input[$id]->option[$i]->result;
            			    $i=$count_options;			
            			   }
            			  break;
             case "GE": if($equation >= $data[2]) 
            			   {return $xml->input[$id]->option[$i]->result;
            			    $i=$count_options;			
            			   }
            			  break;
             case "LT": if($equation < $data[2]) 
            			   {return $xml->input[$id]->option[$i]->result;
            			    $i=$count_options;			
            			   }
            			  break;			  
             case "LE": if($equation <= $data[2]) 
            			   {return $xml->input[$id]->option[$i]->result;
            			    $i=$count_options;			
            			   }
            			  break;
            }	// SWITCH
            */
        } else {
            foreach ($data as $element) {
                if (is_numeric($element)) {
                    $equation .= $element;
                } else {
                    if ($element[0] == 'o') {
                        $operatorDT = ltrim($element, "o");
                    } elseif ($element[0] == 'v') {
                        $trimmed = intval(ltrim($element, "v"));
                        $equation .= $input[$trimmed];
                    } elseif ($element[0] == 'a') {
                        $trimmed = intval(ltrim($element, "a"));
                        $equation .= $answer[$trimmed - 1];
                    } else {
                        $equation .= $element;
                    }
                    /*		
                    		elseif (strpos($element,'v') === False)
                    		{ 
                    		 $equation .= $element;
                    		}
                    		else
                    		{ 
                    		 $trimmed = intval(ltrim($element, "v"));
                             $equation .= $input[$trimmed];
                    		}		 
                    */
                }
            }
            //	return $equation[3];
            //	  $equation = "mcm(2,4)";
            if (strstr($equation, "mcm")) {
                $valor = explode(",", substr($equation, 3));
                $equation_result = mcm(intval($valor[0]), intval($valor[1]));
            } elseif (strstr($equation, "smn")) {
                $valor = explode(",", substr($equation, 3));
                $equation_result = smn(intval($valor[0]), intval($valor[1]));
            } elseif (strstr($equation, "smd")) {
                $valor = explode(",", substr($equation, 3));
                $equation_result = smd(intval($valor[0]), intval($valor[1]));
            } elseif (strstr($equation, "cod")) {
                $valor = explode(",", substr($equation, 3));
                $equation_result = cod(intval($valor[0]), intval($valor[1]));
            } elseif (strstr($equation, "red")) {
                $valor = explode(",", substr($equation, 3));
                $equation_result = red(intval($valor[0]), intval($valor[1]));
            } elseif (strstr($equation, "txt")) {
                $valor = explode(",", substr($equation, 3));
                $equation_result = txt($valor[0], $valor[1]);
            } else {
                $equation_result = $m->evaluate($equation);
            }
            if ($operatorDT == "") {
                $operatorData = "";
            }
            // return strval($equation_result);
            switch ($operator) {
                case "==":
                    if ($equation_result == $value && $operatorDT == $operatorData) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                    break;
                case "!=":
                    if ($equation_result != $value && $operatorDT == $operatorData) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                    break;
                case "GT":
                    if ($equation_result > $value && $operatorDT == $operatorData) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                    break;
                case "GE":
                    if ($equation_result >= $value && $operatorDT == $operatorData) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                    break;
                case "LT":
                    if ($equation_result < $value && $operatorDT == $operatorData) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                    break;
                case "LE":
                    if ($equation_result <= $value && $operatorDT == $operatorData) {
                        return $xml->input[$id]->option[$i]->result;
                        $i = $count_options;
                    }
                    break;
            }
            // SWITCH
        }
        // else IF
    }
    // option's LOOP
}