From: dpvc v. a. <we...@ma...> - 2005-02-15 22:15:53
|
Log Message: ----------- Updated the answer checkers so that you can more easily specify how the correct answer shoudl be displayed. In the past, you could use something like Real(sqrt(2))->cmp(correct_ans=>"sqrt(2)") to do this, but that is awkward. Now the Compute() function (which parses and then evaluates a string) sets things up so that the original string will be what is used as the correct answer. That means Compute("sqrt(2)")->cmp will have the same result as the example above. You can also set the {correct_ans} properly of any Parser object to have that value used as the correct answer. For example $x = Real(sqrt(2)); $x->{correct_ans} = "sqrt(2)"; ANS($x->cmp) would also produce the same answer checker as the two previous examples. All three methods should work. Use the one that is most convenient for you. Modified Files: -------------- pg/macros: Parser.pl pg/lib/Value: AnswerChecker.pm Revision Data ------------- Index: Parser.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/Parser.pl,v retrieving revision 1.4 retrieving revision 1.5 diff -Lmacros/Parser.pl -Lmacros/Parser.pl -u -r1.4 -r1.5 --- macros/Parser.pl +++ macros/Parser.pl @@ -30,8 +30,10 @@ # Parse a formula and evaluate it # sub Compute { - my $formula = Formula(shift); - return $formula->eval(@_); + my $string = shift; + my $formula = Formula($string)->eval(@_); + $formula->{correct_ans} = $string; + return $formula; } # Index: AnswerChecker.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/AnswerChecker.pm,v retrieving revision 1.38 retrieving revision 1.39 diff -Llib/Value/AnswerChecker.pm -Llib/Value/AnswerChecker.pm -u -r1.38 -r1.39 --- lib/Value/AnswerChecker.pm +++ lib/Value/AnswerChecker.pm @@ -26,9 +26,11 @@ sub cmp { my $self = shift; my $ans = new AnswerEvaluator; + my $correct = $self->{correct_ans}; + $correct = $self->string unless defined($correct); $ans->ans_hash( type => "Value (".$self->class.")", - correct_ans => protectHTML($self->string), + correct_ans => protectHTML($correct), correct_value => $self, $self->cmp_defaults, @_ @@ -489,7 +491,8 @@ my $cmp = $self->SUPER::cmp(@_); if ($cmp->{rh_ans}{removeParens}) { $self->{open} = $self->{close} = ''; - $cmp->ans_hash(correct_ans => $self->stringify); + $cmp->ans_hash(correct_ans => $self->stringify) + unless defined($self->{correct_ans}); } return $cmp; } @@ -703,7 +706,8 @@ my $cmp = $self->SUPER::cmp(@_); if ($cmp->{rh_ans}{removeParens} && $self->type eq 'List') { $self->{tree}{open} = $self->{tree}{close} = ''; - $cmp->ans_hash(correct_ans => $self->stringify); + $cmp->ans_hash(correct_ans => $self->stringify) + unless defined($self->{correct_ans}); } if ($cmp->{rh_ans}{eval} && $self->isConstant) { $cmp->ans_hash(correct_value => $self->eval); |