##########################################################
#
# Example showing an answer checker that uses the parser
# to evaluate the student (and professor's) answers.
#
# This is now obsolete, as the paser's ->cmp method
# can be used to produce an answer checker for any
# of the parser types.
#
DOCUMENT(); # This should be the first executable line in the problem.
loadMacros(
"PGbasicmacros.pl",
"PGanswermacros.pl",
"Parser.pl",
"parserUtils.pl",
);
TEXT(beginproblem());
##########################################################
#
# Use Vector context
#
Context('Vector');
##########################################################
#
# Make the answer checker
#
sub vector_cmp {
my $v = shift;
die "vector_cmp requires a vector argument" unless defined $v;
my $v = Vector($v); # covert to vector if it isn't already
my $ans = new AnswerEvaluator;
$ans->ans_hash(type => "vector",correct_ans => $v->string, vector=>$v);
$ans->install_evaluator(~~&vector_cmp_check);
return $ans;
}
sub vector_cmp_check {
my $ans = shift; my $v = $ans->{vector},
$ans->score(0); # assume failure
my $f = Parser::Formula($ans->{student_ans});
my $V = Parser::Evaluate($f);
if (defined $V) {
$V = Formula($V) unless Value::isValue($V); # make sure we can call Value methods
$ans->{preview_latex_string} = $f->TeX;
$ans->{preview_text_string} = $f->string;
$ans->{student_ans} = $V->string;
if ($V->type eq 'Vector') {
$ans->score(1) if ($V == $v); # Let the overloaded == do the check
} else {
$ans->{ans_message} = $ans->{error_message} =
"Your answer doesn't seem to be a Vector (it looks like ".Value::showClass($V).")"
unless $inputs_ref->{previewAnswers};
}
} else {
#
# Student answer evaluation failed.
# Report the error, with formatting, if possible.
#
my $context = Context();
my $message = $context->{error}{message};
if ($context->{error}{pos}) {
my $string = $context->{error}{string};
my ($s,$e) = @{$context->{error}{pos}};
$message =~ s/; see.*//; # remove the position from the message
$ans->{student_ans} = protectHTML(substr($string,0,$s)) .
'' .
protectHTML(substr($string,$s,$e-$s)) .
'' .
protectHTML(substr($string,$e));
}
$ans->{ans_message} = $ans->{error_message} = $message;
}
return $ans;
}
##########################################################
#
# The problem text
#
$V = Vector(1,2,3);
Context()->flags->set(ijk=>0);
Context()->constants->add(a=>1,b=>1,c=>1);
$ABC = Formula("");
BEGIN_TEXT
Enter the vector \(\{$V->TeX\}\) in any way you like: \{ans_rule(20)\}.
$PAR
You can use either \(\{$ABC->TeX\}\) or \(\{$ABC->ijk\}\) notation,$BR
and can perform vector operations to produce your answer.
$PAR
${BBOLD}Note:${EBOLD} This problem is obsolete.
END_TEXT
###########################################################
#
# The answer
#
ANS(vector_cmp($V));
###########################################################
ENDDOCUMENT(); # This should be the last executable line in the problem.