Новые сообщения · Правила  
  • Страница 1 из 1
  • 1
Модератор форума: Berestovskiy  
Форум ПРОГРАММИСТОВ » ПРОГРАММИРОВАНИЕ » С, C++ » Алготим программы
Алготим программы
Обучаюсь С. Есть такая вот программа, у меня проблема с пониманием, прошу помочь мне описать её алгоритм, принцип работы.
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
     
     
/********************************************************************/
#define MAX_LINE_SIZE           1024
#define LINES_DEF_SIZE          256
#define MAX_DIFF                        0.0001
     
     
/********************************************************************/
typedef struct {
          double x, y;
          int flag;
} point_flag_t;
     
typedef struct {
          int size, max_size;
          char **lines;
} objects_t;
     
#define objects_size(arg_pobj)                  ((arg_pobj)->size)
#define objects_line(arg_pobj,arg_i)    ((arg_pobj)->lines[(arg_i)])

Добавлено (20.04.10, 09:40)
---------------------------------------------

Code
/********************************************************************/
void do_process( const char *in_name, const char *out_name );
   
int read_objects_file( objects_t *pobj, const char *name );
void free_objects( objects_t *pobj );
   
int parse_x_y( const char *buf, double *px, double *py );
   
   
/********************************************************************/
int main( int argc, char *argv[] ) {
   
argc--; argv++;
   
if ( argc != 2 ) {
         fprintf( stderr, "Usage: search in.txt out.txt" );
         exit( 2 ) ;
}
   
do_process( argv[0], argv[1] );
return 0;
   
} /* main() */
   
   
/********************************************************************/
void do_process( const char *in_name, const char *out_name ) {
   
int i, j;
int errcode;
   
int line_size;
char *line;
   
int obj_size;
objects_t obj;
   
double x, y;
point_flag_t *ppoint= NULL;
FILE *fout= NULL;

Добавлено (20.04.10, 09:41)
---------------------------------------------

Code
/* Read */
if ( read_objects_file( &obj, in_name ) ) { goto err_read; }
   
/* Check */
obj_size= objects_size( &obj );
if ( obj_size == 0 ) { goto err_no_obj; }
   
/* Allocate points */
ppoint= (point_flag_t*)malloc( obj_size*sizeof(point_flag_t) );
if ( ppoint == NULL ) { goto err_nomem; }
   
/* Parse x,y and setup point */
for ( i= 0; i<obj_size; i++ ) {
         if ( parse_x_y( objects_line( &obj, i ), &x, &y ) ) { goto err_format; }
         ppoint[i].x= x; ppoint[i].y= y;
         ppoint[i].flag= 0;
}
   
/* Compare loop */
for ( i= 0; i<obj_size; i++ ) {
         for ( j= i+1; j<obj_size; j++ ) {
                 if ( fabs( ppoint[i].x-ppoint[j].x )<=MAX_DIFF &&
                         fabs( ppoint[i].y-ppoint[j].y )<=MAX_DIFF ) {
                         ppoint[i].flag= ppoint[j].flag= 1;
                         /* printf( "near %d %d\n", i, j ); */
                         break;
                 }
         }
}
   
/* Create out */
fout= fopen( out_name, "wb" );
if ( fout == NULL ) { goto err_create; }

Добавлено (20.04.10, 09:41)
---------------------------------------------

Code
/* Write out loop */
for ( i= 0; i<obj_size; i++ ) {
         if ( !ppoint[i].flag ) { continue; }
   
         line= objects_line( &obj, i );
         line_size= strlen( line );
         errcode= fprintf( fout, "%s\n", line );
         if ( line_size+1 != errcode ) { goto err_write; }
   
}
   
/* Close out */
if ( fclose( fout ) ) { goto err_close; }
fout= NULL;
   
/* Free ppoint */
free( ppoint ); ppoint= NULL;
   
return;
   
   
/* Processing errors */
err_read:
fprintf( stderr, "Error reading file\n" );
goto err_all;
   
err_no_obj:
fprintf( stderr, "No objects\n" );
goto err_all;
   
err_nomem:
fprintf( stderr, "No memory\n" );
goto err_all;
   
err_format:
fprintf( stderr, "Invalid format\n" );
goto err_all;
   
err_create:
fprintf( stderr, "Error creating file\n" );
goto err_all;
   
err_write:
fprintf( stderr, "Error writing file\n" );
goto err_all;
   
err_close:
fprintf( stderr, "Error closing file\n" );
goto err_all;
   
err_all:
exit( 1 );
   
} /* do_process() */

Добавлено (20.04.10, 09:42)
---------------------------------------------

Code
/********************************************************************/
int read_objects_file( objects_t *pobj, const char *name ) {
   
int buf_size, max_size;
char buf[MAX_LINE_SIZE], *line, **lines;
FILE *fin= NULL;
   
   
/* Init */
pobj->size= 0; pobj->max_size= 0;
pobj->lines= NULL;
   
/* Open */
fin= fopen( name, "r" );
if ( fin == NULL ) { goto err_read; }
   
/* Loop */
for ( ; ; ) {
          
         /* Read line */
         if ( fgets( buf, sizeof(buf), fin ) == NULL ) {
                 if ( feof( fin ) ) { break; }
                 goto err_read;
         }
         buf_size= strlen( buf );
         if ( buf_size>0 && buf[buf_size-1] == '\n' ) { buf[--buf_size]= '\0'; }
   
         /* Check space and realloc */
         if ( pobj->size >= pobj->max_size ) {
   
                 max_size= (pobj->max_size!=0) ? 2*pobj->max_size : LINES_DEF_SIZE;
                 lines= (char**)realloc( pobj->lines, max_size*sizeof(char*) );
                 if ( lines == NULL ) { goto err_nomem; }
                          
                 pobj->max_size= max_size; pobj->lines= lines;
   
         }

Добавлено (20.04.10, 09:42)
---------------------------------------------

Code
/* Store */
         line= malloc( buf_size+1 );
         if ( line == NULL ) { goto err_nomem; }
         memcpy( line, buf, buf_size+1 );
         pobj->lines[pobj->size]= line; pobj->size++;
   
}
   
/* Close */
if ( fclose( fin ) ) { goto err_close; }
fin= NULL;
   
/* Return */
return 0;
   
   
/* Processing errors */
err_read:
err_close:
err_nomem:
return 1;
   
} /* read_objects_file() */
   
   
/********************************************************************/
void free_objects( objects_t *pobj ) {
   
int i;
   
   
for ( i= 0; i<pobj->size; i++ ) {
         if ( pobj->lines[i] != NULL ) { free( pobj->lines[i] ); }
}
free( pobj->lines );
   
pobj->size= pobj->max_size= 0;
pobj->lines= NULL;
          
} /* free_objects() */
   
  

Добавлено (20.04.10, 09:42)
---------------------------------------------

Code
/********************************************************************/
int parse_x_y( const char *buf, double *px, double *py ) {
   
int buf_size;
char *endptr;
   
   
/* Check line */         
buf_size= strlen( buf );
if ( buf_size != 408 ) { return 1; }
   
*px= strtod( &buf[238], &endptr );
if ( *px == 0.0 ) {
         if ( &buf[238] == endptr ) { return 1; }
}
   
*py= strtod( &buf[252], &endptr );
if ( *py == 0.0 ) {
         if ( &buf[252] == endptr ) { return 1; }
}
   
return 0;
   
} /* parse_x_y() */
  

Добавлено (20.04.10, 10:02)
---------------------------------------------
Заданием программы было считать данные с текстового файла, эти данные содержат координаты для каждого определённого объекта, затем посредством сравнения данных между собой нужно было найти те данные, координаты X и Y которых были практическими схожими, допускалось различие в 0.0001 между X-ми и Y-ми. В итог мы должны получить список схожих значений. после отсеивания. Сама программа рабочая

Добавлено (20.04.10, 10:03)
---------------------------------------------
Вот сюда ещё залил на всякий http://www.megaupload.com/?d=I3YOYJE6

1 | Автор: АдексейКо | 2010-04-20, 10:03 | Изменено: АдексейКо - Вт, 2010-04-20, 09:39   |  Репутация: [ + 0 ]
Форум ПРОГРАММИСТОВ » ПРОГРАММИРОВАНИЕ » С, C++ » Алготим программы
  • Страница 1 из 1
  • 1
Поиск: