Diff for /capa/capa51/pProj/capaCommon.c between versions 1.6 and 1.17

version 1.6, 2000/02/04 21:29:20 version 1.17, 2000/10/24 22:17:19
Line 1 Line 1
   /* Library of useful functions
      Copyright (C) 1992-2000 Michigan State University
   
      The CAPA system is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License as
      published by the Free Software Foundation; either version 2 of the
      License, or (at your option) any later version.
   
      The CAPA system is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      General Public License for more details.
   
      You should have received a copy of the GNU General Public
      License along with the CAPA system; see the file COPYING.  If not,
      write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
   
      As a special exception, you have permission to link this program
      with the TtH/TtM library and distribute executables, as long as you
      follow the requirements of the GNU GPL in regard to all of the
      software in the executable aside from TtH/TtM.
   */
   
 /* =||>|===================== capaCommon.c =====================|<||= */  /* =||>|===================== capaCommon.c =====================|<||= */
 /* created 1994 by Isaac Tsai                                         */  /* created 1994 by Isaac Tsai                                         */
 /* 1994, 1995, 1996, 1997, 1998, 1999  copyrighted by Isaac Tsai      */  
 /* TODO: restructure capa_check_ans*() calls into one                 */  /* TODO: restructure capa_check_ans*() calls into one                 */
 /* =||>|===================== capaCommon.c =====================|<||= */  /* =||>|===================== capaCommon.c =====================|<||= */
 #include <ctype.h>  #include <ctype.h>
Line 25  int     yyparse(); Line 48  int     yyparse();
 extern  FILE *yyin;  extern  FILE *yyin;
 extern  void yyrestart();  extern  void yyrestart();
   
   extern  FILE *dfp;
   
 /*----------------------------------------------------------*/  /*----------------------------------------------------------*/
 /*  RETURN: -1 file error                                   */  /*  RETURN: -1 file error                                   */
 /*           0 success                                      */  /*           0 success                                      */
Line 329  long       offset; Line 354  long       offset;
    /* entry->answers*3 == entry->tries, + fudge factor*/     /* entry->answers*3 == entry->tries, + fudge factor*/
    a_line=capa_malloc(strlen(entry->tries)*5+MAX_STUDENT_NUMBER,1);     a_line=capa_malloc(strlen(entry->tries)*5+MAX_STUDENT_NUMBER,1);
    sprintf(a_line,"%s %s,%s\n",entry->student_number,entry->answers,entry->tries);     sprintf(a_line,"%s %s,%s\n",entry->student_number,entry->answers,entry->tries);
    printf("Setting:%s\n",a_line);/*TEST*/  
    new_len = strlen(a_line);     new_len = strlen(a_line);
    sprintf(fmtbuf, "%%%dc",MAX_STUDENT_NUMBER);     sprintf(fmtbuf, "%%%dc",MAX_STUDENT_NUMBER);
    flockstream(fp);  /* <==== lock the setX.db file */     flockstream(fp);  /* <==== lock the setX.db file */
Line 597  int        set; Line 621  int        set;
      ans_p[length]='\0';       ans_p[length]='\0';
      sprintf(fmtbuf, "%%%dc",(3*length-1));       sprintf(fmtbuf, "%%%dc",(3*length-1));
      sscanf(oneline + MAX_STUDENT_NUMBER+1+length+1,fmtbuf,tries_p);       sscanf(oneline + MAX_STUDENT_NUMBER+1+length+1,fmtbuf,tries_p);
      tries_p[3*length-1];       tries_p[3*length-1]='\0';
      entry->answers = ans_p;       entry->answers = ans_p;
      entry->tries   = tries_p;       entry->tries   = tries_p;
      entry->e_probs = nq;       entry->e_probs = nq;
Line 608  int        set; Line 632  int        set;
        }         }
        ans_p[nq]='\0';tries_p[3*(nq)+2]='\0';tries_p[3*length-1]=',';         ans_p[nq]='\0';tries_p[3*(nq)+2]='\0';tries_p[3*length-1]=',';
      }       }
      printf("Got:%s:%s:\n",ans_p,tries_p);/*TEST*/  
    }     }
    return (offset);     return (offset);
 }  }
Line 2126  extern  int        Symb_count; Line 2149  extern  int        Symb_count;
 extern  int        first_run;  extern  int        first_run;
 extern  int        Stop_Parser;  extern  int        Stop_Parser;
 extern  void       (*Status_Func)();  extern  void       (*Status_Func)();
   #ifdef TTH
   extern void tth_restart();
   extern char* tth_err;
   #endif /*TTH*/
   long  seed1,     seed2;    long  seed1,     seed2;
   T_student        a_student;    T_student        a_student;
   char            *class, *classname, warn_msg[WARN_MSG_LENGTH];    char            *class, *classname, warn_msg[WARN_MSG_LENGTH];
Line 2154  extern  void       (*Status_Func)(); Line 2181  extern  void       (*Status_Func)();
   }    }
   u_getunit(fp);    u_getunit(fp);
   fclose(fp);    fclose(fp);
   #ifdef TTH
     if(tth_err) { free(tth_err); tth_err=NULL; }
     tth_restart();
   #endif /*TTH*/
   strncpy(Parse_name,a_student.s_nm,MAX_NAME_CHAR+1);    strncpy(Parse_name,a_student.s_nm,MAX_NAME_CHAR+1);
   strncpy(Parse_student_number,student_number,MAX_STUDENT_NUMBER+1);    strncpy(Parse_student_number,student_number,MAX_STUDENT_NUMBER+1);
   Parse_section = a_student.s_sec;    Parse_section = a_student.s_sec;
Line 2614  answers_string(mode, p)int mode;Problem_ Line 2645  answers_string(mode, p)int mode;Problem_
                str_bb = format_toTeX(lower);                   str_bb = format_toTeX(lower);  
              } else { /* answer could be string, choice */               } else { /* answer could be string, choice */
                str_bb = (char *)capa_malloc(strlen(lower)+MAX_BUFFER_SIZE,1);                 str_bb = (char *)capa_malloc(strlen(lower)+MAX_BUFFER_SIZE,1);
        if (p->ans_type == ANSWER_IS_FORMULA || 1)         if (p->verbatim == DO_VERBATIM)
  sprintf(str_bb,"\\begin{verbatim}%s\\end{verbatim}",lower);   sprintf(str_bb,"\\begin{verbatim}%s\\end{verbatim}",lower);
        else         else
  strcpy(str_bb,lower);   strcpy(str_bb,lower);
Line 2869  answers_string(mode, p)int mode;Problem_ Line 2900  answers_string(mode, p)int mode;Problem_
   
 /* <== This routine checks user input string *ans against correct answer *s ==> */  /* <== This routine checks user input string *ans against correct answer *s ==> */
 int  int
 capa_check_ans(ai,ans) AnswerInfo_t  *ai; char *ans;  capa_check_ans(ai,ans, error) AnswerInfo_t  *ai; char *ans; char **error;
 {  {
   int      t;     /* ans_type  */    int      t;     /* ans_type  */
   char    *s;     /* ans_str   */    char    *s;     /* ans_str   */
Line 2912  capa_check_ans(ai,ans) AnswerInfo_t  *ai Line 2943  capa_check_ans(ai,ans) AnswerInfo_t  *ai
               outcome = split_num_unit(ans,&n_part,num_str,unit_str);                outcome = split_num_unit(ans,&n_part,num_str,unit_str);
               if( outcome > 1 ) { /* with both num and unit parts or only unit part */                if( outcome > 1 ) { /* with both num and unit parts or only unit part */
                 if( u_p != NULL ) {                  if( u_p != NULL ) {
                   result = check_correct_unit(unit_str,u_p,&scale);    if (UNIT_FAIL == (result = check_correct_unit(unit_str,u_p,&scale))) {
       *error=strsave(unit_str);
     }
                 } else { /* what to do when no unit is specified but student entered a unit? */                  } else { /* what to do when no unit is specified but student entered a unit? */
                   result = UNIT_NOTNEEDED;                    result = UNIT_NOTNEEDED;
     *error=strsave(unit_str);
                 }                  }
               } else {                } else {
                 if( u_p != NULL ) {                  if( u_p != NULL ) {
Line 2929  capa_check_ans(ai,ans) AnswerInfo_t  *ai Line 2963  capa_check_ans(ai,ans) AnswerInfo_t  *ai
                 }                  }
                 given = n_part * scale; /* convert the given answer into proper scale for units */                  given = n_part * scale; /* convert the given answer into proper scale for units */
                 sig = calc_sig( num_str );                  sig = calc_sig( num_str );
                 if( (sig < sl) || (sig > su) ) {                  if( ((sig < sl   ) || (sig > su   )) && (sig!=0)) {
                   result = SIG_FAIL;    result = SIG_FAIL;
     *error=capa_malloc(1,ANSWER_STRING_LENG);
     sprintf(*error,"%d",sig);
                 } else {                  } else {
                   switch( tt ) { /* tolerence type */                    switch( tt ) { /* tolerence type */
                    case TOL_ABSOLUTE:                     case TOL_ABSOLUTE:
Line 2972  capa_check_ans(ai,ans) AnswerInfo_t  *ai Line 3008  capa_check_ans(ai,ans) AnswerInfo_t  *ai
                 } /* end sig check */                  } /* end sig check */
               } /* end if unit check */                } /* end if unit check */
             } else { /* user entered alphabets, but no number */              } else { /* user entered alphabets, but no number */
               result = INCORRECT;                result = WANTED_NUMERIC;
             }               } 
           }            }
           break;            break;
Line 2983  capa_check_ans(ai,ans) AnswerInfo_t  *ai Line 3019  capa_check_ans(ai,ans) AnswerInfo_t  *ai
               for(idx=0;idx<ANSWER_STRING_LENG;idx++) choice[idx] = 0;                for(idx=0;idx<ANSWER_STRING_LENG;idx++) choice[idx] = 0;
               result = EXACT_ANS;                result = EXACT_ANS;
               for(ii=0;ii<corr_len; ii++) {                for(ii=0;ii<corr_len; ii++) {
                 idx = toupper(correct[ii]) - 'A'; choice[idx] =  1;                  idx = toupper(s[ii]) - 'A'; choice[idx] =  1;
               }                }
               for(ii=0;ii<input_len;ii++) {                for(ii=0;ii<input_len;ii++) {
                 idx = toupper(answer[ii]) - 'A';                  idx = toupper(ans[ii]) - 'A';
                 if(choice[idx] != 1 )  result = INCORRECT;                  if(choice[idx] != 1 )  result = INCORRECT;
               }                }
             } else { result = INCORRECT; }              } else { result = INCORRECT; }
Line 3002  capa_check_ans(ai,ans) AnswerInfo_t  *ai Line 3038  capa_check_ans(ai,ans) AnswerInfo_t  *ai
           result = check_formula_ans(s,ans,ai->ans_id_list,ai->ans_pts_list,tt,to);            result = check_formula_ans(s,ans,ai->ans_id_list,ai->ans_pts_list,tt,to);
           break;            break;
     case ANSWER_IS_EXTERNAL: /* Not yet implemented */      case ANSWER_IS_EXTERNAL: /* Not yet implemented */
             
             
             
           break;            break;
   }    }
   return (result);    return (result);
Line 3024  capa_check_ans(ai,ans) AnswerInfo_t  *ai Line 3063  capa_check_ans(ai,ans) AnswerInfo_t  *ai
   
   
 int  int
 capa_check_answer(p, answer) Problem_t *p; char *answer;  capa_check_answer(p, answer, error) Problem_t *p; char *answer; char **error;
 {  {
   int     type;     int     type; 
   char   *correct;    char   *correct;
Line 3065  capa_check_answer(p, answer) Problem_t * Line 3104  capa_check_answer(p, answer) Problem_t *
               outcome = split_num_unit(answer,&n_part,input,unit_str);                outcome = split_num_unit(answer,&n_part,input,unit_str);
               if( outcome > 1 ) { /* with both num and unit parts or only unit part */                if( outcome > 1 ) { /* with both num and unit parts or only unit part */
                 if( p->ans_unit != NULL ) {                  if( p->ans_unit != NULL ) {
                   result = check_correct_unit(unit_str,p->ans_unit,&scale);    if ( UNIT_FAIL == ( result = check_correct_unit(unit_str,p->ans_unit,&scale) ) ) {
                       *error=strsave(unit_str);
     }
                 } else { /* what to do when no unit is specified but student entered a unit? */                  } else { /* what to do when no unit is specified but student entered a unit? */
                   result = UNIT_NOTNEEDED;                    result = UNIT_NOTNEEDED;
     *error=strsave(unit_str);
                 }                  }
               } else {                } else {
                 if( p->ans_unit != NULL ) {                  if( p->ans_unit != NULL ) {
Line 3085  capa_check_answer(p, answer) Problem_t * Line 3126  capa_check_answer(p, answer) Problem_t *
                 sig = calc_sig( input );                  sig = calc_sig( input );
                 if( ((sig < sig_l) || (sig > sig_u)) && (sig!=0)) {                  if( ((sig < sig_l) || (sig > sig_u)) && (sig!=0)) {
                   result = SIG_FAIL;                    result = SIG_FAIL;
     *error=capa_malloc(1,ANSWER_STRING_LENG);
     sprintf(*error,"%d",sig);
                 } else {                  } else {
                   switch( tol_type ) {                    switch( tol_type ) {
                    case TOL_ABSOLUTE:                     case TOL_ABSOLUTE:
Line 3126  capa_check_answer(p, answer) Problem_t * Line 3169  capa_check_answer(p, answer) Problem_t *
                 } /* end sig check */                  } /* end sig check */
               } /* end if unit check */                } /* end if unit check */
             } else { /* user entered alphabet, but no number */              } else { /* user entered alphabet, but no number */
               result = INCORRECT;                result = WANTED_NUMERIC;
             }               } 
           }            }
           break;            break;
Line 3158  capa_check_answer(p, answer) Problem_t * Line 3201  capa_check_answer(p, answer) Problem_t *
          /* we assume the external program is called through popen() */           /* we assume the external program is called through popen() */
          /* and the result will be given back as 0 or 1 to indicate the */           /* and the result will be given back as 0 or 1 to indicate the */
          /* given answer is correct or not */           /* given answer is correct or not */
                     /* arguments are given to the program as */
            /* before running the program, check its existance first */
            /* should we specify a time out period in capa.config file? */
            /* set up a timer for this purpose */
            /* FILE *popen (const char *command,const char *type ); */
   
           break;            break;
           
   }    }
   return (result);    return (result);
 }  }
   
 /* ----------------------------------------------------------------------------------- */  /* -------------------------------------------------------------------------- */
 /*   assumming the formula is *fml_str and the student input is *input_str             */  /*   assumming the formula is *fml_str and the student input is *input_str             */
 /*   according to the type of tolerance, we form the final formula as                  */  /*   according to the type of tolerance, we form the final formula as                  */
 /*      absolute tolerance:  (*fml_str) - (*input_str)                                 */  /*      absolute tolerance:  (*fml_str) - (*input_str)                                 */
Line 3310  int tol_type;double tol;char *lower;char Line 3358  int tol_type;double tol;char *lower;char
 /* New check answers routine checks the /AND and /OR group of answers */  /* New check answers routine checks the /AND and /OR group of answers */
 /*   use array of char pointers  char **a */  /*   use array of char pointers  char **a */
 int  int
 capa_check_answers(p,answers,cnt) Problem_t *p; char **answers; int cnt;  capa_check_answers(p,answers,cnt,error) 
   Problem_t *p; char **answers; int cnt; char **error;
 {  {
   AnswerInfo_t  *ai;    AnswerInfo_t  *ai;
   int            ii, done, result;    int            ii, done, result;
   int           *outcomes;    int           *outcomes;
     char          **errormsg;
       
     errormsg=(char**)capa_malloc(cnt,sizeof(char*));
   if(p->ans_op == ANS_AND) {  /* ans /and ans */    if(p->ans_op == ANS_AND) {  /* ans /and ans */
     if( (cnt != p->ans_cnt) ) { return (ANS_CNT_NOT_MATCH); }      if( (cnt != p->ans_cnt) ) { return (ANS_CNT_NOT_MATCH); }
     if( cnt == 1 ) { return (capa_check_answer(p, answers[0])); } /* there is only one answer */      if( cnt == 1 ) { return (capa_check_answer(p, answers[0], error)); } /* there is only one answer */
     outcomes = (int *)capa_malloc(sizeof(int),cnt);      outcomes = (int *)capa_malloc(sizeof(int),cnt);
     for(ii=0;ii<cnt;ii++) outcomes[ii]=0;  /* initialize the outcomes array */      for(ii=0;ii<cnt;ii++) outcomes[ii]=0;  /* initialize the outcomes array */
     outcomes[0] = capa_check_answer(p, answers[0]);      outcomes[0] = capa_check_answer(p, answers[0], &errormsg[0]);
   #ifdef    COMMON_DBUG
       printf("CAPA_CHECK_ANSWER(%s,%s):: outcome[0]=%d\n", 
          p->answer,answers[0],outcomes[0]); 
   #endif
     for(ii=1, ai = p->ans_list; ai; ii++,ai = ai->ans_next  ) {      for(ii=1, ai = p->ans_list; ai; ii++,ai = ai->ans_next  ) {
        outcomes[ii] =  capa_check_ans(ai,answers[ii]);         outcomes[ii] =  capa_check_ans(ai,answers[ii],&(errormsg[ii]));
   #ifdef    COMMON_DBUG
       printf("CAPA_CHECK_ANS(%s,%s): outcome[%d]=%d\n", ai->ans_str,answers[ii],ii,outcomes[ii]); 
   #endif
     }      }
     done = ii = 0;      done = ii = 0;
     result = 0;      result = 0;
Line 3332  capa_check_answers(p,answers,cnt) Proble Line 3390  capa_check_answers(p,answers,cnt) Proble
           (outcomes[ii] == NO_UNIT)   ||            (outcomes[ii] == NO_UNIT)   ||
           (outcomes[ii] == UNIT_NOTNEEDED) ) {            (outcomes[ii] == UNIT_NOTNEEDED) ) {
          result = outcomes[ii];           result = outcomes[ii];
    if (result != NO_UNIT) { *error=strsave(errormsg[ii]); }
          done = 1;           done = 1;
       }        }
       ii++;        ii++;
       if(ii==cnt) done = 1;        if(ii==cnt) done = 1;
     }      }
     if( result == 0 ) { /* check if any of the outcome has failed on sig figs */      if( result == 0 ) { 
       /* check if any of the outcome has failed to be a numeric 
          or was a malformed equation */
         done = ii = 0;
         while( !done ) {
           if( outcomes[ii] == WANTED_NUMERIC || outcomes[ii] == BAD_FORMULA ) {
             result = outcomes[ii];
             done = 1;
           }
           ii++;
           if(ii==cnt) done = 1;
         }
       }
       if( result == 0 ) {/*check if any of the outcome has failed on sig figs*/
       done = ii = 0;        done = ii = 0;
       while( !done ) {        while( !done ) {
         if( outcomes[ii] == SIG_FAIL ) {          if( outcomes[ii] == SIG_FAIL ) {
           result = outcomes[ii];            result = outcomes[ii];
     *error = strsave(errormsg[ii]);
           done = 1;            done = 1;
         }          }
         ii++;          ii++;
Line 3359  capa_check_answers(p,answers,cnt) Proble Line 3432  capa_check_answers(p,answers,cnt) Proble
         if(ii==cnt) done = 1;          if(ii==cnt) done = 1;
       }        }
     }      }
       for (ii=0;ii<cnt;ii++) {
         if( (outcomes[ii] == UNIT_FAIL) ||
             (outcomes[ii] == SIG_FAIL)   ||
             (outcomes[ii] == UNIT_NOTNEEDED) ) {
    capa_mfree(errormsg[ii]);
         }
       }
       capa_mfree((char *)errormsg);
     capa_mfree((char *)outcomes);      capa_mfree((char *)outcomes);
     if( result == 0 ) {      if( result == 0 ) {
       result = APPROX_ANS; /* all answers are correct */        result = APPROX_ANS; /* all answers are correct */
     }      }
         
   } else { /* should be ANS_OR , user answer count should always be 1 */    } else { /* should be ANS_OR , user answer count should always be 1 */
      if( cnt != 1 ) { return (ANS_CNT_NOT_MATCH); }       if( cnt != 1 ) { return (ANS_CNT_NOT_MATCH); }
      if( p->ans_cnt == 1 ) { return (capa_check_answer(p, answers[0])); }       if( p->ans_cnt == 1 ) { return (capa_check_answer(p, answers[0], error)); }
      result = capa_check_answer(p, answers[0]);       result = capa_check_answer(p, answers[0], error);
      ii = 1;  ai = p->ans_list;       ii = 1;  ai = p->ans_list;
      while( (ii<p->ans_cnt) && ( (result != EXACT_ANS) && (result != APPROX_ANS) ) ) {       while( (ii<p->ans_cnt) && ( (result != EXACT_ANS) && (result != APPROX_ANS) ) ) {
          result =  capa_check_ans(ai,answers[0]);         if((ii!=1)&&((result==UNIT_FAIL)||(result==SIG_FAIL)||(result==UNIT_NOTNEEDED))) {
          ai = ai->ans_next; ii++;   capa_mfree((char*)error);
          }
          result =  capa_check_ans(ai,answers[0],error);
          ai = ai->ans_next; ii++;
      }       }
   }    }
   return (result);    return (result);

Removed from v.1.6  
changed lines
  Added in v.1.17


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