Newsgroup: it.comp.lang.c
Titolo: Aiuto !!

ciao a tutti
premetto che di C non conosco assolutamente niente, lavoro in altri linguaggi.
mi ritrovo fra le mani un programma in C da far girare su un AS400 ma non riesco a capire la sintassi dei parametri che deve
ricevere.
Domando prima di venire infamato :
Se posto per intero il programma ( circa 15KB ) potete aiutarmi a capire la sintassi dei parametri ?
Grazie in anticipo

(:-))
Ecco il programma, mi potete spiegare la sintassi dei parametri in ingresso ?
Grazie in anticipo
/*
     KNAP.C  by  Kai Risku, 1992, 1993
     Crunching program to solve knapsack problem.
  */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
/* Round "a" up to nearest higher multiple of "n" */
#define ROUND_UP(a,n) (((a)+((n)-(1))) - ((a)+((n)-1)) % n)
#define VER "2.0"
#define OPT_NOGROUP     0x01
#define OPT_OVERFLOW    0x02
#define OPT_VERBOSE     0x04
struct filespec
{
     long size;
     char *name;
     int dest;
     struct filespec *next;
};
typedef struct
{
     int options;            /* bitfield options */
     int sizepos;            /* position of size */
     long items;             /* number of items */
     long threshold;         /* accept fit when space<=threshold */
     long quant;             /* round up itemsizes to nearest multiple of quant */
     long total;             /* total space to be filled */
     struct filespec *firstspec;
                             /* following used in recur: */
     int  kbhit;
     long recleft;
     int  disk;
     int  maxlevel;
} globstr;
/* Global variables */
struct filespec **specs;        /* pointer to list of filespec pointers */
int *fits;                      /* used in "fitting" files */
globstr glob;                   /* global data & options */
unsigned _stklen = 32000;       /* set a bigger stack */
void usage( void );
long get_item_info( void );
int  get_cmd_line( int ac, char *av[] );
void do_fit( void );
void do_disk( int diskno, int maxdisk );
int  sort_seq( void );
int  sort_big( void );
int  recur( long space, struct filespec *this, int level );
void freeall( void );
void flag_int( int  );
int  split_line( char *s, char *args[] );
void *strdup(char *strsource) ;
void *strdup(char *strsource) {
  char *dupsource = NULL;
  if (strsource) {
    dupsource = calloc(strlen(strsource)+1, 1);
    memcpy(dupsource, strsource, strlen(strsource));
  }
  return dupsource;
}
void usage( void )
{
     fputs( "KNAP ver "VER", "__DATE__" by Kai Risku\n\n"
            "KNAP tries to optimally fit the items with\n"
            "associated sizes into bins of the given targetsize.\n\n"
            "Usage:   KNAP [options] targetsize [< file]\n\n"
            "Options: -n       no opt; files assigned in given order\n"
            "         -o       allow overflow (greater than targetsize)\n"
            "         -v       verbose; output more info to stderr\n"
            "         -p val   set position of itemsize in inputdata, default is 2\n"
            "         -t val   set threshold to val, default is 0\n"
            "         -q val   set quantizing to val, default is 1\n"
             , stderr );
     exit( 1 );
}
int main( int argc, char *argv[] )
{
     int i, disks, wid;
     if( argc==1 )
     {
         usage();
         exit( 0 );
     }
     if( get_cmd_line( argc, argv )==0 )
         exit( 1 );                 /* error on command line */
     glob.items = get_item_info();
     if( glob.items==0L )
         fputs( "Nothing to do!\n", stderr );
     else
         do_fit();
     freeall();
     return( 0 );
}
int get_cmd_line( int ac, char *av[] )
{
     int ok, n, i;
     ok = 1;
     glob.options = 0;                   /* defaults */
     glob.total = 0L;
     glob.items = glob.threshold = 0L;
     glob.quant = 1L;
     glob.sizepos = 2;
     for( n=1; n<ac; n++ )
     {
         if( av[n][0]=='-' )
         {
             i = 1;
             while( av[n][i] )
             {
                 switch( av[n][i] )         /* options */
                 {
                     case 'n':   glob.options |= OPT_NOGROUP;
                                 break;
                     case 'o':   glob.options |= OPT_OVERFLOW;
                                 break;
                     case 'v':   glob.options |= OPT_VERBOSE;
                                 break;
                     case 'p':   if( av[n][i+1]=='\0' )
                                     ok = sscanf( av[++n], "%d", &glob.sizepos );
                                 else
                                     ok = sscanf( &av[n][i+1], "%d", &glob.sizepos );
                                 if( ok==0 )
                                     fprintf( stderr, "Error in option %s\n", av[n] );
                                 i = strlen( av[n] ) - 1;
                                 break;
                     case 't':   if( av[n][i+1]=='\0' )
                                     ok = sscanf( av[++n], "%ld", &glob.threshold );
                                 else
                                     ok = sscanf( &av[n][i+1], "%ld", &glob.threshold );
                                 if( ok==0 )
                                     fprintf( stderr, "Error in option %s\n", av[n] );
                                 i = strlen( av[n] ) - 1;
                                 break;
                     case 'q':   if( av[n][i+1]=='\0' )
                                     ok = sscanf( av[++n], "%ld", &glob.quant );
                                 else
                                     ok = sscanf( &av[n][i+1], "%ld", &glob.quant );
                                 if( ok==0 )
                                     fprintf( stderr, "Error in option %s\n", av[n] );
                                 i = strlen( av[n] ) - 1;
                                 break;
                     default:    fprintf( stderr, "Unknown option in %s\n",
                                             av[n] );
                                 ok = 0;
                                 break;
                 }
                 i++;
             }
         }
         else
         {
             if( glob.total!=0L )
                 fprintf( stderr, "Ambiguous targetsize!\n" );
             else
             {
                 ok = sscanf( av[n], "%ld", &glob.total );
                 if( ok==0 )
                     fprintf( stderr, "Invalid targetsize '%s'!\n", av[n] );
             }
         }
     }
     if( glob.total==0L && ok==1 )
     {
         ok = 0;
         fputs( "No size given!\n", stderr );
     }
     glob.total = ROUND_UP( glob.total, glob.quant );
     return( ok );
}
/* here we read the itemnames and sizes into a linked list */
long get_item_info( void )
{
     int i, p;
     long size, qty;
     char str[81], str2[81], *args[64];
     struct filespec *prev, *node, *new;
     qty = 0L;
     glob.firstspec = (struct filespec *) calloc( 1, sizeof(struct filespec) );
     prev = node = glob.firstspec;
     fgets( str, 81, stdin );
     while( !feof(stdin) )
     {
         if( str[strlen(str)-1]=='\n' )
             str[strlen(str)-1] = 0;
         strcpy( str2, str );
         i = split_line( str2, args );
         if( glob.sizepos>=0 )
             p = glob.sizepos;
         else
             p = i+glob.sizepos+1;
         if( sscanf( args[p-1], "%ld", &size )!=1 )
             size = -1L;
         if( i>=p && size<=glob.total && size>=0L )
         {
             new = (struct filespec *) calloc( 1, sizeof(struct filespec) );
             new->name = strdup( str );
             new->size = ROUND_UP( size, glob.quant );
             new->next = prev->next;
             prev->next = new;
             prev = new;
             qty++;
         }
         else
         {
             if( glob.options&OPT_VERBOSE )
             {
                 fprintf( stderr, "Skipped line: '%s'\n", str );
                 if( i<p )
                 {
                     fprintf( stderr, "Too few arguments in line!\n" );
                 }
                 else
                 {
                     if( size>glob.total )
                         fprintf( stderr, "Itemsize exceeds targetsize!\n" );
                     else
                         fprintf( stderr, "Invalid itemsize: %s\n", args[glob.sizepos-1] );
                 }
             }
         }
         fgets( str, 81, stdin );
     }
     fits = (int *)calloc( qty, sizeof(int) );
     specs = (struct filespec **)calloc( qty, sizeof(struct filespec*) );
     i = 0;
     node = glob.firstspec->next;
     while( node!=NULL )
     {
         fits[i] = -1;
         specs[i] = node;
         node = node->next;
         i++;
     }
     return( qty );
}
void do_fit( void )
{
     int dd, i;
     long totbytes;
     struct filespec *node;
     totbytes = 0L;
     for( i=0; i<glob.items; i++ )
         totbytes += specs[i]->size;
     if( glob.options&OPT_NOGROUP )
         dd = sort_seq();
     else
         dd = sort_big();
     for( i=1; i<=dd; i++)
         do_disk( i, dd );
}
void do_disk( int diskno, int maxdisk )
{
     int i;
     long l, t;
     puts( "" );
     printf( "# Bin %d of %d:\n", diskno, maxdisk );
     l = 0L;
     for( i=0; i<glob.items; i++ )
     {
         if( specs[i]->dest==diskno )
         {
             printf( " %s\n", specs[i]->name );
             l += specs[i]->size;
         }
     }
     t = glob.total - l;
     printf( "## Total size: %6ld (%ld %s target)\n", l, labs( t ), t<0?"over":"from" );
}
/* sort files sequentially on the disks */
int sort_seq( void )
{
     int lp, disk;
     long size;
     struct filespec *node;
     for( lp=0; lp<glob.items; lp++ )
         specs[lp]->dest = 0;
     size = 0L;
     disk = 1;
     for( lp=0; lp<glob.items; lp++ )
     {
         if( size+specs[lp]->size > glob.total )
         {
             disk++;
             size = 0L;
         }
         size += specs[lp]->size;
         specs[lp]->dest = disk;
     }
     return( disk );
}
/* try "fitting" the files on the disks */
int sort_big( void )
{
     int lp, disk, best, left;
     long bytesfree;
     struct filespec *node;
     signal( SIGINT, flag_int );     /* so we can abort the fitting
gracefully */
     for( lp=0; lp<glob.items; lp++ )
     {
         specs[lp]->dest = 0;
         fits[lp] = -1;
     }
     bytesfree = glob.total;
     disk = 1;
     left = glob.items;
     if( glob.options & OPT_VERBOSE )
         fprintf( stderr, "Bin %2d: %7ld from target", 1, bytesfree );
     do
     {
         fits[0] = -1;
         glob.recleft = bytesfree;
         glob.disk = disk;
         glob.maxlevel = left;
         best = recur( bytesfree, specs[0], 0 );
         bytesfree = glob.recleft;
         if( bytesfree<0 )           /* aborted */
         {
             lp = 0;
             while( fits[lp]>=0 && lp<glob.items )
                 specs[fits[lp++]]->dest = disk;
             left -= lp;
             best = 0;
             bytesfree = -bytesfree;
         }
         if( best==0 )               /* disk full */
         {
             disk++;
             bytesfree = glob.total;
             if( glob.options&OPT_VERBOSE )
                 fprintf( stderr, "\nBin %2d: %7ld from target",disk,bytesfree );
         }
         else
         {
             for( lp=0; lp<best; lp++ )
                 specs[fits[lp]]->dest = disk;
             left -= best;
         }
     } while( left>0 );              /* more to go.. (same disk) */
     if( glob.options&OPT_VERBOSE )
         fputs( "\n", stderr );
     signal( SIGINT, SIG_DFL );      /* back to normal signal handler */
     return( disk );
}
/* recursively combine files to obtain maximum fill */
int recur( long space, struct filespec *this, int level )
{
     unsigned i, t, p;
     char done;
     long absleft;
     done = 0;
     if( glob.kbhit )
     {
         fprintf( stderr, " [Halted!]  C)ontinue, A)ccept fit or Q)uit? " );
         do
             i = fgetc( stderr );
         while( strchr( "cCaAqQ", i )==NULL );
         glob.kbhit = 0;
         if( glob.options&OPT_VERBOSE )
             fprintf( stderr, " \rBin %2d: %7ld from target",glob.disk,glob.recleft);
         if( i=='Q' || i=='q' )
         {
             freeall();
             fputs( "\nProgram quitted!\n", stderr );
             exit( 2 );
         }
         if( i=='A' || i=='a' )
         {
             glob.recleft = -glob.recleft;
             return( 0 );
         }
     }
     while( !done && this!=NULL )
     {
         if( this->dest==0 )
         {
             if( glob.options&OPT_OVERFLOW )
                 absleft = labs( space - this->size );
             else
                 absleft = space - this->size;
             if( absleft>=0L && space>0L )
             {
                 if( absleft < glob.recleft )
                 {
                     glob.recleft = absleft;
                     if( glob.options&OPT_VERBOSE )
                         fprintf( stderr, "\rBin %2d: %7ld from target ",
                                 glob.disk, glob.recleft );
                     this->dest = glob.disk+1;
                     done = -1;
                     for( t = p = 0; t<glob.items; t++ )
                     {
                         if( specs[t]->dest==glob.disk+1 )
                             fits[p++] = t;
                         if( specs[t]->dest==0 )
                             done = 0;
                     }
                     while( p<=glob.items )
                         fits[p++] = -1;
                     this->dest = 0;
                     if( glob.recleft <= glob.threshold )
                         done = 1;
                 }
                 if( !done && level+1 < glob.maxlevel )
                 {
                     this->dest = glob.disk+1;
                     done = recur( space - this->size, this->next, level+1 );
                     this->dest = 0;
                 }
             }
         }
         this = this->next;
     }
     if( level!=0 )
     {
         return( done );
     }
     else
     {
         t = 0;
         while( fits[t]!=-1 )
             t++;
         return( t );
     }
}
void freeall( void )
{
     int i;
     for( i=0; i<glob.items; i++ )
     {
         if( specs[i]->name!=NULL )
             free( specs[i]->name );
         free( specs[i] );
     }
     free( specs );
     free( fits );
}
void flag_int( int i)
{
     glob.kbhit = 1;
}
/*
  *  Split the string into arguments.
  *  The function returns the number of arguments found.
  *  The passed string will be destroyed.
  */
int split_line( char *s, char *args[] )
{
     int i, j;
     char *p, *h;
     char *stops[64];
     i = 0;
     args[0] = NULL;
     p = s;
     do
     {
         while( *p!=0 && strchr( " ,;\r\n\t", *p )!=NULL )   /* gobble spaces
*/
             p++;
         h = p;                          /* beginning of the argument */
         if( *h=='\'' || *h=='\"' )
         {
             p = strchr( h+1, *h );
             if( p!=NULL )
             {
                 args[i] = h+1;
                 stops[i] = p;
                 i++;
                 p++;
             }
         }
         else
         {
             p = strpbrk( h, " ,;\t\r\n\'\"" );
             args[i] = h;
             if( p!=NULL )
                 stops[i] = p;
             else
                 stops[i] = s+strlen( s );
             if( args[i]!=stops[i] )
                 i++;
         }
     }while( p!=NULL && *p!=0 );
     for( j=0; j<i; j++ )
         *stops[j] = 0;
     args[i] = NULL;
     return( i );
}

             \"         -q val   set quantizing to val, default is 1\\n\"
Certo che se impiegavi 2 minuti a fare 2 prove invece di inondare i gruppi 
e rompere i cogliomberi a tutti con questo programma, forse facevi prima.... 
non era necessario leggere il codice.
La cosa sta alla portata di un bambino, anche uno che abbia  subito
la RifMor.
I dati di input li puoi mettere in un file, riga per riga,
dove in ogni riga c\'e\' il \"nome\" dell\'oggetto e la dimensione,
tipo:
praseodimio 120
deontologia 20
spaghetti 84
caprimulgo 232
NanoBagonghi 431
pituitaria 312
coprolalia 12
etc.etc.
Poi targetsize = la dimensione degli zaini in cui cerca di mettere
gli oggetti.
Le opzioni: 
-n non fa ottimizzazioni, mette gli oggetti negli zaini nello stesso ordine in
cui sono nel file di input.
-o ammette anche di sforare in eccesso (possibilmente di poco) la dimensione
degli zaini
-v verbose parla un poco di piu...
-p val le righe di input contengono il valore non nella posizione 2 (dopo il nome dell\'item ma in posizione val-esima). Quindi se il tuo file ha la struttura:
.......
sono pisquano 662
sedna acne    121
ebdo madario  232
allora metti -p 3
-t val uno zaino viene considerato sufficientemente pieno quando
differisce dal threshold per un valore val. Il default = 0 e quindi
di default cerca di ottimizzare al massimo.
-q val quantizza (arrotonda) le dimensioni a multipli di val.
addio 
Servizio FlashMail - From 
http://www.makmo.com
SPOT : 
E-Mail address changed?Do you wan't change your address? Try ours service!
Click:
http://www.makmo.com/makcod/en/