--- capa/capa51/pProj/capaCommon.c 2002/06/26 17:26:09 1.21 +++ capa/capa51/pProj/capaCommon.c 2005/12/01 20:05:22 1.25 @@ -2454,7 +2454,8 @@ check_input_usymb(u_symb)char *u_symb; /* num_p : the numerical part in string */ /* unit_p : the unit string */ /* num_p is used to calculate sig figs */ -/* return : 0 empty string */ +/* return : -1 invalid string */ +/* 0 empty string */ /* 1 number without units */ /* 2 empty number with units */ /* 3 number with units */ @@ -2499,14 +2500,17 @@ char *buf;double *num; char *num_p; char while( isspace(buf[idx]) ) { idx++; } sscanf(num_str, "%lg", &result); /* put the numerical value into a double variable */ errcode = errcode | 1; - } else if( buf[idx] == 'x' || buf[idx] == 'X') { /* optional x or X part */ + } else if( buf[idx] == 'x' || buf[idx] == 'X' || buf[idx] == '*') { /* optional x or X part */ idx++; /* skip x or X */ while( isspace(buf[idx]) ) { idx++; } e_part = 1.0; /* default power */ base_str[b_idx] = 0; /* initialize base_str[] */ - while( isdigit(buf[idx]) || buf[idx] == '.' ) { /* should start with a digit or . */ + if( buf[idx] == '1' && buf[idx+1] == '0') { base_str[b_idx++] = buf[idx++]; + base_str[b_idx++] = buf[idx++]; + } else { + return (-1); } base_str[b_idx] = 0; /* terminate base_str[] */ while( isspace(buf[idx]) ) { idx++; } /* skip over white spaces */ @@ -2520,7 +2524,7 @@ char *buf;double *num; char *num_p; char while( isspace(buf[idx]) ) { idx++; } if( isdigit(buf[idx]) || buf[idx] == '+' || buf[idx] == '-' ) { exp_str[e_idx] = 0; /* initialize exp_str[], again */ - while( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) { + while( isdigit(buf[idx]) /*|| buf[idx] == '.'*/ || buf[idx] == '+' || buf[idx] == '-' ) { exp_str[e_idx++] = buf[idx++]; } exp_str[e_idx] = 0; /* terminate exp_str[] */ @@ -2554,7 +2558,7 @@ char *buf;double *num; char *num_p; char while( isspace(buf[idx]) ) { idx++; } if( isdigit(buf[idx]) || buf[idx] == '+' || buf[idx] == '-' ) { exp_str[e_idx] = 0; - while( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) { + while( isdigit(buf[idx]) /*|| buf[idx] == '.'*/ || buf[idx] == '+' || buf[idx] == '-' ) { exp_str[e_idx++] = buf[idx++]; } exp_str[e_idx] = 0; @@ -2565,6 +2569,9 @@ char *buf;double *num; char *num_p; char } sscanf(exp_str, "%lg", &e_part); sscanf(num_str, "%lg", &n_part); + if( n_part != 10 ) { + return (-1); + } result = pow(n_part,e_part); errcode = errcode | 1; } else { /* number unit */ @@ -2638,7 +2645,7 @@ answers_string(mode, p)int mode;Problem_ if(len_dd == 0 ) { /* no unit_str */ sprintf(ans_str," %s [%s,%s]\n\n", str_aa,str_bb,str_cc); } else { - sprintf(ans_str," %s [%s,%s] $%s$\n\n", str_aa,str_bb,str_cc,p->unit_str); + sprintf(ans_str," %s [%s,%s] $\\mathrm{%s}$\n\n", str_aa,str_bb,str_cc,p->unit_str); } capa_mfree((char *)str_aa); capa_mfree((char *)str_bb); @@ -2660,7 +2667,7 @@ answers_string(mode, p)int mode;Problem_ if(len_dd == 0 ) { /* no unit_str */ sprintf(ans_str," %s\n", str_bb); } else { - sprintf(ans_str," %s $%s$\n", str_bb,p->unit_str); + sprintf(ans_str," %s $\\mathrm{%s}$\n", str_bb,p->unit_str); } capa_mfree((char *)str_bb); } @@ -2897,6 +2904,16 @@ answers_string(mode, p)int mode;Problem_ } +int check_for_unit_fail(int result) +{ + int ret; + ret = (result == UNIT_FAIL || + result == UNIT_IRRECONCIBLE || + result == UNIT_INVALID_STUDENT3 || + result == UNIT_INVALID_STUDENT2 || + result == UNIT_INVALID_STUDENT1); + return ret; +} /* ------------------------------ called from capalogin */ /* impose stronger checking on the user input string *answer */ @@ -2943,11 +2960,14 @@ capa_check_ans(ai,ans, error) AnswerInfo all_alphabet = 0; } } - if( !all_alphabet ) { /* answer string is not all alphabets */ + if( !all_alphabet ) { outcome = split_num_unit(ans,&n_part,num_str,unit_str); + } + if( outcome > 0 ) { if( outcome > 1 ) { /* with both num and unit parts or only unit part */ if( u_p != NULL ) { - if (UNIT_FAIL == (result = check_correct_unit(unit_str,u_p,&scale))) { + result = check_correct_unit(unit_str,u_p,&scale); + if (check_for_unit_fail(result)) { *error=strsave(unit_str); } } else { /* what to do when no unit is specified but student entered a unit? */ @@ -2959,7 +2979,7 @@ capa_check_ans(ai,ans, error) AnswerInfo result = NO_UNIT; } } - if( (result != NO_UNIT) && (result != UNIT_FAIL) && ( result != UNIT_NOTNEEDED) ) { + if( (result != NO_UNIT) && (!check_for_unit_fail(result)) && ( result != UNIT_NOTNEEDED) ) { if( t == ANSWER_IS_FLOAT ) { target = (double)atof(s); /* real number */ } else { @@ -3097,7 +3117,7 @@ capa_check_answer(p, answer, error) Prob fmt = p->ans_fmt; calc_type = p->calc; unit_str[0]=0; - + switch(type) { case ANSWER_IS_INTEGER: case ANSWER_IS_FLOAT: @@ -3111,9 +3131,12 @@ capa_check_answer(p, answer, error) Prob } if( !all_alphabet ) { outcome = split_num_unit(answer,&n_part,input,unit_str); + } + if( outcome > 0 ) { if( outcome > 1 ) { /* with both num and unit parts or only unit part */ if( p->ans_unit != NULL ) { - if ( UNIT_FAIL == ( result = check_correct_unit(unit_str,p->ans_unit,&scale) ) ) { + result = check_correct_unit(unit_str,p->ans_unit,&scale); + if (check_for_unit_fail(result)) { *error=strsave(unit_str); } } else { /* what to do when no unit is specified but student entered a unit? */ @@ -3125,7 +3148,7 @@ capa_check_answer(p, answer, error) Prob result = NO_UNIT; } } - if( (result != NO_UNIT) && (result != UNIT_FAIL) && ( result != UNIT_NOTNEEDED) ) { + if( (result != NO_UNIT) && (!check_for_unit_fail(result)) && ( result != UNIT_NOTNEEDED) ) { if( type == ANSWER_IS_FLOAT ) { target = (double)atof(correct); /* real number */ } else { @@ -3400,7 +3423,7 @@ Problem_t *p; char **answers; int cnt; c done = ii = 0; result = 0; while( !done ) { /* check if any of the outcome has failed on units */ - if( (outcomes[ii] == UNIT_FAIL) || + if( (check_for_unit_fail(outcomes[ii])) || (outcomes[ii] == NO_UNIT) || (outcomes[ii] == UNIT_NOTNEEDED) ) { result = outcomes[ii]; @@ -3447,7 +3470,7 @@ Problem_t *p; char **answers; int cnt; c } } for (ii=0;iians_list; while( (iians_cnt) && ( (result != EXACT_ANS) && (result != APPROX_ANS) ) ) { - if((ii!=1)&&((result==UNIT_FAIL)||(result==SIG_FAIL)||(result==UNIT_NOTNEEDED))) { + if((ii!=1)&&((check_for_unit_fail(result))||(result==SIG_FAIL)||(result==UNIT_NOTNEEDED))) { capa_mfree((char*)error); } result = capa_check_ans(ai,answers[0],error);