Annotation of capa/capa51/pProj/checkunits.c, revision 1.3

1.2       albertel    1: /* harness to test the unit engine out
                      2:    Copyright (C) 1992-2000 Michigan State University
                      3: 
                      4:    The CAPA system is free software; you can redistribute it and/or
                      5:    modify it under the terms of the GNU Library General Public License as
                      6:    published by the Free Software Foundation; either version 2 of the
                      7:    License, or (at your option) any later version.
                      8: 
                      9:    The CAPA system is distributed in the hope that it will be useful,
                     10:    but WITHOUT ANY WARRANTY; without even the implied warranty of
                     11:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
                     12:    Library General Public License for more details.
                     13: 
                     14:    You should have received a copy of the GNU Library General Public
                     15:    License along with the CAPA system; see the file COPYING.  If not,
                     16:    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.3     ! albertel   17:    Boston, MA 02111-1307, USA.
        !            18: 
        !            19:    As a special exception, you have permission to link this program
        !            20:    with the TtH/TtM library and distribute executables, as long as you
        !            21:    follow the requirements of the GNU GPL in regard to all of the
        !            22:    software in the executable aside from TtH/TtM.
        !            23: */
1.1       albertel   24: 
                     25: /* 
                     26:    Units is checked against real or integer answers, 
                     27:    for string and choice answers, units is not checked 
                     28:    
                     29:           UNIT_NOTNEEDED  ==> user entered some units and answer did not need one
                     30:           NO_UNIT         ==> 
                     31:           UNIT_FAIL
                     32:    
                     33:    ans_type must be either ANSWER_IS_INTEGER or ANSWER_IS_FLOAT
                     34:    or else, skip or return UNIT_NOTNEEDED
                     35:    if unit_p is NULL, then UNIT_NOTNEEDED
                     36:    
                     37:    answer must contains at least one digit 
                     38:    
                     39:    
                     40: */
                     41: 
                     42: /*   
                     43: 
                     44: 
                     45: #define    EXACT_ANS           1
                     46: #define    APPROX_ANS          2 
                     47: #define    SIG_FAIL            3
                     48: #define    UNIT_FAIL           4
                     49: #define    NO_UNIT             5
                     50: #define    UNIT_OK             6
                     51: #define    INCORRECT           7
                     52: #define    UNIT_NOTNEEDED      8
                     53: #define    ANS_CNT_NOT_MATCH   9
                     54:   /*  called from capaCommon.c */
                     55: /*                      */
                     56: /*  UNIT_FAIL           */
                     57: /*  NO_UNIT             */
                     58: /*  result: UNIT_OK correct   */
                     59: /*                            */
                     60:  */
                     61: 
                     62: /* capa_check_units() only works under ANSWER_IS_INTEGER or ANSWER_IS_FLOAT */
                     63: int
                     64: capa_check_units( ans, u_p ) char *ans;Unit_t *u_p;
                     65: {
                     66:   input_len = strlen(ans);
                     67:   all_alphabet = 1;
                     68:   for(idx=0;idx<input_len;idx++) {
                     69:     if( isdigit(ans[idx]) ) {
                     70:       all_alphabet = 0;
                     71:     }
                     72:   }
                     73:   
                     74:   if( !all_alphabet ) { /* contains at least a digit char */
                     75:     outcome = split_num_unit(ans,&n_part,num_str,unit_str);
                     76:     /* num_str is used in calc_sig() for sig fig */
                     77:     /* n_part is used together with scale from check_correct_answer() */
                     78:     /* unit_str is used later as input to check_correct_unit() */
                     79:     if( outcome > 1 ) {  /* with both num and unit parts or only unit part */
                     80:     
                     81:       if( u_p != NULL ) {
                     82:           result = check_correct_unit(unit_str,u_p,&scale);
                     83:           /* result could be UNIT_OK, UNIT_FAIL */
                     84:           
                     85:           
                     86:       } else { /* no unit is specified but student entered an units? */
                     87:           
                     88:           result = UNIT_NOTNEEDED;
                     89:           
                     90:       }
                     91: 
                     92:     } else { /* outcome = 0 empty string, 1 a number without units */
                     93:       if( u_p != NULL ) {
                     94:           
                     95:           result = NO_UNIT; /* units required */
                     96:           
                     97:       } else { 
                     98:           result = INCORRECT, /* proceed to check numerical value */
                     99:       }
                    100:     }
                    101:     /* at this point, result could be one of UNIT_OK, UNIT_FAIL, UNIT_NOTNEEDED, and NO_UNIT */
                    102:     
                    103:     sig = calc_sig( num_str );
                    104:     
                    105:   } else {   /* contains all non-digit  char */
                    106:       result = INCORRECT;  /* the students may enter an units but not containing any number */
                    107:       
                    108:   }
                    109:   return (result);
                    110: }
                    111: 
                    112: 
                    113: 
                    114: 
                    115: 
                    116: 
                    117: 
                    118: 
                    119: 
                    120: 
                    121: 
                    122: 
                    123: 
                    124: 
                    125: int
                    126: capa_check_allunits(p,answers,cnt) Problem_t *p; char **answers; int cnt;
                    127: {
                    128:   int     type; 
                    129:   char   *correct;
                    130:   char    input[ANSWER_STRING_LENG], unit_str[UNIT_STRING_LENG];
                    131:   int     tol_type, calc_type;
                    132:   double  tol, n_part; 
                    133:   int     sig_l; 
                    134:   int     sig_u;
                    135:   char   *fmt;
                    136:   int     choice[SIXTY_FOUR], ii, idx, corr_len, input_len;
                    137:   int     result = INCORRECT, sig, outcome, all_alphabet;
                    138:   char    fmted[FORMAT_STRING_LENG];
                    139:   double  given, target, ratio, fmted_target, target_u, target_l, scale=1.0;
                    140:   double  delta;
                    141:   
                    142:   type       = p->ans_type;
                    143:   correct    = p->answer;
                    144:   tol_type   = p->tol_type;
                    145:   tol        = p->tolerance;
                    146:   sig_l      = p->sig_lbound;
                    147:   sig_u      = p->sig_ubound;
                    148:   fmt        = p->ans_fmt;
                    149:   calc_type  = p->calc;
                    150:   unit_str[0]= 0;
                    151:   
                    152:   if( (cnt != p->ans_cnt) ) { return (ANS_CNT_NOT_MATCH); }
                    153:   switch(type) {
                    154:     case ANSWER_IS_INTEGER:
                    155:     case ANSWER_IS_FLOAT:
                    156:           {
                    157:             input_len = strlen(ans);
                    158:             all_alphabet = 1;
                    159:             for(idx=0;idx<input_len;idx++) {
                    160:               if( isdigit(ans[idx]) ) {
                    161:                 all_alphabet = 0;
                    162:               }
                    163:             }
                    164:             if( !all_alphabet ) {  /* answer string is not all alphabets */
                    165:               outcome = split_num_unit(ans,&n_part,num_str,unit_str);
                    166:               if( outcome > 1 ) {  /* with both num and unit parts or only unit part */
                    167:                 if( u_p != NULL ) {
                    168:                   result = check_correct_unit(unit_str,u_p,&scale);
                    169:                 } else { /* what to do when no unit is specified but student entered a
                    170:                 unit? */
                    171:                   result = UNIT_NOTNEEDED;
                    172:                 }
                    173:               } else {
                    174:                 if( u_p != NULL ) {
                    175:                   result = NO_UNIT;
                    176:                 }
                    177:               }
                    178:   
                    179:   idx = 1;  ai = p->ans_list;
                    180:        while( (idx<cnt) && ( (result == EXACT_ANS) || (result == APPROX_ANS) ) ) {
                    181:          result =  capa_check_ans(ai,answers[idx]);
                    182:          ai = ai->ans_next; idx++;
                    183:        }
                    184:   if(num_answer==2)  printf("Answer:->%s [%s,%s] %s, ",p->answer,lower,upper,
                    185: 			    p->unit_str); 
                    186:       else  printf("Answer:->%s, ",lower);
                    187:   switch(p->ans_type) {
                    188:        case ANSWER_IS_INTEGER:   printf(" INTEGER,");
                    189:         break;
                    190:        case ANSWER_IS_FLOAT: 
                    191:         printf(" FLOAT format=%s, SIG=[%d,%d],",
                    192:           p->ans_fmt,p->sig_lbound,p->sig_ubound );
                    193:         break;
                    194:        case ANSWER_IS_STRING_CI: printf(" STRING C.I.,"); break;
                    195:        case ANSWER_IS_STRING_CS: printf(" STRING C.S.,"); break;
                    196:        case ANSWER_IS_CHOICE:    printf(" CHOICE,");      break;
                    197:    }
                    198:    printf(" WEIGHT=%d, TRIES=%d\n", p->weight,p->tries); fflush(stdout);
                    199:    if(p->ans_unit != NULL) {
                    200:         print_unit_t(p->ans_unit);  fflush(stdout);
                    201:    }

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