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

📄 utils.c

📁 RoboCup 2D 仿真组冠军源代码之1998年冠军队——CMUnited98源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  d_frac = 1.0;  result = 0.0;  m_flag = d_flag = 0;  /* Advance to the beginning of the number */  while( !isdigit(**str_ptr) && **str_ptr!='-' && **str_ptr!='.')    (*str_ptr)++;  /* Process the number bit by bit */  while((**str_ptr!=')') && (**str_ptr!=' ') && (**str_ptr!='\n') && (**str_ptr!=',')){    if (**str_ptr=='.')      d_flag=1;    else if (**str_ptr=='-')      m_flag=1;    else if (d_flag){      d_frac *= 10.0;      result+=(float)(**str_ptr-'0')/d_frac;    }    else      result=result*10.0+(float)(**str_ptr-'0');    (*str_ptr)++;  }  if (m_flag)    result=-result;  //printf("%.1f\n",result);  return result;}/* Get the number, but don't advance pointer */float get_float(char *str){  char **str_ptr = &str;  return get_float(str_ptr);}/****************************************************************************/int get_int(char **str_ptr){  int result;  int m_flag;  result = 0;  m_flag = 0;  /* Advance to the beginning of the number */  while( !isdigit(**str_ptr) && **str_ptr!='-')    (*str_ptr)++;  /* Process the number bit by bit */  while((**str_ptr!=')') && (**str_ptr!=' ') && (**str_ptr!='\n') && (**str_ptr!=',')){    if (**str_ptr=='-')      m_flag=1;    else      result=result*10+(int)(**str_ptr-'0');    (*str_ptr)++;  }  if (m_flag)    result=-result;  return result;}int get_int(char *str){  char **str_ptr = &str;  return get_int(str_ptr);}/****************************************************************************/void get_word(char **str_ptr){  while ( !isalpha(**str_ptr) ) (*str_ptr)++;}/****************************************************************************/void get_next_word(char **str_ptr){  while ( isalpha(**str_ptr) ) (*str_ptr)++;  get_word(str_ptr);}/****************************************************************************/void advance_to(char c, char **str_ptr){  while ( **str_ptr != c ) (*str_ptr)++;}/****************************************************************************//* These routines are to save time instead of using sprintf or atof, etc.   *//* *str should point to the END of the string where the number is going     *//* return the length of the number placed in                                *//****************************************************************************/int put_float(char *str, float fnum, int precision){  int m_flag = 0, length = 0;  int num, old_num;  for (int i=0; i<precision; i++)    fnum *= 10;  num = closest_int(fnum);  /* round off the rest */  if ( precision == 0 )     return put_int(str,num);  if ( num < 0 ){    m_flag = 1;    num = -num;  }  old_num = num;  while ( num > 0 || length < precision ){    num /= 10;    *str = '0' + old_num - num*10;    old_num = num;    str--;    length++;    if ( length == precision ){      *str = '.';      str--;      length++;      if ( num == 0 ){	*str = '0';	str--;	length++;	break;      }    }  }  if ( m_flag ){    *str = '-';    length++;  }  return length;}/****************************************************************************/int put_int(char *str, int num){  int m_flag = 0, length = 0;  int old_num;  if ( num == 0 ){    *str = '0';    return 1;  }  if ( num < 0 ){    m_flag = 1;    num = -num;  }  old_num = num;  while ( num > 0 ){    num /= 10;    *str = '0' + old_num - num*10;    old_num = num;    str--;    length++;  }  if ( m_flag ){    *str = '-';    length++;  }  return length;}/****************************************************************************//****************************************************************************//****************************************************************************/void BubbleSort(int length, int *elements, float *keys){  /*  for (int a=0; a<length; a++){    printf("%d--%.1f ",elements[a], keys[a]);  }  printf("\n");*/  /* Sort the elements in increasing order according to the keys */  float keytemp;  int eltemp;  for (int i=0; i<length; i++){    for (int j=i+1; j<length; j++){      if ( keys[j] < keys[i] ){	keytemp = keys[i];	keys[i] = keys[j];	keys[j] = keytemp;	eltemp = elements[i];	elements[i] = elements[j];	elements[j] = eltemp;      }    }  }/*  for (int a=0; a<length; a++){    printf("%d--%.1f ",elements[a], keys[a]);  }  printf("\n");*/}/****************************************************************************/int BinarySearch(int length, float *elements, float key){    /* Assume the list is already sorted in increasing order */  int lbound = 0, ubound = length;    for ( int index = length/2; ubound-lbound > 0; index = lbound+(ubound-lbound)/2 ){    /* printf("%d ",index); */    if ( elements[index] == key ){      lbound = ubound = index;    }    else if ( elements[index] < key ){      lbound = index+1;    }    else {      ubound = index-1;    }  }   int toReturn = Max(ubound,lbound);  if (elements[toReturn] < key) toReturn++;  /* Guarantees >= key */  return toReturn;}/****************************************************************************/ /* replace all occurrences in a string */void StrReplace(char *str, char oldchar, char newchar){  int i=0;#if 0  int numReplaced;#endif  int strLength = strlen(str);  while ( i++ < strLength ){    if ( str[i] == oldchar ){      str[i] = newchar;#if 0      numReplaced++;#endif    }    if ( i==1000 )       my_error("String of length >1000?");  }#if 0  printf("***Replaced %d %c's in string of length %d (%d): %s***\n",	 numReplaced,oldchar,strlen(str),i,str);#endif}/****************************************************************************//***************************   random stuff    ******************************//****************************************************************************//* From Andrew's C package                                                  */int int_random(int n){  static int FirstTime = TRUE;    if ( FirstTime ){    /* initialize the random number seed. */    timeval tp;    gettimeofday( &tp, NULL );    srandom( (unsigned int) tp.tv_usec );    FirstTime = FALSE;  }  if ( n > 2 )    return( random() % n );  else if ( n == 2 )    return( ( (random() % 112) >= 56 ) ? 0 : 1 );  else if ( n == 1 )    return(0);  else  {    printf("int_random(%d) ?\n",n);    my_error("You called int_random(<=0)");    return(0);  }}float range_random(float lo, float hi){  int x1 = int_random(10000);  int x2 = int_random(10000);  float r = (((float) x1) + 10000.0 * ((float) x2))/(10000.0 * 10000.0);  return( lo + (hi - lo) * r );}int very_random_int(int n){  int result = (int) range_random(0.0,(float)n);  /* rounds down */  if ( result == n ) result = n-1;  return(result);}void GetStampedName( char *name, char *outputName ){  char date[100],weekday[10],month[10],temp[10];  int  day,hour,min,sec,year;  FILE *dateFile;    //system("date > ../date.log");        /* Put the date in a file */ /* done by a player */  dateFile = fopen("../date.log","r");  fscanf(dateFile,"%[^\n]",date);         /* Scan it in             */  fclose(dateFile);    sscanf(date,"%s %s %d %d:%d:%d %s %d",	 weekday,month,&day,&hour,&min,&sec,temp,&year);  sprintf(name,"%s-%s%d-%d:%d:%d.dat",outputName,month,day,hour,min,sec);}

⌨️ 快捷键说明

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