int func_is_poly(func_t *f) //多項式であるか { int i; if(f==NULL) return 0; if(func_is_mono_not_num(f)) return 1; if(func_is_number(f)) return 1; if(func_is_add(f) && func_power(f)==1){ for(i=0; i<func_asize(f); i++){ if(!func_is_mono_not_num(f->a[i]) && !func_is_number(f->a[i])) return 0; } return 1; } return 0; }
//1変数多項式の判定 文字番号出力 int func_poly_var1n(func_t *f) { int i,a,b; if (func_is_number(f)) { return -1; } else if(func_is_mono_not_num(f)) { return func_mono_var1n(f); } else if(!func_is_poly(f)) { return -1; } else if(!(func_is_add(f) && func_power(f)==1)){ return -1; } a=func_mono_var1n(f->a[0]); for(i=1; i<func_asize(f); i++){ b=func_mono_var1n(f->a[i]); if(!func_is_number(f->a[i]) && a!=b) return -1; } return a; }
func_t *func_add(func_t *f1, func_t *f2) { if (f1==NULL || f2==NULL) { FUNC_ERROR_ARG2("func_add",f1,f2); } else if(func_is_zero(f1)) { f1=func_del(f1); return f2; } else if(func_is_zero(f2)) { f2=func_del(f2); return f1; } else if(func_in_bigint(f1) && func_in_bigint(f2)) { return func_bigint_add(f1,f2); } else if(func_in_real(f1) && func_in_real(f2)) { return func_real_add(f1,f2); } else if(func_in_complex(f1) && func_in_complex(f2)){ return func_complex_add(f1,f2); } else if(func_is_real(f1) && func_is_number(f2)) { return func_add(f1,func_evalf(f2)); } else if(func_is_complex(f1) && func_is_number(f2)) { return func_add(f1,func_evalf(f2)); } else if(func_is_number(f1) && func_is_real(f2)) { return func_add(func_evalf(f1),f2); } else if(func_is_number(f1) && func_is_complex(f2)){ return func_add(func_evalf(f1),f2); } else if(func_in_vec(f1) && func_in_vec(f2)) { return func_vec_add(f1,f2); } else if(func_in_mat(f1) && func_in_mat(f2)) { return func_mat_add(f1,f2); } else { return func_add_eval(func_arg2_new(__func_add,f1,f2)); } }
func_t *func_poly_clone_coeff_ntarm(func_t *f,int n) { func_t *a; if(!func_is_poly(f)){ return NULL; } a=func_poly_get_mono_ntarm(f,n); if(a==NULL){ return func_zero(); } if(func_is_number(a)){ return func_clone(FR(a)); } a=func_mono_get_coeff(a); if(a==NULL){ return func_one(); } return func_clone(FR(a)); }
int func_poly_degree_max(func_t *f) { int a,b,i; if(func_is_number(f)) return 0; if(func_is_mono_not_num(f)) return func_mono_degree(f); if(func_is_poly(f)){ a=func_mono_degree(f->a[0]); for(i=1; i<func_asize(f); i++){ b=func_mono_degree(f->a[i]); if(a<b) a=b; } return a; } return 0; }
int email_grammar_checker(char *string) { int i=0,j=0; int val_start,val_end; int iteration_values[2][2]={{0,0},{0,0}}; int len=0; //length of string int position_at=0; //location of at int is_dot=0; //found dot? //rule 3 int count_at=0; int pass=SW_FAIL; //pass variable (default:FAIL) int char_count; //the number of letter char *p; char prev_ch=0; /* 1) an email address has two parts, Local and Domain, separated by an '@' char 2) both the Local part and the Domain part consist of a sequence of Words, separated by '.' characters 3) the Local part has one or more Words; the Domain part has two or more Words (i.e. at least one '.') 4) each Word is a sequence of one or more characters, starting with a letter 5) each Word ends with a letter or a digit 6) between the starting and ending chars, there may be any number of letters, digits or hyphens ('-') */ //--------------------------------------------------------------------------------------- //RULE 1 //1) an email address has two parts, Local and Domain, separated by an '@' char //String is not safisfied with rule 1( '@' should be located between Local and Domain.); p=string; len=0; count_at=0; pass=SW_FAIL; while(*p) { if(func_is_at(*p)== IS_TRUE) { pass=SW_PASS; //this rule is valid because of the presence of @. position_at=(int)(p-string);//get position of '@'. count_at++; } p++; len++; } if(count_at>1) // abc@[email protected] pass=SW_FAIL; if(pass==SW_FAIL) RETURN_MSG("err:r1",RET_INVALID); //Local Checking scope iteration_values[0][0]=0; iteration_values[0][1]=position_at; //Domain Checking scope iteration_values[1][0]=position_at+1; iteration_values[1][1]=len; pass=SW_PASS; //---------------------------------------------------------------------------------------- //RULE 2 //2) both the Local part and the Domain part consist of a sequence of Words, separated by '.' characters //Words should be separated by '.'. So, ".." is not permitted. for(j=0;j<2;j++) { val_start=iteration_values[j][0]; val_end=iteration_values[j][1]; if(func_is_dot(string[val_start])== IS_TRUE) // [email protected] =>FAIL pass=SW_FAIL; else if(func_is_dot(string[val_end-1])== IS_TRUE) // [email protected] =>FAIL pass=SW_FAIL; if(pass == SW_FAIL) RETURN_MSG("err:r2-0",RET_INVALID); prev_ch= string[val_start]; for(i=val_start+1;i<val_end;i++) { if(prev_ch==string[i]) if(func_is_dot(prev_ch)== IS_TRUE) // [email protected] { pass=SW_FAIL; break; } prev_ch=string[i]; } if(pass == SW_FAIL) RETURN_MSG("err:r2-1",RET_INVALID); } //---------------------------------------------------------------------------------------- //RULE 3 //3) the Local part has one or more Words; the Domain part has two or more Words (i.e. at least one '.') //Local Checking char_count=0; for(i=0;i<position_at;i++) { if(func_is_alphabet(string[i]) == IS_TRUE) char_count++; } if(char_count<1) pass=SW_FAIL; if(pass == SW_FAIL) RETURN_MSG("err:r3-1",RET_INVALID); //Domail Checking char_count=0; is_dot=0; for(i=position_at+2;i<len;i++) { if(func_is_alphabet(string[i]) == IS_TRUE) char_count++; else if(func_is_dot(string[i]) == IS_TRUE) is_dot=1; //[email protected] } if(char_count<2 || is_dot==0) // haha@ad pass=SW_FAIL; if(pass == SW_FAIL) RETURN_MSG("err:r3-2",RET_INVALID); //---------------------------------------------------------------------------------------- //RULE 4 //each Word is a sequence of one or more characters, starting with a letter for(j=0;j<2;j++) { val_start=iteration_values[j][0]; val_end=iteration_values[j][1]; prev_ch=string[val_start]; if(func_is_alphabet(prev_ch) == IS_FALSE) //[email protected] pass=SW_FAIL; else for(i=val_start+1;i<val_end;i++) { if(func_is_dot(prev_ch)== IS_TRUE) { if(func_is_alphabet(string[i]) == IS_FALSE) // [email protected] { pass=SW_FAIL; break; } } } if(pass == SW_FAIL) RETURN_MSG("err:r4",RET_INVALID); } //---------------------------------------------------------------------------------------- //RULE 5 //5) each Word ends with a letter or a digit for(j=0;j<2;j++) { val_start=iteration_values[j][0]; val_end=iteration_values[j][1]; if(!((func_is_alphabet(string[val_end-1]) ==IS_TRUE) || (func_is_number(string[val_end-1])==IS_TRUE))) // abc [d] @abc.com is a letter or a digit pass=SW_FAIL; else { prev_ch=string[val_start]; for(i=val_start+1;i<val_end;i++) { if(func_is_dot(string[i]) == IS_TRUE) // abc[.]@abc.com { if(!((func_is_alphabet(prev_ch) == IS_TRUE) || (func_is_number(prev_ch) == IS_TRUE))) { pass=SW_FAIL; break; } } prev_ch=string[i]; } } if(pass == SW_FAIL) RETURN_MSG("err:r5",RET_INVALID); } //---------------------------------------------------------------------------------------- //RULE 6 //6) between the starting and ending chars, there may be any number of letters, digits or hyphens ('-') for(j=0;j<2;j++) { val_start=iteration_values[j][0]; val_end=iteration_values[j][1]; if(func_is_dash(string[val_start])== IS_TRUE || func_is_dash(string[val_end-1]) == IS_TRUE) // [email protected] pass=SW_FAIL; else { prev_ch=string[val_start]; for(i=val_start+1;i<val_end;i++) { if(func_is_dot(prev_ch) == IS_TRUE)// [.][email protected] { if(func_is_dash(string[i]) == IS_TRUE) // .[-][email protected] { pass=SW_FAIL; break; } } else if(func_is_dot(string[i]) == IS_TRUE) // abc-[.][email protected] { if(func_is_dash(prev_ch) == IS_TRUE) // ab[-][email protected] { pass=SW_FAIL; break; } } prev_ch=string[i]; } } if(pass == SW_FAIL) RETURN_MSG("err:r6-1",RET_INVALID); } //------------------------------------------------------------------------------------------ return RET_VALID; }