--- capa/capa51/pProj/capaCommon.c 1999/12/01 18:22:00 1.3 +++ capa/capa51/pProj/capaCommon.c 2000/07/07 18:33:03 1.13 @@ -1,6 +1,29 @@ +/* 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 Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library 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 =====================|<||= */ /* created 1994 by Isaac Tsai */ -/* 1994, 1995, 1996, 1997, 1998, 1999 copyrighted by Isaac Tsai */ /* TODO: restructure capa_check_ans*() calls into one */ /* =||>|===================== capaCommon.c =====================|<||= */ #include @@ -569,8 +592,8 @@ int set; } funlockstream(fp); fclose(fp); if(!found) { - ans_p = capa_malloc(nq+1,1); - tries_p = capa_malloc(3*nq+2,1); /* space and \0 */ + ans_p = capa_malloc(nq+2,1); + tries_p = capa_malloc(3*nq+3,1); /* space and \0 */ for(ii=0;iianswers = ans_p; entry->tries = tries_p; entry->e_probs = nq; @@ -2122,6 +2147,10 @@ extern int Symb_count; extern int first_run; extern int Stop_Parser; extern void (*Status_Func)(); +#ifdef TTH +extern void tth_restart(); +extern char* tth_err; +#endif /*TTH*/ long seed1, seed2; T_student a_student; char *class, *classname, warn_msg[WARN_MSG_LENGTH]; @@ -2150,6 +2179,10 @@ extern void (*Status_Func)(); } u_getunit(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_student_number,student_number,MAX_STUDENT_NUMBER+1); Parse_section = a_student.s_sec; @@ -2610,7 +2643,7 @@ answers_string(mode, p)int mode;Problem_ str_bb = format_toTeX(lower); } else { /* answer could be string, choice */ 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); else strcpy(str_bb,lower); @@ -2865,7 +2898,7 @@ answers_string(mode, p)int mode;Problem_ /* <== This routine checks user input string *ans against correct answer *s ==> */ 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 */ char *s; /* ans_str */ @@ -2908,9 +2941,12 @@ capa_check_ans(ai,ans) AnswerInfo_t *ai 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( 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? */ result = UNIT_NOTNEEDED; + *error=strsave(unit_str); } } else { if( u_p != NULL ) { @@ -2925,8 +2961,10 @@ capa_check_ans(ai,ans) AnswerInfo_t *ai } given = n_part * scale; /* convert the given answer into proper scale for units */ sig = calc_sig( num_str ); - if( (sig < sl) || (sig > su) ) { - result = SIG_FAIL; + if( ((sig < sl ) || (sig > su )) && (sig!=0)) { + result = SIG_FAIL; + *error=capa_malloc(1,ANSWER_STRING_LENG); + sprintf(*error,"%d",sig); } else { switch( tt ) { /* tolerence type */ case TOL_ABSOLUTE: @@ -2968,7 +3006,7 @@ capa_check_ans(ai,ans) AnswerInfo_t *ai } /* end sig check */ } /* end if unit check */ } else { /* user entered alphabets, but no number */ - result = INCORRECT; + result = WANTED_NUMERIC; } } break; @@ -2998,6 +3036,9 @@ capa_check_ans(ai,ans) AnswerInfo_t *ai result = check_formula_ans(s,ans,ai->ans_id_list,ai->ans_pts_list,tt,to); break; case ANSWER_IS_EXTERNAL: /* Not yet implemented */ + + + break; } return (result); @@ -3020,7 +3061,7 @@ capa_check_ans(ai,ans) AnswerInfo_t *ai 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; char *correct; @@ -3061,10 +3102,12 @@ capa_check_answer(p, answer) Problem_t * outcome = split_num_unit(answer,&n_part,input,unit_str); if( outcome > 1 ) { /* with both num and unit parts or only unit part */ 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? */ result = UNIT_NOTNEEDED; + *error=strsave(unit_str); } } else { if( p->ans_unit != NULL ) { @@ -3079,8 +3122,10 @@ capa_check_answer(p, answer) Problem_t * } given = n_part * scale; /* convert the given answer into proper scale for units */ sig = calc_sig( input ); - if( (sig < sig_l) || (sig > sig_u) ) { + if( ((sig < sig_l) || (sig > sig_u)) && (sig!=0)) { result = SIG_FAIL; + *error=capa_malloc(1,ANSWER_STRING_LENG); + sprintf(*error,"%d",sig); } else { switch( tol_type ) { case TOL_ABSOLUTE: @@ -3122,7 +3167,7 @@ capa_check_answer(p, answer) Problem_t * } /* end sig check */ } /* end if unit check */ } else { /* user entered alphabet, but no number */ - result = INCORRECT; + result = WANTED_NUMERIC; } } break; @@ -3151,6 +3196,11 @@ capa_check_answer(p, answer) Problem_t * result = check_formula_ans(correct,answer,p->id_list,p->pts_list,tol_type,tol); break; case ANSWER_IS_EXTERNAL: /* not yet implemented */ + /* we assume the external program is called through popen() */ + /* and the result will be given back as 0 or 1 to indicate the */ + /* given answer is correct or not */ + /* arguments are given to the program as */ + break; } @@ -3302,20 +3352,23 @@ int tol_type;double tol;char *lower;char /* New check answers routine checks the /AND and /OR group of answers */ /* use array of char pointers char **a */ 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; int ii, done, result; int *outcomes; + char **errormsg; + errormsg=(char**)capa_malloc(cnt,sizeof(char*)); if(p->ans_op == ANS_AND) { /* ans /and ans */ 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); for(ii=0;iians_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])); } done = ii = 0; result = 0; @@ -3324,16 +3377,29 @@ capa_check_answers(p,answers,cnt) Proble (outcomes[ii] == NO_UNIT) || (outcomes[ii] == UNIT_NOTNEEDED) ) { result = outcomes[ii]; + if (result != NO_UNIT) { *error=strsave(errormsg[ii]); } done = 1; } ii++; if(ii==cnt) done = 1; } + if( result == 0 ) { /* check if any of the outcome has failed to be a numeric */ + done = ii = 0; + while( !done ) { + if( outcomes[ii] == WANTED_NUMERIC ) { + 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; while( !done ) { if( outcomes[ii] == SIG_FAIL ) { result = outcomes[ii]; + *error = strsave(errormsg[ii]); done = 1; } ii++; @@ -3351,18 +3417,30 @@ capa_check_answers(p,answers,cnt) Proble if(ii==cnt) done = 1; } } + for (ii=0;iians_cnt == 1 ) { return (capa_check_answer(p, answers[0])); } - result = 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], error); ii = 1; ai = p->ans_list; while( (iians_cnt) && ( (result != EXACT_ANS) && (result != APPROX_ANS) ) ) { - result = capa_check_ans(ai,answers[0]); - ai = ai->ans_next; ii++; + if((ii!=1)&&((result==UNIT_FAIL)||(result==SIG_FAIL)||(result==UNIT_NOTNEEDED))) { + capa_mfree((char*)error); + } + result = capa_check_ans(ai,answers[0],error); + ai = ai->ans_next; ii++; } } return (result); @@ -3467,6 +3545,7 @@ char *key_word;char *value; fclose(fp); if (done) { + trim_response_ws(right); /*get rid of leading and trailing spaces*/ for(i=0,j=0;i<(strlen(right)+1);i++) { value[j]='\0'; if (right[i] == '\\' && (i < (strlen(right))) ) {