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

📄 amdym.c

📁 ADaM is a data mining and image processing toolkit
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (val == 0.0) ivec_set( iv, i, 0);    else if (val == 1.0) ivec_set( iv, i, 1);    else {      my_errorf( "mk_ivec_from_binary_dym_column: row=%d, col=%d, val=%f, "                 "not binary.", i, col, val);    }  }  return iv;}void fprintf_dym(FILE *s, const char *m1, const dym *d, const char *m2){  if ( d == NULL ){    fprintf(s,"%s = (dym *)NULL%s",m1,m2);  }  else if ( d->rows <= 0 || d->cols <= 0 )    fprintf(s,"%s = <Dym with %d row%s and %d column%s>%s",            m1,d->rows,(d->rows==-1)?"":"s",            d->cols,(d->cols==-1)?"":"s",m2           );  else  {    int i;    buftab bt;    init_buftab(&bt,d->rows,d->cols + 4);    for ( i = 0 ; i < d->rows ; i++ )    {      int j;      set_buftab(&bt,i,0,(i == (d->rows-1)/2) ? m1 : "");      set_buftab(&bt,i,1,(i == (d->rows-1)/2) ? "=" : "");      set_buftab(&bt,i,2,"[");      for ( j = 0 ; j < d -> cols ; j++ )      {        char buff[100];        sprintf(buff," %g ",d->tdarr[i][j]);        set_buftab(&bt,i,3+j,buff);      }      set_buftab(&bt,i,3+d->cols,"]");    }    fprint_buftab(s,&bt);    free_buftab_contents(&bt);  }  fprintf(s,"\n");} /** Read the values from the file and set them in dym  *  fp: File pointer at the beginning of the file *  d : allocated dym */int assign_dym_values(PFILE* fp, dym* d){  int rc, j, nrows, ncols, rowno, lineno;  double v;  char *line, *s, *endptr;  string_array *sa;  rc = 0;  if (fp == NULL) my_error( "assign_dym_values: FILE *fp=NULL.");  if (d == NULL) my_error( "assign_dym_values: dym *d=NULL.");  nrows = dym_rows( d);  ncols = dym_cols( d);  lineno = 0;  rowno = -1;  while (!pfeof(fp)) {    /* Get line. */    line = mk_readline( fp);    /* Check it is defined. */    if (line==NULL) {      if (pfeof(fp)) break; /* Read eof. */      else my_error( "assign_dym_values: mk_readline() returned NULL.");    }    /* Line is happy. */    lineno += 1;    /* Skip comments. */    if (is_comment(line)) {      free( line);      continue;    }    /* Line is not a comment. */    rowno += 1;    sa = mk_broken_string_using_seppers( line, (const char*) delim);    if (string_array_size(sa) != ncols) {      printf( "assign_dym_values: bad line:\n%s\n", line);      my_errorf( "assign_dym_values: line %d has %d atts, but the data file\n"                 "appears to have %d atts.",                 lineno, string_array_size(sa), ncols);    }    /* Parse numbers from line. */    for (j=0; j<ncols; ++j) {      s = string_array_ref(sa,j);      v = strtod( string_array_ref(sa,j), &endptr);      if(errno == ERANGE) {        printf( "assign_dym_values: bad line:\n%s\n", line);        my_errorf( "assign_dym_values: value '%s' cause over- or underflow.\n"                   "line=%d, att=%d\n", s, lineno, j+1);      }      if (endptr == s) {        printf( "assign_dym_values: bad line:\n%s\n", line);        my_errorf( "assign_dym_values: failed to convert '%s' to a number\n"                   "line=%d, att=%d\n", s, lineno, j+1);      }      /* Set dym value. */      v =d->tdarr[rowno][j] = v;    }    /* Done with line and tokens. */    free_string_array( sa);    free( line);  }  if (rowno+1 != nrows) {    my_errorf( "assign_dym_values: read %d lines of data, but expected %d from"               " first pass.\n",               rowno+1, nrows);  }  return rc;} /** Read the values from the file and set them in dym  *  fp: File pointer at the beginning of the file *  factors: allocated dym *  outputs: allocated dyv */int assign_dym_values_for_csv( PFILE* fp, dym *factors, dyv *outputs){  int rc, j, nrows, ncols, rowno, lineno;  double v;  char *line, *s, *endptr;  string_array *sa;  rc = 0;  if (fp == NULL) my_error( "assign_dym_values_for_csv: FILE *fp=NULL.");  if (factors == NULL) my_error( "assign_dym_values_for_csv: dym *d=NULL.");  if (outputs == NULL) my_error( "assign_dym_values_for_csv: dym *d=NULL.");  nrows = dym_rows( factors);  ncols = dym_cols( factors);  lineno = 0;  rowno = -1;  while (!pfeof(fp)) {    /* Get line. */    line = mk_readline( fp);    /* Check it is defined. */    if (line==NULL) {      if (pfeof(fp)) break; /* Read eof. */      else my_error( "assign_dym_values_for_csv: mk_readline() returned "                     "NULL.");    }    /* Line is happy. */    lineno += 1;    /* Skip comments. */    if (is_comment(line)) {      free( line);      continue;    }    /* Line is not a comment. */    rowno += 1;    sa = mk_broken_string_using_seppers( line, (const char*) delim);    if (string_array_size(sa) != ncols+1) {      printf( "assign_dym_values_for_csv: bad line:\n%s\n", line);      my_errorf( "assign_dym_values_for_csv: line %d has %d values, but we\n"                 "thought the data file had %d values per line.",                 lineno, string_array_size(sa), ncols+1);    }    /* Parse numbers from line. */    for (j=0; j < (ncols+1); ++j) {      s = string_array_ref(sa,j);      v = strtod( string_array_ref(sa,j), &endptr);      if(errno == ERANGE) {        printf( "assign_dym_values_for_csv: bad line:\n%s\n", line);        my_errorf( "assign_dym_values_for_csv: value '%s' cause over- or "                   "underflow.\n"                   "line=%d, att=%d\n", s, lineno, j+1);      }      if (endptr == s) {        printf( "assign_dym_values_for_csv: bad line:\n%s\n", line);        my_errorf( "assign_dym_values_for_csv: failed to convert '%s' to a "                   "number\n"                   "line=%d, att=%d\n", s, lineno, j+1);      }      /* Set value. */      if (j < ncols) dym_set( factors, rowno, j, v);      else dyv_set( outputs, rowno, v);    }    /* Done with line and tokens. */    free_string_array( sa);    free( line);  }  if (rowno+1 != nrows) {    my_errorf( "assign_dym_values_for_csv: read %d lines of data, but "               "expected %d from first pass.\n",               rowno+1, nrows);  }  return rc;}char *mk_readline(PFILE* fp ){   char *cptr, *in;   in = (char *) malloc(MAX_LINE_SIZE * sizeof(char));   if(in==NULL) {     cptr = NULL;     my_error( "mk_readline: Failed to allocate memory");   }   else {     cptr = pfgets(in, MAX_LINE_SIZE, fp);     if (cptr == NULL) free( in);   }   return cptr;}int find_number_of_columns(const char* line){  int ncols = 0;  if( line ){    string_array *sa = mk_broken_string_using_seppers(line,(const char*)delim);    ncols = string_array_size(sa);    free_string_array(sa);  }  return ncols;}int find_number_of_rows_and_columns(PFILE* fp , int *nrows , int *ncols ){  int cols;  char *line;  *ncols = 0;  *nrows = 0;  while( ! pfeof(fp) ){    line = mk_readline(fp);    if(line) {      if (!is_comment(line)) {        cols = find_number_of_columns(line);        if( *ncols < cols) *ncols = cols;        ++(*nrows);      }      free(line);    }  }  return 0;}int is_comment(const char* line){  /* Check if first non-whitespace char is '#'. */  size_t pos;  /* First non-whitespace char with C99 function strspn. */  pos = strspn( line, " \t");  if (pos == strlen(line)-1) {    /* First non-whitespace char is \n.  Treat as comment. */    return 1;  }  else if (line[pos] == '#') {    /* Comment. */    return 1;  }  else {    /* Not a comment. */    return 0;  }}/*************************************************************************//* TESTING                                                               *//*************************************************************************/#if 0int assign_value(dym* d){  if( d ){    int i = 0;    int j = 0;    for(i =0 ; i < d->rows; i++){      for(j=0 ; j < d->cols ; j++){	d->tdarr[i][j] = 1.5;      }    }  }  return 0;}int display_dym(dym* d){  if( d ){    int i = 0;    int j = 0;    for(i =0 ; i < d->rows; i++){      for(j=0 ; j < d->cols ; j++){	printf("%g ",d->tdarr[i][j] );      }      printf("\n");    }  }  return 0;}int display(PFILE *fp){  int r , c;  find_number_of_rows_and_columns(fp , &r ,&c);  printf(" display %d %d\n",r,c);  while (! pfeof(fp) ){    char* line = mk_readline(fp);    if(line){      printf("line %s\n",line);      printf("ncols %d\n",find_number_of_columns(line));      free(line);    }  }  return 0;}int main(int argc, char** argv){  dym* d = mk_read_dym("dym.out");  if( d ) {    display_dym(d);    free_dym(d);  }  return 0;}#endif

⌨️ 快捷键说明

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