/* apply a user-supplied function to each entry in the hashtable */ int ht_iterate(hashtable *p_ht, int (*p_func)(unsigned long slot, void *key, void *val), int *p_rv) { unsigned long i; int rv = 0; struct hashbucket_struct *hb; if(p_ht == NULL || p_ht->tag != HASHTABLE_TAG) return 1; if(p_func == NULL) return 0; for(i = 0; i < p_ht->size; i++) { /* for each slot in the table, iterate over each bucket in the list */ hb = p_ht->table[i]; while(hb != NULL) { /* iterate over the bucket list for this slot */ if(hb->tag != HASHBUCKET_TAG) return 4; rv = p_func(i, hb->key, hb->val); /* call user iteation function */ if(rv != 0) { if(p_rv != NULL) *p_rv = rv; return 5; } hb = hb->link; } } for(i = 0; i < p_ht->old_size; i++) { /* for each slot in the old table, iterate over each bucket in the lsit */ hb = p_ht->old_table[i]; while(hb != NULL) { /* iterate over the bucket list for this slot */ if(hb->tag != HASHBUCKET_TAG) return 4; rv = p_func(i, hb->key, hb->val); /* call user iteration function */ if(rv != 0) { if(p_rv != NULL) *p_rv = rv; return 5; } hb = hb->link; } } return 0; }
void load_file(char *filename, int (*p_func)(char *)) { int ret = 0; FILE * fp = NULL; char * fbuf = (char *)malloc(5*1024*1024); char *ptr = fbuf; int rlen = 0; fp = fopen("CONFIG_FILE_1", "rb"); if( NULL == fp) { perror(CONFIG_FILE_1" open fail!"); } /*读文件*/ do { ptr += rlen; rlen = fread(ptr, 1, 1500, fp); }while(1500 == rlen); ptr = fbuf; ret = p_func((char *)(ptr)); printf(CONFIG_FILE_1" ret = %d\n", ret); free(fbuf); }
int main(void) { char func_symb; double number; printf("Enter real number: "); scanf("%lf", &number); getchar(); // читаем из стандартного потока ввода символ переноса строки ('\n', появляется в нём после нажатия клавиши Enter) printf("Choose the function to calculate\n(e - exponent, l - log10, other symbol - abs): "); scanf("%c", &func_symb); // объявляем указатель на функцию void (*p_func)(double); // по ведённому пользователем символу выбираем функцию switch (func_symb) { case 'e': p_func = calculate_exp; break; case 'l': p_func = calculate_log10; break; default: p_func = calculate_abs; break; } // вызываем выбранную функцию через указатель p_func(number); return 0; }
bool call_command(const char* command, const char* data) { bool valid = false; // Is it a command? void (*p_func)(uint16_t); int i; for (i=0; pgm_read_word(&COMMANDS[i].PTEXT); i++) { if( !strcmp( (char*)pgm_read_word(&COMMANDS[i].PTEXT),command) ) { // Get the function and call it p_func = (PGM_VOID_P)pgm_read_word(&COMMANDS[i].PFUNC); p_func(atoi(data)); valid = true; } } if(valid) { return true; } else { printf_P(PSTR("ERROR: call_command - not a command: %s\n"),command); return false; } }
/* ** Loop do ... and return next ** */ int iter_routine_next(t_list *list, t_zappy *s, t_node *(*p_func)(t_zappy*, t_node*, t_type, int *)) { t_node *loop; int ret; ret = -1; loop = list->head; while (loop) loop = p_func(s, loop, list->type, &ret); return (ret); }
/* Output : None */ void traverseTblLst(const PTableNode p_tbl, void (*p_func)(ElemType item)) { UINT32 i; ElemType item; for (i = 0; i < getLengthTblLst(*p_tbl); ++i) { if ( TRUE == getItemByIndexTblLst(p_tbl, i, &item)) { p_func(item); } else { fprintf(stdout, "<ERROR> Can not locate item by index.\n"); } } fprintf(stdout, "\n"); return ; }
/* ** the return val can be modify in ptr_func */ int iter(t_list *list, t_zappy *s, int (*p_func)(t_zappy*, t_node*, t_type, int*)) { t_node *loop; int ret; ret = -1; loop = list->head; while (loop) { p_func(s, loop, list->type, &ret); loop = loop->next; } return (ret); }
int main() { char opr = 0; int num = 0, num1 = 0, res = 0; int (*p_func)(int, int) = NULL; printf("请输入表达式:"); scanf("%d%c%d", &num, &opr, &num1); if (opr == '+') { p_func = add; } else if (opr == '-') { p_func = sub; } else if (opr == '*') { p_func = mul; } else { p_func = div; } res = p_func(num, num1); printf("计算结果是%d\n", res); return 0; }
int main () { char line[128]; char cdum[128]; int i; m_p = m_p*c2 - 1.*m_e; // nuclear masses in MeV m_d = m_d*c2 - 1.*m_e; m_3he = m_3he*c2 - 2.*m_e; m_4he = m_4he*c2 - 2.*m_e; printf("\n"); printf(" _________________________________________ \r\n"); printf(" | |\r\n"); printf(" | A N M A G 1.0 |\r\n"); printf(" | |\r\n"); printf(" | Program to calculate the |\r\n"); printf(" | I(A) and B(T) for a certain |\r\n"); printf(" | particle type (p,d,3He,4He) |\r\n"); printf(" | with a certain kinetic energy T(MeV) |\r\n"); printf(" | |\r\n"); printf(" | Relativistic expressions are used: |\r\n"); printf(" | T = E - mc^2 and |\r\n"); printf(" | E^2= (pc)^2 + (mc^2)^2 |\r\n"); printf(" | |\r\n"); printf(" | E-mail : [email protected] |\r\n"); printf(" | Created : 25 Feb 2014 |\r\n"); printf(" | Modified: 08 Mar 2014 |\r\n"); printf(" |_________________________________________|\r\n"); printf(" \r\n"); fp = fopen("anmag_in.txt", "r"); if(fp == NULL){ printf("Could not open file anmag_in.txt, default values are used \n"); }else{ fgets_ignore(line,sizeof(line),fp); sscanf(line, " %d %f \n", &type,&T); } fclose(fp); printf("\nParticle type (p,d,3He,4He) = (1,2,3,4): <%1d>",type); fgets_ignore(line,sizeof(line),stdin); sscanf(line,"%d", &type); if(type==1){ q = 1.*e; mc2 = m_p; } if(type==2){ q = 1.*e; mc2 = m_d; } if(type==3){ q = 2.*e; mc2 = m_3he; } if(type==4){ q = 2.*e; mc2 = m_4he; } printf("\nParticle kinetic energy: <%7.3f>", T); fgets_ignore(line,sizeof(line),stdin); sscanf(line,"%f", &T); printf("\nMass %7.3e, charge %7.3e, type particle %d, and beam energy %6.3f",mc2,q,type,T); p = p_func( T, mc2); B = B_func( p, q, r); I = a0 + a1 * B; printf("\nMomentum p %7.3e, field B %7.3e, Coil currents (A) %f\n",p,B,I); fp = fopen("anmag_in.txt", "w"); if(fp == NULL){ printf("Could not open file anmag_in.txt \n"); }else{ fprintf(fp,"%d %f \n", type,T); } fclose(fp); fp = fopen("anmag_out.txt", "w"); for(i=1;i<=500;i++){ T=((float)i)/10.; p1=p_func(T,m_p); B1=B_func(p1,1.*e,r); I1 = a0 + a1 * B1; p2=p_func(T,m_d); B2=B_func(p2,1.*e,r); I2 = a0 + a1 * B2; p3=p_func(T,m_3he); B3=B_func(p3,2.*e,r); I3 = a0 + a1 * B3; p4=p_func(T,m_4he); B4=B_func(p4,2.*e,r); I4 = a0 + a1 * B4; fprintf(fp," %7.1f %7.3f %7.2f %7.3f %7.2f %7.3f %7.2f %7.3f %7.2f \n",T,B1,I1,B2,I2,B3,I3,B4,I4); } fclose(fp); return 0; }
void for_each(int *p_num, int size, void (*p_func)(int*, void*), void *p_v) { int num = 0; for (num = 0;num <= size - 1;num++) { p_func(p_num + num, p_v); } }