File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.c
Revision 1.19: download - view: text, annotated - select for diffs
Thu Dec 1 22:34:10 2005 UTC (18 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4475
<formularesponse answer="35*10^3">
 with no smaples defiend now works correctly

    1: /* The LearningOnline Network with CAPA 
    2:  * CAPA wrapper code
    3:  * $Id: caparesponse.c,v 1.19 2005/12/01 22:34:10 albertel Exp $
    4:  *
    5:  * Copyright Michigan State University Board of Trustees
    6:  *
    7:  * This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8:  *
    9:  * LON-CAPA is free software; you can redistribute it and/or modify
   10:  * it under the terms of the GNU General Public License as published by
   11:  * the Free Software Foundation; either version 2 of the License, or
   12:  * (at your option) any later version.
   13:  *
   14:  * LON-CAPA is distributed in the hope that it will be useful,
   15:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   16:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17:  * GNU General Public License for more details.
   18:  *
   19:  * You should have received a copy of the GNU General Public License
   20:  * along with LON-CAPA; if not, write to the Free Software
   21:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22:  *
   23:  * /home/httpd/html/adm/gpl.txt
   24:  *
   25:  * http://www.lon-capa.org/
   26:  */
   27: 
   28: #include <capaCommon.h>
   29: #include <ranlib.h>
   30: 
   31: PointsList_t * parse_pts_list (char *pts_list) {
   32:   PointsList_t *new=NULL, *end=NULL, *beforeend=NULL, *rlist=NULL;
   33:   char *idx_pts=pts_list;
   34:   int done=0;
   35:   /*fprintf(stderr,"ids %s\n",id_list);
   36:     fprintf(stderr,"pts %s\n",pts_list);*/
   37:   while (!done && pts_list) {
   38:     int idx;
   39:     /*fprintf(stderr,"pts; %s\n",idx_pts);*/
   40:     new=gen_ptslist_str(idx_pts);
   41:     if (!new) break;
   42:     if (!rlist) { rlist=new; }
   43:     if (end) { 
   44:       end->pts_next=new; 
   45:       idx=end->pts_idx;
   46:     } else {
   47:       idx=-1;
   48:     }
   49:     end=new;
   50:     while (end) {
   51:       idx++;
   52:       end->pts_idx=idx;
   53:       /*fprintf(stderr,"end is:%d:%d:%s:%d\n",idx,end->pts_idx,end->pts_str,
   54: 	end->pts_next);*/
   55:       beforeend=end;
   56:       end=end->pts_next;
   57:     }
   58:     end=beforeend;
   59:     idx_pts=strchr(idx_pts,';');
   60:     if (idx_pts) { idx_pts++; } else { done=1; }
   61:   }
   62:   
   63:   return rlist;
   64: }
   65: 
   66: int caparesponse_capa_check_answer(char *response,char *correct,
   67: 				   int type,int tol_type,double tolerance,
   68: 				   int sig_lbound,int sig_ubound, 
   69: 				   char *ans_fmt, char *unit_str,
   70: 				   int calc, char *id_list, char *pts_list, 
   71: 				   char *rndseed, char** reterror)
   72: 				   
   73: {
   74:   long result,seed1,seed2;
   75:   Problem_t p;
   76:   char *error=NULL,filename[FILE_NAME_LENGTH];
   77:   FILE *fp;
   78: 
   79:   /* need to initialize unit parser*/
   80:   *reterror=NULL;
   81:   sprintf(filename,"/home/httpd/html/res/adm/includes/capa.units");
   82:   if ((fp=fopen(filename,"r"))==NULL) {
   83:     /* printf("Error: can't open %s\n",filename);*/
   84:       return (-1); 
   85:   }
   86:   u_getunit(fp);
   87:   fclose(fp);
   88:   /* need to setup random generator (FIXME) should only do this if 
   89:      it hasn't been yet*/
   90:   phrtsd(rndseed,&seed1,&seed2);
   91:   setall(seed1,seed2);
   92: 
   93:   /* assign_id_list and assign_pts_list exist in capaGrammerDef.y */
   94:   p.id_list=NULL;
   95:   p.pts_list=NULL;
   96: 
   97:   if (type == ANSWER_IS_FORMULA) {
   98:     p.id_list=id_list;
   99:     p.pts_list=parse_pts_list(pts_list);
  100: //  if ( p.id_list == NULL || p.pts_list == NULL) {
  101: //    return BAD_FORMULA;
  102: //  }
  103:   }
  104:   p.ans_type   = type;
  105:   p.answer     = correct;
  106:   p.tol_type   = tol_type;
  107:   p.tolerance  = tolerance;
  108:   p.sig_lbound = sig_lbound;
  109:   p.sig_ubound = sig_ubound;
  110:   
  111:   if (ans_fmt != NULL ) {
  112:     strncpy(p.ans_fmt,ans_fmt,ANSWER_STRING_LENG-1);
  113:   }
  114:   if (unit_str != NULL && unit_str[0]!='\0') {
  115:     strncpy(p.unit_str,unit_str,ANSWER_STRING_LENG-1);
  116:     //p.ans_unit = u_parse_unit(unit_str);
  117:     p.ans_unit   = parse_unit_expr(unit_str);
  118:     p.ans_unit   = process_utree(p.ans_unit);
  119:     //print_unit_t(p.ans_unit);
  120:   } else {
  121:     p.unit_str[0]='\0';
  122:     p.ans_unit=NULL;
  123:   }
  124:   p.calc       = calc;
  125: 
  126:   result=capa_check_answer(&p,response,&error);
  127:   *reterror=error;
  128:   // Caller is expected to free reterror
  129:   //  if (error!=NULL) {free(error);}
  130: 
  131:   return result;
  132: }
  133: 
  134: int caparesponse_get_real_response (char* unit_str, char* answer,
  135: 				    double* scaled) {
  136:   //double caparesponse_get_real_response (char* unit_str, char* answer) {
  137:   int     input_len,all_alphabet,idx,outcome,result;
  138:   double  n_part,scale=1.0,given,target;
  139:   char    input[ANSWER_STRING_LENG],filename[FILE_NAME_LENGTH],
  140:     tmp_unit_str[ANSWER_STRING_LENG];
  141:   Unit_t *ans_unit;
  142:   long    seed1,seed2;
  143:   FILE   *fp;
  144:   sprintf(filename,"/home/httpd/html/res/adm/includes/capa.units");
  145:   if ((fp=fopen(filename,"r"))==NULL) {
  146:     /* printf("Error: can't open %s\n",filename);*/
  147:     return (-1); 
  148:   }
  149:   u_getunit(fp);
  150:   fclose(fp);
  151: 
  152:   if (unit_str != NULL && unit_str[0]!='\0') {
  153:     ans_unit   = parse_unit_expr(unit_str);
  154:     ans_unit   = process_utree(ans_unit);
  155:   } else {
  156:     ans_unit=NULL;
  157:   }
  158:   input_len = strlen(answer);
  159:   all_alphabet = 1;
  160:   for(idx=0;idx<input_len;idx++) {
  161:     if( isdigit(answer[idx]) ) {
  162:       all_alphabet = 0;
  163:     }
  164:   }
  165:   if( !all_alphabet ) {
  166:     tmp_unit_str[0] = 0;
  167:     outcome = split_num_unit(answer,&n_part,input,tmp_unit_str);
  168:     if( outcome > 1 ) { /* with both num and unit parts or only unit part */
  169:       if( ans_unit != NULL ) {
  170: 	result = check_correct_unit(tmp_unit_str,ans_unit,&scale);
  171:       } else {
  172: 	/* what to do when no unit is specified but student entered a unit? */
  173: 	result = UNIT_NOTNEEDED;
  174:       }
  175:     } else {
  176:       if( ans_unit != NULL ) {
  177: 	result = NO_UNIT;
  178:       }
  179:     }
  180:     if( (result != NO_UNIT) && (!check_for_unit_fail(result)) && ( result != UNIT_NOTNEEDED) ) {
  181:       given = n_part * scale;
  182:       *scaled=given;
  183:       /* convert the given answer into proper scale for units */
  184:     } /* end if unit check */
  185:   } else { /* user entered a
  186: lphabet, but no number */
  187:     result = WANTED_NUMERIC;
  188:   } 
  189:   return result;
  190: }
  191: 
  192: /* Testing harnass
  193: int main(void) {
  194:   int result=0;
  195:   char *reterror=NULL;
  196:   result= caparesponse_capa_check_answer("10^3","1000",
  197: 					 ANSWER_IS_FORMULA,
  198: 					 TOL_ABSOLUTE,1E-3,
  199: 					 3,5,NULL,NULL,
  200: 					 CALC_UNFORMATED,
  201: 					 "","4",
  202: 					 "rndseed",
  203: 					 &reterror);
  204:   fprintf(stderr,"result %d\nreterror: %s\n",result,reterror);
  205: }
  206: */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>