- NumberConversionTask: staticCheck() sign handling with optional leading zeros fixed

This commit is contained in:
Wiesner András 2025-10-12 23:16:35 +02:00
parent 0625aab454
commit cf8c7a3e21

View File

@ -76,20 +76,31 @@ class NumberConversionTask extends OpenEndedTask
$this->src_rep = "u"; $this->src_rep = "u";
} }
// switch between source and destination constrains
if ($this->hasFlag("sourceconstrain")) {
$base = $this->src_base;
$n_digits = $this->src_n_digits;
$rep = $this->src_rep;
} else {
$base = $this->dst_base;
$n_digits = $this->dst_n_digits;
$rep = $this->dst_rep;
}
// specify the range // specify the range
$max = 1; $max = 1;
$min = 0; $min = 0;
switch ($this->dst_rep) { switch ($rep) {
case "u": case "u":
$max = pow($this->dst_base, $this->dst_n_digits) - 1; $max = pow($base, $n_digits) - 1;
$min = 0; $min = 0;
break; break;
case "s": case "s":
$max = pow($this->dst_base, $this->dst_n_digits) - 1; $max = pow($base, $n_digits) - 1;
$min = -$max; $min = -$max;
break; break;
case "c": case "c":
$max = pow($this->dst_base, $this->dst_n_digits - 1) - 1; $max = pow($base, $n_digits - 1) - 1;
$min = -($max + 1); $min = -($max + 1);
break; break;
} }
@ -106,11 +117,17 @@ class NumberConversionTask extends OpenEndedTask
{ {
$mark = 0.0; $mark = 0.0;
$pa = strtolower($this->player_answer); $pa = strtolower($this->player_answer);
if ($this->hasFlag("acceptwithoutleadingzeros")) { $ca = $this->correct_answer;
$mark = (ltrim($pa, " 0") === ltrim($this->correct_answer, "0")) ? $this->getMaxMark() : 0.0; if (($ca[0] === "-") && ($pa[0] !== "-")) {
} else { goto setmark;
$mark = (trim($pa) === trim($this->correct_answer)) ? $this->getMaxMark() : 0.0;
} }
if ($this->hasFlag("acceptwithoutleadingzeros")) {
$mark = (ltrim($pa, "+- 0") === ltrim($ca, "-0")) ? $this->getMaxMark() : 0.0;
} else {
$mark = (trim($pa) === trim($ca)) ? $this->getMaxMark() : 0.0;
}
setmark:
$this->setMark($mark); $this->setMark($mark);
} }
} }