⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ambs.c

📁 ADaM is a data mining and image processing toolkit
💻 C
📖 第 1 页 / 共 2 页
字号:
    else      printf("commandline %s\n",(result)?"TRUE":"FALSE");  }  return(result);}int my_irint(double x)/* Returns the closest integer to x */{  int returner = (int)floor(x + 0.5);  return returner;}void fprintf_int(FILE *s,char *m1,int x,char *m2){  fprintf(s,"%s = %d%s",m1,x,m2);}void fprintf_realnum(FILE *s,char *m1,double x,char *m2){  fprintf(s,"%s = %g%s",m1,x,m2);}void fprintf_float(FILE *s,char *m1,double x,char *m2){  fprintf_realnum(s,m1,x,m2);}void fprintf_double(FILE *s,char *m1,double x,char *m2){  fprintf_realnum(s,m1,x,m2);}void fprintf_bool(FILE *s,char *m1,bool x,char *m2){  fprintf(s,"%s = %s%s",m1,(x)?"True":"False",m2);}void fprintf_string(FILE *s,char *m1,char *x,char *m2){  fprintf(s,"%s = \"%s\"%s",m1,x,m2);}int index_of_char(char *s,char c)/*   Returns the least index i such that s[i] == c.   If no such index exists in string, returns -1*/{  int result = -1;  if ( s != NULL )  {    int i;    for ( i = 0 ; result < 0 && s[i] != '\0' ; i++ )      if ( s[i] == c ) result = i;  }  return(result);}bool char_is_in(char *s,char c){  bool result = index_of_char(s,c) >= 0;  return(result);}/* This returns the number of occurences of character c in string s.    It is legal for s to be NULL (in which case the result will be 0).      Added by Mary on 8 Dec 96.*/int num_of_char_in_string(const char *s, char c){  int res = 0;  if (s != NULL)  {    int i;    for (i = 0; i < (int) strlen(s); i++)      if (s[i] == c) res++;  }  return(res);}FILE *am_fopen(char *filename,char *mode){  FILE *s = NULL;  bool all_whitespace = TRUE;  int i;  if ( filename == NULL )	  my_error("am_fopen: Called with filename == NULL");    for ( i = 0 ; filename[i] != '\0' && all_whitespace ; i++ )	all_whitespace = filename[i] == ' ';  if ( !all_whitespace )	s = fopen(filename,mode);  return(s);}bool is_all_digits(const char *string){        if (!string || !*string)                return FALSE;  /* null pointer, or pointer to null char */        while (*string)                if (!isdigit(*string++))                        return FALSE;  /* found a non-digit! */        return TRUE;  /* must all be digits */}bool is_a_number(const char *string)/*   uses a finite state machine with 8 states.   These are the symbols in the FSM alphabet:    S     + or -    P     .    D     0 or 1 or .. or 9    E     e or E    X     anything else  In the following descriptions of states, any unmentioned  symbol goes to an absorbing, non-accepting, error state       State 1: S --> 2 , D --> 4 , P --> 3       State 2: P --> 3 , D --> 4        State 3: D --> 5    *  State 4: D --> 4 , E --> 6 , P --> 5    *  State 5: D --> 5 , E --> 6       State 6: D --> 8 , S --> 7       State 7: D --> 8    *  State 8: D --> 8  The starred states are acceptors.*/{  int state = 1;  int i = 0;  bool err_state = FALSE;  bool result;  while ( string[i] != '\0' && !err_state )  {    char c = string[i];    char symbol = ( c == '.' ) ? 'P' :                  ( c == 'e' || c == 'E' ) ? 'E' :                  ( c == '+' || c == '-' ) ? 'S' :                  ( c >= '0' && c <= '9' ) ? 'D' : 'X';/*    printf("i = %d , c = %c , symbol = %c , state = %d\n",i,c,symbol,state);*/    if ( state == 1 )    {      if ( symbol == 'S' ) state = 2;      else if ( symbol == 'D' ) state = 4;      else if ( symbol == 'P' ) state = 3;      else err_state = TRUE;    }    else if ( state == 2 )    {      if      ( symbol == 'P' ) state = 3 ;      else if ( symbol == 'D' ) state = 4 ;      else err_state = TRUE;    }    else if ( state == 3 )    {      if      ( symbol == 'D' ) state = 5;      else err_state = TRUE;    }    else if ( state == 4 )    {      if      ( symbol == 'D' ) state = 4 ;      else if ( symbol == 'E' ) state = 6 ;      else if ( symbol == 'P' ) state = 5;      else err_state = TRUE;    }    else if ( state == 5 )    {      if      ( symbol == 'D' ) state = 5 ;      else if ( symbol == 'E' ) state = 6;      else err_state = TRUE;    }    else if ( state == 6 )    {      if      ( symbol == 'D' ) state = 8 ;      else if ( symbol == 'S' ) state = 7;      else err_state = TRUE;    }    else if ( state == 7 )    {      if      ( symbol == 'D' ) state = 8;      else err_state = TRUE;    }    else if ( state == 8 )    {      if      ( symbol == 'D' ) state = 8;      else err_state = TRUE;    }    i += 1;  }  result = !err_state && ( state==4 || state==5 || state==8 );  return(result);}bool bool_from_string(char *s,bool *r_ok){  bool result = FALSE;  *r_ok = FALSE;  if ( s != NULL && s[0] != '\0' )  {    char c = s[0];    if ( c >= 'a' && c <= 'z' ) c += 'A' - 'a';    if ( c == 'Y' || c == 'T' || c == '1' )    {      *r_ok = TRUE;      result = TRUE;    }    if ( c == 'N' || c == 'F' || c == '0' )    {      *r_ok = TRUE;      result = FALSE;    }  }  return(result);}/*------------------------------------------------*/int int_from_string(char *s,bool *r_ok){  int result;  if ( is_a_number(s) )  {    result = atoi(s);    *r_ok = TRUE;  }  else  {    result = 0;    *r_ok = FALSE;  }  return(result);}/*------------------------------------------------*/double double_from_string(char *s,bool *r_ok){  double result;  if ( is_a_number(s) )  {    result = atof(s);    *r_ok = TRUE;  }  else  {    result = 0;    *r_ok = FALSE;  }  return(result);}void sensible_limits( double xlo, double xhi, double *res_lo, double *res_hi, double *res_delta ){  double scale,rel_hi,rel_delta;  if ( xlo > xhi )  {    double temp = xlo; xlo = xhi; xhi = temp;  }  if ( xhi - xlo < 1e-50 )  {    double xmid = (xlo + xhi)/2.0;    xlo = xmid - 1e-50;    xhi = xmid + 1e-50;  }      scale = pow(10.0,ceil(-log10(xhi - xlo)));  rel_hi = scale * (xhi - xlo);  rel_delta = ( rel_hi < 1.5 ) ? 0.2 :              ( rel_hi < 2.5 ) ? 0.5 :              ( rel_hi < 5.0 ) ? 1.0 :              2.0;  *res_delta = rel_delta / scale;  *res_lo = *res_delta * floor(xlo / *res_delta);  *res_hi = *res_delta * ceil(xhi / *res_delta);}/* This version is about 3 times as fast.  -jab  */int next_highest_power_of_two(int n){  int result;  if (n<=0) return 0;  if (n<=1) return 1;  if (n<=2) return 2;  if (n<=4) return 4;  if (n<=8) return 8;  if (n<=16) return 16;  if (n<=32) return 32;  if (n<=64) return 64;  if (n<=128) return 128;  if (n<=256) return 256;  if (n<=512) return 512;  if (n<=1024) return 1024;  if (n<=2048) return 2048;  if (n<=4096) return 4096;  if (n<=8192) return 8192;  if (n<=16384) return 16384;  if (n<=32768) return 32768;  result = 65536;  n--;  n >>= 16;    while (n)  {    result <<= 1;    n >>= 1;  }  return result;}/* Returns TRUE if and only if x is NaN or Inf (i.e. returns FALSE   if and only if x is a completely legal number) */         bool is_ill_defined(double x){  return am_isnan(x);}/* Returns TRUE iff n is a power of two. Note that 0 is not defined   to be a power of two. */bool is_power_of_two(int x){  bool result = x > 0;  while ( result && x > 1 )  {    result = (x & 1) == 0;    x = x >> 1;  }  return result;}/*------------------ user interaction code ------------------------*//* by default the uimode is kUI_Shell (interact with the user at the   command shell).*/void Global_set_ui_mode(ui_mode mode){    if (mode >= 0 && mode < kNumUIModes)    {	Global_user_interaction_mode = mode;    }    else    {	fprintf(stderr, "Error in set_ui_mode(%d).  %d is an invalid "		"mode number.\n", mode, mode);    }}int Global_get_ui_mode(void){    return Global_user_interaction_mode;}/* -------------------*//* ---------------Wait for key code-------------------- *//*main functions - called by outside world*/void wait_for_key(void){    if ( Ignore_next_n > 0 )	Ignore_next_n -= 1;    else 	do_wait_for_key(FALSE);}void really_wait_for_key(void){     do_wait_for_key(TRUE);}/*decide how to handle the wait*/void do_wait_for_key(bool really_wait){    if ( really_wait || Verbosity >= 0.0 )     {	switch (Global_get_ui_mode())	{	  case kUI_Shell:	      wait_for_key_shell(really_wait);	      break;	  case kUI_NoInput:	      printf("skipping wait_for_key in batch mode.\n");	      break;	  default:	      break; /*should not get here!*/	}    }}/*kUI_Shell (text based) wait method */void wait_for_key_shell(bool really_wait){  int c = ' ';  int n = 0;  /*instructions to user*/  if (really_wait)      printf("really_wait_for_key(): [Return to continue or q to quit]");  else      printf("wait_for_key(): [Return to continue or h for other options]\n");  /*get user input */  while ( c != '\n' )  {      c = getchar();      if (!really_wait)      {	  /*process menu choice (really_wait has no menu)*/	  /*num waitpoints to skip*/          if ( c >= '0' && c <= '9' )          {	      n = 10 * n + c - '0';          }	  /*verbosity*/	  else if ( c == 'v' || c == 'V' ) 	  {	      char buff[100];	      int i = 0;	      c = getchar();	      while ( i < 99 && c != '\n')	      {		  buff[i] = c;		  buff[i+1] = '\0';		  i += 1;		  c = getchar();	      }	      printf("Old Verbosity: %g\n",Verbosity);	      Verbosity = atof(buff);	      printf("New Verbosity: %g\n",Verbosity);	  }	  /*help*/	  else if ( c == 'h' || c == 'H' )	  {	      printf("Enter:\n");	      printf("   <Return>          To continue past this wait point.\n");	      printf("   <number> <Return> To continue past this and the next\n");	      printf("                     <number> wait points.\n");	      printf("   q (or Q)          To exit the program entirely.\n");	      printf("   b (or B)          Call my_breakpoint\n");	      printf("   h (or H)          To read this help menu.\n");	      printf("   v <number>        To change the Verbosity level. The\n");	      printf("                     higher the verbosity level the more\n");	      printf("                     information will be printed on your\n");	      printf("                     terminal (and the more wait points).\n");	      while ( c != '\n') c = getchar();	      c = ' ';	  }      }      if ( c == 'q' || c == 'Q' )      {	  printf("You hit Q at a wait point. Exiting program\n");	  exit(0);      }  }  /* Andrew writes: I want to still fast forward through ordinary     wait_for_key()'s even if I've asked to skip some but I'm at a     really_wait_for_key. So I only want to overwrite n if at a     regular wait. I have a feeling someone wanted things the other     way, so feel free to debate this crucial point with me!! */  if ( !really_wait )    Ignore_next_n = n;}/* ---------------End wait for key code-------------------- *//* -------------- End get user input code ----------------- *//* -------------- buftab stuff used by both fprintf_dyv and fprintf_dym  ----------------- *//* (was static in amdym.c) */char *bufstr(buftab *bt,int i,int j){  char *result;  if ( i < 0 || i >= bt->rows || j < 0 || j >= bt->cols )  {    result = NULL;    my_error("bufstr()");  }  else    result = bt->strings[i][j];  if ( result == NULL )    result = "-";  return(result);}void fprint_buftab( FILE *s, buftab *bt){  int *widths = AM_MALLOC_ARRAY(int,bt->cols);  int i,j;  for (i=0; i < bt->cols; ++i) widths[i] = 0;  for ( i = 0 ; i < bt->rows ; i++ )    for ( j = 0 ; j < bt->cols ; j++ )      widths[j] = int_max(widths[j],strlen(bufstr(bt,i,j)));  for ( i = 0 ; i < bt->rows ; i++ )    for ( j = 0 ; j < bt->cols ; j++ )    {      char ford[20];      sprintf(ford,"%%%ds%s",widths[j],(j==bt->cols-1) ? "\n" : " ");      fprintf(s,ford,bufstr(bt,i,j));    }  AM_FREE_ARRAY(widths,int,bt->cols);}void init_buftab( buftab *bt, int rows, int cols){  if ( rows < 0 || cols < 0 )    my_error("init_buftab()");  else  {    int i,j;    bt -> rows = rows;    bt -> cols = cols;    bt -> strings = AM_MALLOC_ARRAY(char_ptr_ptr,rows);    for ( i = 0 ; i < rows ; i++ )      bt->strings[i] = AM_MALLOC_ARRAY(char_ptr,cols);    for ( i = 0 ; i < rows ; i++ )      for ( j = 0 ; j < cols ; j++ )        bt->strings[i][j] = NULL;  }}void free_buftab_contents(buftab *bt){  int i,j;  for ( i = 0 ; i < bt->rows ; i++ )    for ( j = 0 ; j < bt->cols ; j++ )      if ( bt->strings[i][j] != NULL )        AM_FREE_ARRAY(bt->strings[i][j],char, strlen(bt->strings[i][j]) + 1);  for ( i = 0 ; i < bt->rows ; i++ )    AM_FREE_ARRAY(bt->strings[i],char_ptr,bt->cols);      AM_FREE_ARRAY(bt->strings,char_ptr_ptr,bt->rows);}void set_buftab( buftab *bt, int i, int j, const char *str){  if ( i < 0 || i >= bt->rows || j < 0 || j >= bt->cols )    my_error("set_buftab()");  else if ( bt->strings[i][j] != NULL )    my_error("set_buftab: non null string");  else    bt->strings[i][j] = make_copy_string(str);}/*-----------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -