--- loncom/homework/default_homework.lcpm 2005/03/31 17:12:37 1.98 +++ loncom/homework/default_homework.lcpm 2005/11/16 22:18:22 1.104 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run() # -# $Id: default_homework.lcpm,v 1.98 2005/03/31 17:12:37 albertel Exp $ +# $Id: default_homework.lcpm,v 1.104 2005/11/16 22:18:22 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -89,11 +89,15 @@ sub caparesponse_check { } else { $message .="no ws in :$response:\n"; } - if ($type eq 'cs' || $type eq 'ci' || $type eq 'mc') { + &LONCAPA_INTERNAL_DEBUG(" type is $type "); + if ($type eq 'cs' || $type eq 'ci') { #for string answers make surec all places spaces occur, there is #really only 1 space, in both the answer and the response $answer=~s/ +/ /g; $response=~s/ +/ /g; + } elsif ($type eq 'mc') { + $answer=~s/[\s,]//g; + $response=~s/[\s,]//g; } if ($type eq 'float' && $unit=~/\$/) { if ($response!~/^\$/) { return "NO_UNIT: Missing \$ "; } @@ -189,7 +193,7 @@ sub caparesponse_check_list { my $type = $LONCAPA::CAPAresponse_args{'type'}; $result.="Got type :$type:\n"; if ($type ne '' && $#list > 0) { - (@responselist)=split /,/,$response; + (@responselist)=split(/,/,$response); } else { (@responselist)=($response); } @@ -213,18 +217,23 @@ sub caparesponse_check_list { $result.="trying answer :$list[$i]:\n"; my $thisanswer=$list[$i]; $result.="trying answer :$thisanswer:\n"; - if ($unit eq '') { - ($aresult,$msg)=&caparesponse_check($thisanswer,$responselist[$i]); + if (defined($list[$i])) { + if ($unit eq '') { + ($aresult,$msg)=&caparesponse_check($thisanswer, + $responselist[$i]); + } else { + ($aresult,$msg)=&caparesponse_check($thisanswer, + $responselist[$i]." $unit"); + } } else { - ($aresult,$msg)=&caparesponse_check($thisanswer, - $responselist[$i]." $unit"); + $aresult='ERROR'; } - my ($temp)=split /:/, $aresult; + my ($temp)=split(/:/, $aresult); $awards.="$temp,"; $result.=$aresult; push(@msgs,$msg); } - chop $awards; + chop($awards); return ("$awards:\n$result",@msgs); } @@ -447,14 +456,14 @@ sub random_negative_binomial { return @retArray; } -sub abs { abs(shift) } -sub sin { sin(shift) } -sub cos { cos(shift) } -sub exp { exp(shift) } -sub int { int(shift) } -sub log { log(shift) } -sub atan2 { atan2($_[0],$_[1]) } -sub sqrt { sqrt(shift) } +sub abs { CORE::abs(shift) } +sub sin { CORE::sin(shift) } +sub cos { CORE::cos(shift) } +sub exp { CORE::exp(shift) } +sub int { CORE::int(shift) } +sub log { CORE::log(shift) } +sub atan2 { CORE::atan2($_[0],$_[1]) } +sub sqrt { CORE::sqrt(shift) } sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) } #sub atan { atan2($_[0], 1); } @@ -518,11 +527,12 @@ sub format { #if ($options =~ /\$/) { $dollamode=1; } #if ($options =~ /,/) { $commamode=1; } if ($options =~ /\./) { $alwaysperiod=1; } + my $result; if ($fmt=~/s$/i) { $result=&format_significant_figures($value,$fmt); } else { $fmt=~s/e/E/g; - my $result=sprintf('%.'.$fmt,$value); + $result=sprintf('%.'.$fmt,$value); if ($alwaysperiod && $fmt eq '0f') { $result .='.'; } $result=~s/(E[+-]*)0/$1/; } @@ -632,12 +642,12 @@ sub prettyprint { sub commaformat { my ($number,$target) = @_; if ($number =~ /\./) { - while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) { - $number = $1.','.$2.$3; + while ($number =~ /([^0-9]*)([0-9]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) { + $number = $1.$2.','.$3.$4; } } else { - while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) { - $number = $1.','.$2.$3; + while ($number =~ /^([^0-9]*)([0-9]+)([^,][^,][^,])([,0-9]*)$/) { + $number = $1.$2.','.$3.$4; } } return $number; @@ -885,6 +895,30 @@ sub choose { return $_[$num]; } +#&sum1(1,$x,sub { &sum1($_[0],2*$_[0], sub { fact($_[0])**2 })}); +#sub sum1 { +# my ($start,$end,$sub)=@_; +# my $sum=0; +# for (my $i=$start;$i<=$end;$i++) { +# $sum+=&$sub($i); +# } +# return $sum +#} + +#&sum2('a',1,$x,'&sum2(\'b\',$a,2*$a, \'&factorial($b)**2\')'); +#sub sum2 { +# my ($varname,$start,$end,$line)=@_; +# my $sum=0; +# for (my $i=$start;$i<=$end;$i++) { +# my $func=sub { +# eval("\$".$varname."=$i"); +# eval($line); +# }; +# $sum+=&$func($i); +# } +# return $sum +#} + # expiremental idea sub proper_path { my ($path)=@_;