--- loncom/homework/default_homework.lcpm 2006/06/13 14:57:54 1.107 +++ loncom/homework/default_homework.lcpm 2006/07/18 14:06:17 1.111 @@ -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.107 2006/06/13 14:57:54 albertel Exp $ +# $Id: default_homework.lcpm,v 1.111 2006/07/18 14:06:17 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -152,8 +152,12 @@ sub caparesponse_check { my $reterror=""; my $result; if ($type eq '9') { - $result = &maxima_check($response,$answer,\$reterror); + $result = &maxima_check(&maxima_cas_formula_fix($response),&maxima_cas_formula_fix($answer),\$reterror); } else { + if ($type eq '8') { # fml type + $response = &capa_formula_fix($response); + $answer = &capa_formula_fix($answer); + } $result = &caparesponse_capa_check_answer($response,$answer,$type, $tol_type,$tol, $sig_lbound,$sig_ubound, @@ -184,6 +188,36 @@ sub caparesponse_check { return ("$result:\nRetError $reterror:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message",$reterror); } +sub maxima_cas_formula_fix { + my ($expression)=@_; + return &implicit_multiplication($expression); +} + +sub capa_formula_fix { + my ($expression)=@_; + return &implicit_multiplication($expression); +} + +sub implicit_multiplication { + my ($expression)=@_; +# Escape scientific notation, so 3e8 does not become 3*e*8 +# 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8 + $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi; +# 3x10^8 -> 3&8; 3*10^-8 -> 3&-8 + $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi; +# Fill in multiplication signs +# a b -> a*b + $expression=~s/\s+/\*/gs; +# 3a -> 3*a; 3( -> 3*(; 3A -> 3*a + $expression=~s/(\d)([a-zA-Z\(])/$1\*$2/gs; +# a3 -> a*3; )3 -> )*3 + $expression=~s/([a-zA-Z])(\d)/$1\*$2/gs; +# )a -> )*a + $expression=~s/\)(\w)/\)\*$1/gs; +# 3&8 -> 3e8; 3&-4 -> 3e-4 + $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs; + return $expression; +} sub caparesponse_check_list { my $response=$LONCAPA::CAPAresponse_args{'response'};