Annotation of loncom/homework/default_homework.lcpm, revision 1.22
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:
154: sub tan { sin($_[0]) / cos($_[0]) }
1.21 harris41 155: #sub atan { atan2($_[0], 1); }
156: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
157: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ! ng 158:
1.18 albertel 159: sub log10 { log($_[0])/log(10); }
1.22 ! ng 160:
1.20 harris41 161: sub factorial {
162: my $input = int(shift);
163: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
164: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
165: return 1 if $input == 0;
166: my $result = 1;
167: for (my $i=2; $i<=$input; $i++) { $result *= $i }
168: return $result;
169: }
170:
171: sub sgn {
172: return -1 if $_[0] < 0;
173: return 0 if $_[0] == 0;
174: return 1 if $_[0] > 0;
175: }
176:
177: sub min {
178: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
179: return shift @sorted;
180: }
181:
182: sub max {
183: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
184: return pop @sorted;
185: }
1.1 harris41 186:
187: sub html {
1.4 albertel 188: if ( $external::target eq "web" ) {
189: return @_[0];
1.1 harris41 190: }
191: }
192:
1.20 harris41 193: sub roundto {
194: my ($input,$n) = @_;
195: return sprintf('%.'.$n.'f',$input);
196: }
197:
198: sub to_string {
199: my ($input,$n) = @_;
200: return sprintf('%'.$n,$input) if $n ne "";
201: return sprintf($input) if $n eq "";
202: }
203:
204: sub sub_string {
205: my ($str,$start,$len) = @_;
206: return substr($str,$start-1,$len);
207: }
1.1 harris41 208:
209: sub pow {return $_[0] ** $_[1]; }
210: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
211: sub floor {return int($_[0]); }
212:
1.2 albertel 213: sub format {
214: my ($value,$fmt)=@_;
1.11 albertel 215: return sprintf('%.'.$fmt,$value);
1.2 albertel 216: }
1.5 albertel 217:
218: sub map {
219: my $num = $#_/2;
220: my $i;
1.6 albertel 221: my @used;
222: my @permutation;
223: for ($i=0; $i < $num;) {
224: $rand = &random(1,$num,1);
225: if ( $used[$rand] == '0' ) {
226: $i++;
227: $used[$rand]='1';
228: $permutation[$i]=$rand;
229: }
230: }
1.5 albertel 231: for ( $i=1 ; $i<$num+1 ; $i++ ) {
1.6 albertel 232: ${$_[$permutation[$i]]}=$_[$i+$num]
1.5 albertel 233: }
234: }
1.22 ! ng 235:
! 236: sub name{
! 237: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
! 238: $fullname = "" if $fullname eq ", ";
! 239: return $fullname;
! 240: }
! 241:
! 242: sub student_number {
! 243: my $id = &EXT('environment.id');
! 244: $id = '' if $id eq "";
! 245: return $id;
! 246: }
! 247:
! 248: sub class {
! 249: my $course = &EXT('course.description');
! 250: $course = '' if $course eq "";
! 251: return $course;
! 252: }
! 253:
! 254: sub sec {
! 255: my $sec = &EXT('request.course.sec');
! 256: $sec = '' if $sec eq '';
! 257: return $sec;
! 258: }
! 259:
! 260: sub open_date { &EXT('resource.0.opendate') }
! 261: sub due_date { &EXT('resource.0.duedate') }
! 262: sub answer_date { &EXT('resource.0.answerdate') }
1.5 albertel 263:
264: sub choose {
265: my $num = $_[0];
266: return $_[$num];
267: }
1.6 albertel 268: #$external::randomseed=time;
1.5 albertel 269: #$a=2;
270: #$b=2;
1.6 albertel 271: #$c=2;
272: #&map(1,\$a,\$b,\$c,3,4,5);
1.5 albertel 273: #print $a."\n".$b."\n";
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>