#include #include #include #define MAX_LEN 25 #define MAX_GENITORI 2 #define MAX_FIGLI 5 //---------------------------------------------------------------------------- // Definizione del tipo di dato per la memorizzazione della lista di nomi // typedef struct TAG_LIST { char nome[MAX_LEN]; struct TAG_LIST *next; struct TAG_LIST *genitori[MAX_GENITORI]; int n_genitori; struct TAG_LIST *figli[MAX_FIGLI]; int n_figli; } LIST; LIST *testa; //---------------------------------------------------------------------------- // Prototipi delle funzioni // LIST* list_add( char *n ); LIST* list_lookup( char *n ); void list_print_all( void ); void read_file( char *s ); void stampa_relazione( char *p ); void stampa_albero( char *p ); //---------------------------------------------------------------------------- // Implementazione delle funzioni // LIST *list_add( char *n ) { LIST *new; LIST *ptr; LIST *pptr; int i; // Prepara il nuovo nodo // new = (LIST *)malloc( sizeof( LIST ) ); if( new == NULL ) { printf( "Impossibile allocare la memoria\n" ); exit( 1 ); } strcpy( new->nome, n ); new->next = NULL; for( i = 0; i < MAX_GENITORI; i++ ) new->genitori[i] = NULL; new->n_genitori = 0; for( i = 0; i < MAX_FIGLI; i++ ) new->figli[i] = NULL; new->n_figli = 0; if( testa == NULL ) { // Inserisci in testa // testa = new; } else { ptr = testa; pptr = NULL; // Cerca la posizione in cui inserire // while( ptr->next != NULL && strcmp( n, ptr->nome ) > 0 ) { pptr = ptr; ptr = ptr->next; } if( ptr->next == NULL || ptr == testa ) { if( strcmp( n, ptr->nome ) > 0 ) { // Aggiungo in coda // ptr->next = new; } else { // Aggiungo in testa // new->next = ptr; testa = new; } } else { // Aggiungo tra due nodi // new->next = ptr; pptr->next = new; } } return( new ); } LIST *list_lookup( char *n ) { LIST *ptr; ptr = testa; while( ptr != NULL && strcmp( n, ptr->nome ) != 0 ) ptr = ptr->next; return( ptr ); } voidlist_print_all( void ) { LIST *ptr; ptr = testa; while( ptr != NULL ) { printf( "%s\n", ptr->nome ); ptr = ptr->next; } } voidread_file( char *s ) { FILE*fp; char tmp[80]; char n1[80]; char n2[80]; int i, j; char flag; LIST *padre; LIST *figlio; fp = fopen( s, "r" ); if( fp == NULL ) { printf( "Impossibile aprire %s\n", s ); exit( 1 ); } while( fgets( tmp, 80, fp ) != NULL ) { // Riconosce le due stringhe // flag = 0; j = 0; i = 0; while( tmp[i] != '\n' && tmp[i] != '\0' ) { switch( flag ) { case 0: if( tmp[i] != '#' ) n1[j++] = tmp[i++]; else { n1[j-1] = '\0'; flag = 1; j = 0; i += 2; } break; case 1: n2[j++] = tmp[i++]; if( tmp[i] == '\n' ) n2[j] = '\0'; break; } } // Aggiorna l'elenco delle persone e le relazioni // padre->figlio // padre = list_lookup( n1 ); if( padre == NULL ) { // Aggiungo il genitore all'elenco // padre = list_add( n1 ); } figlio = list_lookup( n2 ); if( figlio == NULL ) { // Aggiungo il figlio all'elenco // figlio = list_add( n2 ); } // Aggiorno l'associazione padre->figlio // if( padre->n_figli >= MAX_FIGLI ) { printf( "%s ha piu' di %d figli\n", n1, MAX_FIGLI ); return; } else { padre->figli[padre->n_figli] = figlio; padre->n_figli++; } // Aggiorno l'associazione figlio->padre // if( figlio->n_genitori >= MAX_GENITORI ) { printf( "%s ha piu' di %d genitori\n", n2, MAX_GENITORI ); return; } else { figlio->genitori[figlio->n_genitori] = padre; figlio->n_genitori++; } } fclose( fp ); } voidstampa_relazione( char *p ) { LIST*ptr; inti; ptr = list_lookup( p ); if( ptr == NULL ) printf( "%s non trovato nell'elenco\n" ); else { printf( "NOME: %s\n", p ); printf( "FIGLI: %d\n", ptr->n_figli ); for( i = 0; i < ptr->n_figli; i++ ) printf( "\t%s\n", (ptr->figli[i])->nome ); printf( "GENITORI: %d\n", ptr->n_genitori ); for( i = 0; i < ptr->n_genitori; i++ ) printf( "\t%s\n", (ptr->genitori[i])->nome ); } } voidattraversa( int g, LIST *ptr ) { // Visita in ampiezza } voidstampa_albero( char *n ) { LIST*ptr; ptr = list_lookup( n ); if( ptr == NULL ) printf( "%s non trovato nell'elenco\n" ); else { attraversa( 0, ptr ); } } intmain( void ) { chartmp[80]; charcmd[80]; char*param; printf( "===> " ); gets( tmp ); while( strstr( tmp, "fine" ) == NULL ) { sscanf( tmp, "%s", cmd ); param = strstr( tmp, " " )+1; if( strcmp( cmd, "lettura" ) == 0 ) { read_file( param ); printf( "Lettura completata\n" ); } if( strcmp( cmd, "elenco" ) == 0 ) { list_print_all(); } if( strcmp( cmd, "relazione" ) == 0 ) { stampa_relazione( param ); } if( strcmp( cmd, "albero" ) == 0 ) { stampa_albero( param ); } printf( "===> " ); gets( tmp ); } return( 0 ); }