#include #include #include #define MAXPAROLE 30 struct _pole{ char cognome[MAXPAROLE]; char nome[MAXPAROLE]; char marca[MAXPAROLE]; int tempo[10]; struct _pole *next; }; typedef struct _pole pole; pole *head=NULL; FILE *fp; void acquisisci(void); void assoluto(void); void best_driver(void); int main(void) { int scelta; char temp[3]; acquisisci(); do{ printf("[1]Ricerca miglior tempo in assoluto\n[2]Ricerca miglior tempo pilota\n[3]Esci\n-->"); fflush(stdout); gets(temp); scelta=atoi(temp); switch(scelta){ case 1: assoluto(); break; case 2: best_driver(); break; case 3: return 1; default: printf("Opzione errata\n"); } }while(scelta!=3); return 0; } void assoluto(void) { pole *p1; char n[MAXPAROLE],c[MAXPAROLE],a[MAXPAROLE]; int i; int migliore=9999; for(p1=head;p1!=NULL;p1=p1->next) for(i=0;i<10;i++) if(p1->tempo[i]tempo[i]; strcpy(c,p1->cognome); strcpy(n,p1->nome); strcpy(a,p1->marca); } printf("La pole position e' di %s %s su %s con il tempo di %d secondi\n",c,n,a,migliore); } void acquisisci(void) { pole *p1; char temp[80]; int i,numero; if((fp=fopen("tempi.txt","r"))==NULL) { printf("Impossibile aprire il file\n"); exit(1); } while(fgets(temp,81,fp)!=NULL) { if(!head) { head=(pole*)malloc(sizeof(pole)); sscanf(temp,"%s %s %s",head->cognome,head->nome,head->marca); for(i=0;i<10;i++) { fgets(temp,81,fp); if(temp[MAXPAROLE]=='\n') temp[MAXPAROLE]='\0'; numero=atoi(temp); head->tempo[i]=numero; } head->next=NULL; } else { p1=head; head=(pole*)malloc(sizeof(pole)); sscanf(temp,"%s %s %s",head->cognome,head->nome,head->marca); for(i=0;i<10;i++) { fgets(temp,81,fp); head->tempo[i]=atoi(temp); } head->next=p1; } } fclose(fp); } void best_driver(void) { pole *p1; int i,migliore=9999; char surname[MAXPAROLE]; printf("Inserisci il cognome del pilota: "); fflush(stdout); gets(surname); for(p1=head;p1!=NULL;p1=p1->next) if(!strcmpi(p1->cognome,surname)) for(i=0;i<9;i++) if(p1->tempo[i]tempo[i]; printf("Il miglior tempo e' di %d secondi\n",migliore); }