File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.16: download - view: text, annotated - select for diffs
Wed Jan 24 17:01:49 2001 UTC (23 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- setup $pi

    1: # file name (temp): default_homework 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: # 
    4: #
    5: #init some globals
    6: $RANDOMINIT=0;
    7: $pi=atan2(1,1)*4;
    8: 
    9: sub caparesponse_check {
   10:   my $answer='';  #done
   11:   my $type='';    #done
   12:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   13:   my $tol='';     #done
   14:   my $sig='';
   15:   my $sig_lbound='';
   16:   my $sig_ubound='';
   17:   my $ans_fmt='';
   18:   my $units='';
   19:   my $calc='';
   20:   my ($response,$expr)=@_;
   21:   
   22:   ($answer,$type,$tol,$sig,$ans_fmt,
   23:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
   24:   #type's definitons come from capaParser.h
   25:   if ($type eq '' ) {
   26:     if ( $answer eq ($answer *1.0)) { $type = 2;
   27:     } else { $type = 3; }
   28:   } else {
   29:          if ($type eq 'cs')    { $type = 4;
   30:     } elsif ($type eq 'ci')    { $type = 3;
   31:     } elsif ($type eq 'mc')    { $type = 5;
   32:     } elsif ($type eq 'fml')   { $type = 8;
   33:     } elsif ($type eq 'subj')  { $type = 7;
   34:     } else { return "ERROR: Unknown type of answer: $type" }
   35:   }
   36: 
   37:   if ($tol eq '') {
   38:     $tol=0.0;
   39:     $tol_type=1; #TOL_ABSOLUTE
   40:   } else {
   41:     if ($tol =~ /%$/) {
   42:       chop $tol;
   43:       $tol_type=2; #TOL_PERCENTAGE
   44:     } else {
   45:       $tol_type=1; #TOL_ABSOLUTE
   46:     }
   47:   }
   48: 
   49:   if ($sig eq '') {
   50:     $sig_lbound = 0; #SIG_LB_DEFAULT
   51:     $sig_ubound =15; #SIG_UB_DEFAULT
   52:   } else {
   53:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   54:   }
   55:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   56: 					       $tol_type,$tol,
   57: 					       $sig_lbound,$sig_ubound,
   58: 					       $ans_fmt,$units,$calc);
   59: 
   60:   if    ($result == '1') { $result='EXACT_ANS'; } 
   61:   elsif ($result == '2') { $result='APPROX_ANS'; }
   62:   elsif ($result == '3') { $result='SIG_FAIL'; }
   63:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   64:   elsif ($result == '5') { $result='NO_UNIT'; }
   65:   elsif ($result == '6') { $result='UNIT_OK'; }
   66:   elsif ($result == '7') { $result='INCORRECT'; }
   67:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   68:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   69:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   70:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   71:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   72:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   73: 
   74:   return "$result:<br>Error $error:<br>Answer $answer:<br>Response $response:<br>$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br>";
   75: }
   76: 
   77: sub caparesponse_check_list {
   78:   my ($response,$expr)=@_;
   79:   my $result='';
   80:   my $aresult='';
   81:   my $current_answer;
   82:   my $answer = eval $expr.';return $answer';
   83:   my (@list) = eval $answer;
   84:   my (@responselist)=split /,/,$response;
   85:   my $unit='';
   86:   $result.="Final response :$responselist['-1']:<br>";
   87:   if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
   88:     $responselist['-1']=$1;
   89:     $unit=$2;
   90:   }
   91:   $result.="Final response :$responselist['-1']:<br>";
   92:   $unit=~s/\s//;
   93:   my $i=0;
   94:   my $awards='';
   95:   for ($i=0; $i<@list;$i++) {
   96:     $aresult=&caparesponse_check($responselist[$i]." $unit",
   97: 				 $expr.';my $answer='.$list[$i].';');
   98:     my ($temp)=split /:/, $aresult;
   99:     $awards.="$temp,";
  100:     $result.=$aresult;
  101:   }
  102:   chop $awards;
  103:   return "$awards:<br>$result";
  104: }
  105: 
  106: sub tex {
  107:   if ( $external::target eq "tex" ) {
  108:     return @_[0];
  109:   } else {
  110:     return @_[1];
  111:   }
  112: }
  113: 
  114: sub web {
  115:   if ( $external::target eq "tex" ) {
  116:     return @_[1];
  117:   } else {
  118:     if ( $external::target eq "web") {
  119:       return @_[0];
  120:     } else {
  121:       return @_[2];
  122:     }
  123:   }
  124: }
  125: 
  126: sub problem {
  127:   return '1';
  128: }
  129: 
  130: sub hinton {
  131:   return 0;
  132: }
  133: 
  134: sub random {
  135:   my ($start,$end,$step)=@_;
  136:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  137:   my $num=1+int(($end-$start)/$step);
  138:   my $result=$start + int(rand() * $num)*$step;
  139:   return $result;
  140: }
  141: 
  142: sub tan  { sin($_[0]) / cos($_[0]) }
  143: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  144: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  145: 
  146: sub html {
  147:   if ( $external::target eq "web" ) {
  148:     return @_[0];
  149:   }
  150: }
  151: 
  152: 
  153: sub pow   {return $_[0] ** $_[1]; }
  154: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
  155: sub floor {return int($_[0]);  }
  156: 
  157: sub format {
  158:   my ($value,$fmt)=@_;
  159:   return sprintf('%.'.$fmt,$value);
  160: }
  161: 
  162: sub map {
  163:   my $num = $#_/2;
  164:   my $i;
  165:   my @used;
  166:   my @permutation;
  167:   for ($i=0; $i < $num;) {
  168:     $rand = &random(1,$num,1);
  169:     if ( $used[$rand] == '0' ) {
  170:       $i++;
  171:       $used[$rand]='1';
  172:       $permutation[$i]=$rand;
  173:     }
  174:   }
  175:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
  176:     ${$_[$permutation[$i]]}=$_[$i+$num]
  177:   }
  178: }
  179: 
  180: sub choose {
  181:   my $num = $_[0];
  182:   return $_[$num];
  183: }
  184: #$external::randomseed=time;
  185: #$a=2;
  186: #$b=2;
  187: #$c=2;
  188: #&map(1,\$a,\$b,\$c,3,4,5);
  189: #print $a."\n".$b."\n";

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>