--- capa/capa51/pProj/capaCommon.c 2004/06/01 19:57:18 1.24 +++ capa/capa51/pProj/capaCommon.c 2005/12/20 19:59:57 1.27 @@ -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 */ @@ -2505,8 +2506,11 @@ char *buf;double *num; char *num_p; char 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 */ @@ -2927,7 +2934,7 @@ capa_check_ans(ai,ans, error) AnswerInfo char *us; /* ans_unit_str */ Unit_t *u_p; /* ans_unit */ int input_len, all_alphabet = 1, idx, ii, type; - int outcome, result = INCORRECT; + int outcome=-1, result = INCORRECT; int sig, corr_len; int choice[ANSWER_STRING_LENG]; char num_str[ANSWER_STRING_LENG], unit_str[ANSWER_STRING_LENG]; @@ -2953,8 +2960,10 @@ 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 ) { result = check_correct_unit(unit_str,u_p,&scale); @@ -3094,7 +3103,7 @@ capa_check_answer(p, answer, error) Prob char *fmt; int choice[ANSWER_STRING_LENG], correctans[ANSWER_STRING_LENG]; int ii, idx, corr_len, input_len; - int result = INCORRECT, sig, outcome, all_alphabet; + int result = INCORRECT, sig, outcome=-1, all_alphabet; char fmted[FORMAT_STRING_LENG]; double given, target, ratio, fmted_target, target_u, target_l, scale=1.0; double delta; @@ -3122,6 +3131,8 @@ 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 ) { result = check_correct_unit(unit_str,p->ans_unit,&scale); @@ -3239,6 +3250,25 @@ capa_check_answer(p, answer, error) Prob return (result); } +int check_tol (formula_val,tol_type,tol) +double formula_val;int tol_type;double tol; +{ + double diff; + int outcome=APPROX_ANS; + if( tol_type == TOL_ABSOLUTE ) { + diff = tol - formula_val; + if( diff < 0.0 ) { + outcome = INCORRECT; + } + } else { + diff = fabs(1.0 - formula_val) * 100.0 ; + if( diff > tol ) { + outcome = INCORRECT; + } + } + return outcome; +} + /* -------------------------------------------------------------------------- */ /* 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 */ @@ -3251,7 +3281,7 @@ char *fml_str;char *input_str;char *var_ char *check_fml_str; int f_len, i_len, outcome, error_code; PointsList_t *pt, *next; - double formula_val, diff; + double formula_val; f_len = strlen(fml_str); i_len = strlen(input_str); @@ -3263,21 +3293,20 @@ char *fml_str;char *input_str;char *var_ sprintf(check_fml_str,"(%s) / (%s)",input_str,fml_str); } outcome = APPROX_ANS; + if( pts_list==NULL ) { + error_code = f_eval_formula(&formula_val,check_fml_str, var_list, NULL); + if( ! error_code ) { + outcome = check_tol(formula_val,tol_type,tol); + } else { + outcome = BAD_FORMULA; + } + } + for(pt= pts_list; pt!=NULL ; pt=next) { next=pt->pts_next; error_code = f_eval_formula(&formula_val,check_fml_str, var_list, pt->pts_str); if( ! error_code ) { - if( tol_type == TOL_ABSOLUTE ) { - diff = tol - formula_val; - if( diff < 0.0 ) { - outcome = INCORRECT; - } - } else { - diff = abs(1.0 - formula_val) * 100.0 ; - if( diff > tol ) { - outcome = INCORRECT; - } - } + outcome = check_tol(formula_val,tol_type,tol); } else { outcome = BAD_FORMULA; break;