[LON-CAPA-cvs] cvs: loncom /homework/caparesponse capa.i caparesponse.c test.pl

albertel lon-capa-cvs@mail.lon-capa.org
Tue, 16 Nov 2004 23:37:41 -0000


albertel		Tue Nov 16 18:37:41 2004 EDT

  Added files:                 
    /loncom/homework/caparesponse	test.pl 

  Modified files:              
    /loncom/homework/caparesponse	capa.i caparesponse.c 
  Log:
  - BUG#2826 $result=capa::caparesponse_get_real_response($correct_unit,$student_answer,\$scaled_answer);
          - given a unit, and a student answer, $scaled_answer will be set to the floating point number that is what capa would use to compare to the 'correct answer'
          - $result is an interger return code check capaCommon.h for possible values
  
  
Index: loncom/homework/caparesponse/capa.i
diff -u loncom/homework/caparesponse/capa.i:1.5 loncom/homework/caparesponse/capa.i:1.6
--- loncom/homework/caparesponse/capa.i:1.5	Sun Mar 21 20:43:28 2004
+++ loncom/homework/caparesponse/capa.i	Tue Nov 16 18:37:40 2004
@@ -25,9 +25,36 @@
     free(*$source);
   }
 }
+
+%typemap(perl5,in) double* scaled {
+  double *temp;
+  if (!SvROK($source)) {
+    croak("expected a reference\n");
+  }
+  SV* tempsv;
+  tempsv = SvRV($source);
+  if (!SvPOK(tempsv) && !SvIOK(tempsv) && !SvNOK(tempsv)) {
+    croak("expected a single reference to a scalar\n");
+  }
+  temp = malloc(sizeof(double));
+  *temp = SvNV(tempsv);
+  $target = temp;
+}
+
+%typemap(perl5,argout) double* scaled {
+  SV *tempsv;
+  tempsv = SvRV($arg);
+  sv_setnv(tempsv, *$source);
+  free($source);
+}
+
+
 extern int caparesponse_capa_check_answer(char *response,char *correct,
 				   int type,int tol_type,double tolerance,
 				   int sig_lbound,int sig_ubound, 
 				   char *ans_fmt, char *unit_str,
 				   int calc, char *id_list, char *pts_list,
 				   char *rndseed, char** reterror);
+
+extern int caparesponse_get_real_response(char* unit_str, char* answer,
+					  double* scaled);
Index: loncom/homework/caparesponse/caparesponse.c
diff -u loncom/homework/caparesponse/caparesponse.c:1.13 loncom/homework/caparesponse/caparesponse.c:1.14
--- loncom/homework/caparesponse/caparesponse.c:1.13	Fri Mar 12 18:46:06 2004
+++ loncom/homework/caparesponse/caparesponse.c	Tue Nov 16 18:37:40 2004
@@ -1,6 +1,6 @@
 /* The LearningOnline Network with CAPA 
  * CAPA wrapper code
- * $Id: caparesponse.c,v 1.13 2004/03/12 23:46:06 albertel Exp $
+ * $Id: caparesponse.c,v 1.14 2004/11/16 23:37:40 albertel Exp $
  *
  * Copyright Michigan State University Board of Trustees
  *
@@ -124,3 +124,58 @@
 
   return result;
 }
+
+int caparesponse_get_real_response (char* unit_str, char* answer,
+				    double* scaled) {
+  //double caparesponse_get_real_response (char* unit_str, char* answer) {
+  int     input_len,all_alphabet,idx,outcome,result;
+  double  n_part,scale=1.0,given,target;
+  char    input[ANSWER_STRING_LENG],filename[FILE_NAME_LENGTH];
+  Unit_t *ans_unit;
+  long    seed1,seed2;
+  FILE   *fp;
+  sprintf(filename,"/home/httpd/html/res/adm/includes/capa.units");
+  if ((fp=fopen(filename,"r"))==NULL) {
+    /* printf("Error: can't open %s\n",filename);*/
+    return (-1); 
+  }
+  u_getunit(fp);
+  fclose(fp);
+
+  if (unit_str != NULL && unit_str[0]!='\0') {
+    ans_unit   = u_parse_unit(unit_str);
+  } else {
+    ans_unit=NULL;
+  }
+  input_len = strlen(answer);
+  all_alphabet = 1;
+  for(idx=0;idx<input_len;idx++) {
+    if( isdigit(answer[idx]) ) {
+      all_alphabet = 0;
+    }
+  }
+  if( !all_alphabet ) {
+    outcome = split_num_unit(answer,&n_part,input,unit_str);
+    if( outcome > 1 ) { /* with both num and unit parts or only unit part */
+      if( ans_unit != NULL ) {
+	result = check_correct_unit(unit_str,ans_unit,&scale);
+      } else {
+	/* what to do when no unit is specified but student entered a unit? */
+	result = UNIT_NOTNEEDED;
+      }
+    } else {
+      if( ans_unit != NULL ) {
+	result = NO_UNIT;
+      }
+    }
+    if( (result != NO_UNIT) && (!check_for_unit_fail(result)) && ( result != UNIT_NOTNEEDED) ) {
+      given = n_part * scale;
+      *scaled=given;
+      /* convert the given answer into proper scale for units */
+    } /* end if unit check */
+  } else { /* user entered a
+lphabet, but no number */
+    result = WANTED_NUMERIC;
+  } 
+  return result;
+}

Index: loncom/homework/caparesponse/test.pl
+++ loncom/homework/caparesponse/test.pl
use strict;
use capa;

my $unit="m";
my $answer="3.4 mm";
my $scaled="3.2";
print("\n return code is (should be 6)".&capa::caparesponse_get_real_response($unit,$answer,\$scaled));
print("\nscaled (should be 0.0034) ".$scaled);
$unit="m";
$answer="3.4 mm/J";
$scaled=3.2;
print("\n return code is (should be 15)".&capa::caparesponse_get_real_response($unit,$answer,\$scaled));
print("\nscaled (should be 3.2) ".$scaled);
print("\n");