codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
typedef struct accompagnatori {
char nomeACC[N];
char cognACC[N];
struct accompagnatori *next;
}acc;
typedef struct impiegato {
char nome[N];
char cognome[N];
char stato[N];
char albergo[N];
struct accompagatori *acc_top;
struct impiegato *next;
}imp;
imp* newR ( );
acc* newA ( );
imp* gestioneR ( imp *top );
imp* gestioneA ( imp *top );
acc* inserimento ( acc*, char*, char* );
int main ()
{
imp *head;
imp *pNew;
imp *p;
int flag;
FILE *aptr;
char nome[N], cogn[N], scelta[N], classe[N], tmp2[N];
head = NULL;
aptr = fopen("testo.txt","r");
while ( fscanf(aptr,"%s%s%s%s", nome, cogn, scelta, classe) != EOF ) {
flag=0;
if ( strcmp(scelta,"registrazione")==0 ) {
p = head;
while ( p != NULL ) {
if ( strcmp(p->nome,nome)==0 && strcmp(p->cognome,cogn)==0 ) {
strcpy (p->stato, classe);
flag = 1;
}
p=p->next;
}
if ( flag == 0 ) {
pNew = newR ( );
strcpy (pNew->nome, nome);
strcpy (pNew->cognome, cogn);
strcpy (pNew->stato, classe);
pNew->acc_top = NULL;
if ( head == NULL ) {
pNew->next=NULL;
head = pNew;
}
else {
pNew->next=head;
head = pNew;
}
}
}
else if ( strcmp(scelta,"albergo")==0 ) {
p = head;
while ( p != NULL ) {
if ( strcmp(p->nome,nome)==0 && strcmp(p->cognome,cogn)==0 ) {
strcpy (p->albergo, classe);
flag = 1;
}
p=p->next;
}
if ( flag == 0 ) {
pNew = newR ( );
strcpy (pNew->nome, nome);
strcpy (pNew->cognome, cogn);
strcpy (pNew->stato, classe);
pNew->acc_top = NULL;
if ( head == NULL ) {
pNew->next=NULL;
head = pNew;
}
else {
pNew->next=head;
head = pNew;
}
}
}
else if ( strcmp(scelta,"accompagnatore")== 0 ) {
fscanf (aptr,"%s", tmp2);
p = head;
while ( p != NULL ) {
if ( strcmp(p->nome,nome)==0 && strcmp(p->cognome,cogn)==0 ) {
/*questa funzione sotto mi da l'errore di assegnazione non compatibile*/
p->acc_top = inserimento ( p->acc_top, classe, tmp2 );
flag = 1;
}
p=p->next;
}
if ( flag == 0 ) {
pNew = newR ( );
strcpy (pNew->nome, nome);
strcpy (pNew->cognome, cogn);
/*questa funzione sotto mi da l'errore di assegnazione non compatibile*/
pNew->acc_top = inserimento ( pNew->acc_top, classe, tmp2 );
if ( head == NULL ) {
p->next=NULL;
head = p;
}
else {
pNew->next=head;
head = pNew;
}
}
}
}
/*problema con la stampa e la gestione di eventuali stringhe vuote*/
p = head;
while ( p != NULL ) {
printf ("Cognome: %s\n", p->cognome);
printf ("Nome: %s\n", p->nome);
printf ("\tStatus: %s\n", p->stato);
printf ("\tAlbergo %s", p->albergo);
p=p->next;
}
printf ("\n");
return(1);
}
imp* newR ( )
{
imp* p;
p=(imp*)malloc(sizeof(imp));
if ( p == NULL ) {
printf ("Errore allocazione!\n");
exit(1);
}
return (p);
}
acc* newA ( )
{
acc *pA;
pA = (acc*)malloc(sizeof(acc));
if ( pA == NULL ) {
printf ("Errore allocazione!\n");
exit(1);
}
return (pA);
}
acc* inserimento ( acc* top, char *nome, char *cogn )
{
acc* pNew;
pNew = newA ( );
strcpy (pNew->nomeACC,nome);
strcpy (pNew->cognACC,cogn);
if ( top == NULL ) {
pNew->next = NULL;
top = pNew;
}
else {
pNew->next = top;
top = pNew;
}
return(top);
}