File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.11: download - view: text, annotated - select for diffs
Wed Nov 22 00:00:54 2000 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-added <display> like <script> but returns the result of the script
-removed SCRIPT_RESULT, <display> takes care of this.

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

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