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=''; #done lowerbnd,upperbnd
15: my $sig_lbound=''; #done
16: my $sig_ubound=''; #done
17: my $ans_fmt='';
18: my $units=''; #done
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: if ($unit eq '') {
97: $aresult=&caparesponse_check($responselist[$i],
98: $expr.';my $answer='.$list[$i].';');
99: } else {
100: $aresult=&caparesponse_check($responselist[$i]." $unit",
101: $expr.';my $answer='.$list[$i].';');
102: }
103: my ($temp)=split /:/, $aresult;
104: $awards.="$temp,";
105: $result.=$aresult;
106: }
107: chop $awards;
108: return "$awards:<br />$result";
109: }
110:
111: sub tex {
112: if ( $external::target eq "tex" ) {
113: return @_[0];
114: } else {
115: return @_[1];
116: }
117: }
118:
119: sub web {
120: if ( $external::target eq "tex" ) {
121: return @_[1];
122: } else {
123: if ( $external::target eq "web") {
124: return @_[0];
125: } else {
126: return @_[2];
127: }
128: }
129: }
130:
131: sub problem {
132: return '1';
133: }
134:
135: sub hinton {
136: return 0;
137: }
138:
139: sub random {
140: my ($start,$end,$step)=@_;
141: if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
142: my $num=1+int(($end-$start)/$step);
143: my $result=$start + int(rand() * $num)*$step;
144: return $result;
145: }
146:
147: sub tan { sin($_[0]) / cos($_[0]) }
148: sub atan { atan2($_[0], 1); }
149: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
150: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
151: sub log10 { log($_[0])/log(10); }
152:
153: sub html {
154: if ( $external::target eq "web" ) {
155: return @_[0];
156: }
157: }
158:
159:
160: sub pow {return $_[0] ** $_[1]; }
161: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
162: sub floor {return int($_[0]); }
163:
164: sub format {
165: my ($value,$fmt)=@_;
166: return sprintf('%.'.$fmt,$value);
167: }
168:
169: sub map {
170: my $num = $#_/2;
171: my $i;
172: my @used;
173: my @permutation;
174: for ($i=0; $i < $num;) {
175: $rand = &random(1,$num,1);
176: if ( $used[$rand] == '0' ) {
177: $i++;
178: $used[$rand]='1';
179: $permutation[$i]=$rand;
180: }
181: }
182: for ( $i=1 ; $i<$num+1 ; $i++ ) {
183: ${$_[$permutation[$i]]}=$_[$i+$num]
184: }
185: }
186:
187: sub choose {
188: my $num = $_[0];
189: return $_[$num];
190: }
191: #$external::randomseed=time;
192: #$a=2;
193: #$b=2;
194: #$c=2;
195: #&map(1,\$a,\$b,\$c,3,4,5);
196: #print $a."\n".$b."\n";
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>