/* File: ese110304_2.c Specifica: Soluzione esercizio 2 Scrivere un programma che legge da tastiera una sequenza di studenti nel formato (nome, cognome, data di nascita, numero di matricola) gianni rossi 01 01 1980 199472 li memorizza in una lista di strutture opportunamente definita, li ordina per nome, matricola e data di nascita, ed infine li stampa nei tre ordini. */ #include #include /* atoi */ #include #define MAX_STRING 20 #define SM "%20s" typedef struct { char nome[MAX_STRING]; char cognome[MAX_STRING]; unsigned int giorno; unsigned int mese; unsigned int anno; unsigned int matricola; } studente; typedef struct elem { studente cont; struct elem * next; } elem; typedef elem * listastudenti; void leggi_stud(listastudenti * l); void stampa_stud(listastudenti l); void ordina_stud(listastudenti * l, int (* confr) (studente s1, studente s2)); int confr_nome(studente s1, studente s2); int confr_matricola(studente s1, studente s2); int confr_data(studente s1, studente s2); int main(void) { listastudenti stud=NULL; leggi_stud(&stud); printf("Studenti inseriti:\n"); stampa_stud(stud); printf("\n"); ordina_stud(&stud,&confr_nome); printf("Studenti ordinati per nome:\n"); stampa_stud(stud); printf("\n"); ordina_stud(&stud,&confr_matricola); printf("Studenti ordinati per matricola:\n"); stampa_stud(stud); printf("\n"); ordina_stud(&stud,&confr_data); printf("Studenti ordinati per data di nascita:\n"); stampa_stud(stud); printf("\n"); return 0; } void leggi_stud(listastudenti * l) { char s[MAX_STRING]; elem *tmp; printf("Inserisci degli studenti con formato:\n\t NOME COGNOME DATA_DI_NASCITA (gg mm aaaa) NUMERO_MATRICOLA\nterminati da CTRL-D (EOF, End-OF-File)\n"); while(scanf(SM,s)!=EOF) { tmp = malloc(sizeof(elem)); if (tmp != NULL) { strcpy(s,(*tmp).cont.nome); scanf(SM,(*tmp).cont.cognome); scanf("%2u",&(*tmp).cont.giorno); scanf("%2u",&(*tmp).cont.mese); scanf("%4u",&(*tmp).cont.anno); scanf("%6u",&(*tmp).cont.matricola); (*tmp).next=*l; *l=tmp; } else { printf("Errore di allocazione!"); exit(-1); } } } void stampa_stud(listastudenti l) { elem * tmp; tmp=l; while(tmp != NULL) { printf("%4d : %s %s, nato/a il %2d/%2d/%4d\n", (*tmp).cont.matricola, (*tmp).cont.nome, (*tmp).cont.cognome, (*tmp).cont.giorno, (*tmp).cont.mese, (*tmp).cont.anno); tmp = (*tmp).next; } } void ordina_stud(listastudenti * l, int (* confr) (studente s1, studente s2)) { elem *maxel, *premaxel, *tmpel, *pretmpel, *newl=NULL; tmpel=maxel=pretmpel=premaxel=*l; while (*l != NULL) { tmpel=maxel=pretmpel=premaxel=*l; while (tmpel != NULL) { if (confr((*maxel).cont,(*tmpel).cont)<0) { maxel=tmpel; premaxel=pretmpel; } pretmpel=tmpel; tmpel=(*tmpel).next; } (*premaxel).next=(*maxel).next; if (maxel==*l) *l=(*maxel).next; (*maxel).next=newl; newl=maxel; } *l=newl; } int confr_nome(studente s1, studente s2) { int tmpc; tmpc=strcmp(s1.cognome,s2.cognome); if (tmpc) return tmpc; else return strcmp(s1.nome,s2.nome); } int confr_matricola(studente s1, studente s2) { if (s1.matricola < s2.matricola) return -1; if (s1.matricola > s2.matricola) return 1; return 0; } int confr_data(studente s1, studente s2) { if (s1.anno < s2.anno) return -1; if (s1.anno > s2.anno) return 1; if (s1.mese < s2.mese) return -1; if (s1.mese > s2.mese) return 1; if (s1.giorno < s2.giorno) return -1; if (s1.giorno > s2.giorno) return 1; return 0; }