Annotation of loncom/homework/default_homework.lcpm, revision 1.23
1.1 harris41 1: # file name (temp): default_homework
2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
3: #
4: #
1.16 albertel 5: #init some globals
1.20 harris41 6: #
7: # Guy Albertelli
8: #
9: # 05/25/2001 H. K. Ng
10: #
1.3 albertel 11: $RANDOMINIT=0;
1.22 ng 12: $pi=atan2(1,1)*4;
13: $rad2deg=180.0/$pi;
14: $deg2rad=$pi/180.0;
1.3 albertel 15:
1.7 albertel 16: sub caparesponse_check {
1.10 albertel 17: my $answer=''; #done
18: my $type=''; #done
19: my $tol_type=''; # gets it's value from whether tol has a % or not done
20: my $tol=''; #done
1.19 albertel 21: my $sig=''; #done lowerbnd,upperbnd
22: my $sig_lbound=''; #done
23: my $sig_ubound=''; #done
1.7 albertel 24: my $ans_fmt='';
1.19 albertel 25: my $units=''; #done
1.7 albertel 26: my $calc='';
27: my ($response,$expr)=@_;
28:
1.12 albertel 29: ($answer,$type,$tol,$sig,$ans_fmt,
30: $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
1.8 albertel 31: #type's definitons come from capaParser.h
32: if ($type eq '' ) {
33: if ( $answer eq ($answer *1.0)) { $type = 2;
34: } else { $type = 3; }
35: } else {
36: if ($type eq 'cs') { $type = 4;
37: } elsif ($type eq 'ci') { $type = 3;
38: } elsif ($type eq 'mc') { $type = 5;
39: } elsif ($type eq 'fml') { $type = 8;
1.9 albertel 40: } elsif ($type eq 'subj') { $type = 7;
41: } else { return "ERROR: Unknown type of answer: $type" }
1.8 albertel 42: }
43:
1.10 albertel 44: if ($tol eq '') {
45: $tol=0.0;
46: $tol_type=1; #TOL_ABSOLUTE
47: } else {
48: if ($tol =~ /%$/) {
1.12 albertel 49: chop $tol;
1.10 albertel 50: $tol_type=2; #TOL_PERCENTAGE
51: } else {
52: $tol_type=1; #TOL_ABSOLUTE
53: }
54: }
1.12 albertel 55:
56: if ($sig eq '') {
57: $sig_lbound = 0; #SIG_LB_DEFAULT
58: $sig_ubound =15; #SIG_UB_DEFAULT
59: } else {
60: ($sig_lbound,$sig_ubound) = split /,/,$sig;
61: }
1.7 albertel 62: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10 albertel 63: $tol_type,$tol,
1.7 albertel 64: $sig_lbound,$sig_ubound,
1.12 albertel 65: $ans_fmt,$units,$calc);
1.9 albertel 66:
67: if ($result == '1') { $result='EXACT_ANS'; }
68: elsif ($result == '2') { $result='APPROX_ANS'; }
69: elsif ($result == '3') { $result='SIG_FAIL'; }
70: elsif ($result == '4') { $result='UNIT_FAIL'; }
71: elsif ($result == '5') { $result='NO_UNIT'; }
72: elsif ($result == '6') { $result='UNIT_OK'; }
73: elsif ($result == '7') { $result='INCORRECT'; }
74: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
75: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
76: elsif ($result =='10') { $result='SUB_RECORDED'; }
77: elsif ($result =='11') { $result='BAD_FORMULA'; }
78: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.13 albertel 79: else {$result = "ERROR: Unknown Result:$result:$@:";}
1.9 albertel 80:
1.17 albertel 81: return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br />$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br />";
1.14 albertel 82: }
83:
84: sub caparesponse_check_list {
85: my ($response,$expr)=@_;
86: my $result='';
1.15 albertel 87: my $aresult='';
1.14 albertel 88: my $current_answer;
89: my $answer = eval $expr.';return $answer';
90: my (@list) = eval $answer;
91: my (@responselist)=split /,/,$response;
1.15 albertel 92: my $unit='';
1.17 albertel 93: $result.="Final response :$responselist['-1']:<br />";
1.15 albertel 94: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
95: $responselist['-1']=$1;
96: $unit=$2;
97: }
1.17 albertel 98: $result.="Final response :$responselist['-1']:<br />";
1.14 albertel 99: $unit=~s/\s//;
100: my $i=0;
101: my $awards='';
102: for ($i=0; $i<@list;$i++) {
1.19 albertel 103: if ($unit eq '') {
104: $aresult=&caparesponse_check($responselist[$i],
105: $expr.';my $answer='.$list[$i].';');
106: } else {
107: $aresult=&caparesponse_check($responselist[$i]." $unit",
108: $expr.';my $answer='.$list[$i].';');
109: }
1.15 albertel 110: my ($temp)=split /:/, $aresult;
1.14 albertel 111: $awards.="$temp,";
1.15 albertel 112: $result.=$aresult;
1.14 albertel 113: }
114: chop $awards;
1.17 albertel 115: return "$awards:<br />$result";
1.7 albertel 116: }
117:
1.4 albertel 118: sub tex {
119: if ( $external::target eq "tex" ) {
120: return @_[0];
121: } else {
122: return @_[1];
123: }
124: }
125:
126: sub web {
127: if ( $external::target eq "tex" ) {
128: return @_[1];
129: } else {
130: if ( $external::target eq "web") {
131: return @_[0];
132: } else {
133: return @_[2];
134: }
135: }
136: }
137:
138: sub problem {
1.11 albertel 139: return '1';
1.4 albertel 140: }
141:
1.1 harris41 142: sub hinton {
143: return 0;
144: }
145:
146: sub random {
147: my ($start,$end,$step)=@_;
1.3 albertel 148: if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
1.1 harris41 149: my $num=1+int(($end-$start)/$step);
150: my $result=$start + int(rand() * $num)*$step;
151: return $result;
152: }
153:
1.23 ! ng 154: sub abs { abs(shift) }
! 155: sub sin { sin(shift) }
! 156: sub cos { cos(shift) }
! 157: sub exp { exp(shift) }
! 158: sub int { int(shift) }
! 159: sub log { log(shift) }
! 160: sub atan2 { atan2($_[0],$_[1]) }
! 161: sub sqrt { sqrt(shift) }
! 162:
1.1 harris41 163: sub tan { sin($_[0]) / cos($_[0]) }
1.21 harris41 164: #sub atan { atan2($_[0], 1); }
165: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
166: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ng 167:
1.18 albertel 168: sub log10 { log($_[0])/log(10); }
1.22 ng 169:
1.20 harris41 170: sub factorial {
171: my $input = int(shift);
172: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
173: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
174: return 1 if $input == 0;
175: my $result = 1;
176: for (my $i=2; $i<=$input; $i++) { $result *= $i }
177: return $result;
178: }
179:
180: sub sgn {
181: return -1 if $_[0] < 0;
182: return 0 if $_[0] == 0;
183: return 1 if $_[0] > 0;
184: }
185:
186: sub min {
187: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
188: return shift @sorted;
189: }
190:
191: sub max {
192: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
193: return pop @sorted;
194: }
1.1 harris41 195:
196: sub html {
1.4 albertel 197: if ( $external::target eq "web" ) {
198: return @_[0];
1.1 harris41 199: }
200: }
201:
1.20 harris41 202: sub roundto {
203: my ($input,$n) = @_;
204: return sprintf('%.'.$n.'f',$input);
205: }
206:
207: sub to_string {
208: my ($input,$n) = @_;
209: return sprintf('%'.$n,$input) if $n ne "";
210: return sprintf($input) if $n eq "";
211: }
212:
213: sub sub_string {
214: my ($str,$start,$len) = @_;
215: return substr($str,$start-1,$len);
216: }
1.1 harris41 217:
218: sub pow {return $_[0] ** $_[1]; }
219: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
220: sub floor {return int($_[0]); }
221:
1.2 albertel 222: sub format {
223: my ($value,$fmt)=@_;
1.11 albertel 224: return sprintf('%.'.$fmt,$value);
1.2 albertel 225: }
1.5 albertel 226:
227: sub map {
228: my $num = $#_/2;
229: my $i;
1.6 albertel 230: my @used;
231: my @permutation;
232: for ($i=0; $i < $num;) {
233: $rand = &random(1,$num,1);
234: if ( $used[$rand] == '0' ) {
235: $i++;
236: $used[$rand]='1';
237: $permutation[$i]=$rand;
238: }
239: }
1.5 albertel 240: for ( $i=1 ; $i<$num+1 ; $i++ ) {
1.6 albertel 241: ${$_[$permutation[$i]]}=$_[$i+$num]
1.5 albertel 242: }
243: }
1.22 ng 244:
1.23 ! ng 245: sub capa_id { return }
! 246:
! 247: sub problem { return }
! 248:
1.22 ng 249: sub name{
250: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
251: $fullname = "" if $fullname eq ", ";
1.23 ! ng 252: $fullname =~ s/\%2d/-/;
1.22 ng 253: return $fullname;
254: }
255:
256: sub student_number {
257: my $id = &EXT('environment.id');
258: $id = '' if $id eq "";
259: return $id;
260: }
261:
262: sub class {
263: my $course = &EXT('course.description');
264: $course = '' if $course eq "";
265: return $course;
266: }
267:
268: sub sec {
269: my $sec = &EXT('request.course.sec');
1.23 ! ng 270: $sec = '' if $sec eq "";
1.22 ng 271: return $sec;
272: }
273:
1.23 ! ng 274: sub open_date {
! 275: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
! 276: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
! 277: }
! 278:
! 279: sub due_date {
! 280: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
! 281: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
! 282: }
! 283:
! 284: sub answer_date {
! 285: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
! 286: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
! 287: }
1.5 albertel 288:
289: sub choose {
290: my $num = $_[0];
291: return $_[$num];
292: }
1.23 ! ng 293:
1.6 albertel 294: #$external::randomseed=time;
1.5 albertel 295: #$a=2;
296: #$b=2;
1.6 albertel 297: #$c=2;
298: #&map(1,\$a,\$b,\$c,3,4,5);
1.5 albertel 299: #print $a."\n".$b."\n";
1.23 ! ng 300:
! 301:
! 302:
! 303:
! 304:
! 305:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>