Guest User

Compito testo7

a guest
Jul 19th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define FLUSH while(getchar()!='\n')
  4. #define S 15
  5.  
  6. typedef short boolean;
  7.  
  8. typedef struct {
  9.         char NomeMateria[S], codice[S];
  10.         unsigned int crediti, giorno, mese, anno, voto;
  11. } tipobaseABR;
  12.  
  13. void LeggiStringa(char s[], unsigned int dim) {
  14.      unsigned int i;
  15.      for(i=0; i<dim-1; i++)
  16.          if((s[i]=getchar())=='\n')
  17.             break;
  18.      if(i==dim-1) while(getchar()!='\n');
  19.      s[i]='\0';
  20. }
  21.  
  22. void LeggiElementoABR(tipobaseABR *x) {
  23.      printf("\nInserisci il nome della materia:  ");
  24.      LeggiStringa(x->NomeMateria, S);
  25.      printf("\nInserisci il codice della materia:  ");
  26.      LeggiStringa(x->codice, S);
  27.      printf("\nInserisci il numero di crediti:  ");
  28.      scanf("%u", &(x)->crediti);
  29.      FLUSH;
  30.      printf("\nInserisci la data dell'esame:\n");
  31.      
  32.      do {
  33.      printf("\nInserisci il giorno:  ");
  34.      scanf("%u", &(x)->giorno);
  35.      FLUSH;
  36.      } while(x->giorno>31||x->giorno<1);
  37.      
  38.      do {
  39.      printf("\nInserisci il mese:  ");
  40.      scanf("%u", &(x)->mese);
  41.      FLUSH;
  42.      } while(x->mese>12||x->mese<1);
  43.      
  44.      do {
  45.      printf("\nInserisci l'anno:  ");
  46.      scanf("%u", &(x)->anno);
  47.      FLUSH;
  48.      } while(x->anno<1);
  49.      
  50.      do {
  51.      printf("\nInserisci il voto:  ");
  52.      scanf("%u", &(x)->voto);
  53.      FLUSH;
  54.      } while(x->voto<18 || x->voto>30);
  55. }
  56.  
  57. void VisualizzaElementoABR(tipobaseABR x) {
  58.      printf("\nNome Materia: %s\n", x.NomeMateria);
  59.      printf("\nCodice Materia: %s\n", x.codice);
  60.      printf("\nData:  %u / %u / %u\n", x.giorno, x.mese, x.anno);
  61.      printf("\nCrediti:  %u\n", x.crediti);
  62.      printf("\nVoto:  %u\n", x.voto);
  63. }
  64.  
  65. int ConfrontaABR(tipobaseABR x, tipobaseABR y) {
  66.     return(strcmp(x.NomeMateria, y.NomeMateria));
  67. }
  68.  
  69. unsigned int EstraiVoto(tipobaseABR x) {
  70.          return(x.voto);
  71. }
  72.  
  73.  
  74. /* Implementazione_ABR */
  75.  
  76. #define ALBEROVUOTO NULL
  77.  
  78. typedef struct nodoABR {
  79.         tipobaseABR info;
  80.         struct nodoABR * leftchild, * rightchild;
  81. } * abr;
  82.  
  83. void MakeNullABR(abr *a) {
  84.      *a=ALBEROVUOTO;
  85. }
  86.  
  87. boolean EmptyABR(abr a) {
  88.         return(a==ALBEROVUOTO);
  89. }
  90.  
  91. boolean FullABR(abr a) {
  92.          struct nodoABR * tmp;
  93.         boolean full=0;
  94.         if((tmp=(struct nodoABR *) malloc (sizeof(struct nodoABR)))==ALBEROVUOTO)
  95.            full=1;
  96.         else free(tmp);
  97.         return full;
  98. }
  99.  
  100. abr LeftChild(abr a) {
  101.     if(!EmptyABR(a))
  102.                     return a->leftchild;
  103. }
  104.  
  105. abr RightChild(abr a) {
  106.     if(!EmptyABR(a))
  107.                     return a->rightchild;
  108. }
  109.  
  110. tipobaseABR Label(abr a) {
  111.             if(!EmptyABR(a))
  112.                   return(a->info);
  113. }
  114.  
  115. boolean Member(abr a, tipobaseABR x) {
  116.         if(EmptyABR(a)) return 0;
  117.         else {
  118.               if(!ConfrontaABR(a->info, x))
  119.                         return 1;
  120.               else  if(ConfrontaABR(a->info, x)>0)
  121.                            return(Member(a->leftchild, x));
  122.                     else   return(Member(a->rightchild, x));
  123.                     }
  124. }
  125.  
  126. void InsertABR(abr *a, tipobaseABR x) {
  127.      if(!FullABR(*a)) {
  128.                        if(EmptyABR(*a)) {
  129.                                         *a=(struct nodoABR *) malloc (sizeof(struct nodoABR));
  130.                                         (*a)->info=x;
  131.                                         (*a)->rightchild=(*a)->leftchild=ALBEROVUOTO;
  132.                                         }
  133.                        else if(ConfrontaABR((*a)->info, x)>0)
  134.                                 InsertABR(&(*a)->leftchild, x);
  135.                             else
  136.                                 InsertABR(&(*a)->rightchild, x);
  137.      }
  138. }
  139.  
  140. /* tipobaseList.h */
  141. #define STR 20
  142. typedef struct {
  143.         char cognome[S], nome[S], CodiceFiscale[STR];
  144.         char residenza[S], matricola[S];
  145.         abr materie;
  146. } tipobaseList;
  147.  
  148. void LeggiElementoList(tipobaseList *x) {
  149.      printf("\nInserisci il cognome:  ");
  150.      LeggiStringa(x->cognome, S);
  151.      printf("\nInserisci il nome:  ");
  152.      LeggiStringa(x->nome, S);
  153.      printf("\nInserisci il codice fiscale:  ");
  154.      LeggiStringa(x->CodiceFiscale, STR);
  155.      printf("\nInserisci la residenza:  ");
  156.      LeggiStringa(x->residenza, S);
  157.      printf("\nInserisci il numero di matricola:  ");
  158.      LeggiStringa(x->matricola, S);
  159.      MakeNullABR(&(x)->materie);
  160. }
  161.  
  162. void CercaCognomeList(tipobaseList *x) {
  163.       printf("\nInserisci il cognome:  ");
  164.       LeggiStringa(x->cognome, S);
  165. }
  166.  
  167. void CercaElementoList(tipobaseList *x) {
  168.      printf("\nInserisci il cognome:  ");
  169.      LeggiStringa(x->cognome, S);
  170.      printf("\nInserisci il nome:  ");
  171.      LeggiStringa(x->nome, S);
  172.      printf("\nInserisci il codice fiscale:  ");
  173.      LeggiStringa(x->CodiceFiscale, STR);
  174. }
  175.  
  176. void VisualizzaElementoList(tipobaseList x) {
  177.      printf("\nCognome:  %s\n", x.cognome);
  178.      printf("Nome:  %s\n", x.nome);
  179.      printf("Codice Fiscale:  %s\n", x.CodiceFiscale);
  180.      printf("Residenza:  %s\n", x.residenza);
  181.      printf("Numero di matricola:  %s\n", x.matricola);
  182. }
  183.  
  184. int ConfrontaList(tipobaseList x, tipobaseList y) {
  185.     if(!strcmp(x.cognome, y.cognome))
  186.              if(!strcmp(x.nome, y.nome))
  187.                   return(strcmp(x.CodiceFiscale, y.CodiceFiscale));
  188.              else return(strcmp(x.nome, y.nome));
  189.     else return(strcmp(x.cognome, y.cognome));
  190. }
  191.  
  192. int ConfrontaCognome(tipobaseList x, tipobaseList y) {
  193.     return(strcmp(x.cognome, y.cognome));
  194. }
  195.  
  196. abr EstraiABR(tipobaseList x) {
  197.     return (x.materie);
  198. }
  199.  
  200. void AggiornaABR(tipobaseList *x, abr a) {
  201.      x->materie=a;
  202. }
  203.  
  204. unsigned int Indice(tipobaseList x) {
  205.          if(x.cognome[0]>='A'&&x.cognome[0]<='Z')
  206.               return(x.cognome[0]-'A');
  207.          if(x.cognome[0]>='a'&&x.cognome[0]<='z')
  208.               return(x.cognome[0]-'a');
  209.          return 0;
  210. }
  211.  
  212.  
  213. /* Implementazione_list.h */
  214.  
  215. #define LISTAVUOTA NULL
  216.  
  217. typedef struct nodoList {
  218.         tipobaseList info;
  219.         struct nodoList * next;
  220. } * list;
  221.  
  222. typedef list position;
  223.  
  224. void MakeNullList(list *l) {
  225.      *l=LISTAVUOTA;
  226. }
  227.  
  228. boolean EmptyList(list l) {
  229.         return(l==LISTAVUOTA);
  230. }
  231.  
  232. boolean FullList(list l) {
  233.         struct nodoList * tmp;
  234.         boolean full=0;
  235.         if((tmp=(struct nodoList *) malloc (sizeof(struct nodoList)))==LISTAVUOTA)
  236.            full=1;
  237.         else free(tmp);
  238.         return full;
  239. }
  240.  
  241. tipobaseList Retrieve(list l, position p) {
  242.              if(!EmptyList(l)) {
  243.                                if(p==LISTAVUOTA) return l->info;
  244.                                return (p->next->info);
  245.                                }
  246. }
  247.  
  248. position First(list l) {
  249.          return LISTAVUOTA;
  250. }
  251.  
  252. position End(list l) {
  253.          if(EmptyList(l)) return LISTAVUOTA;
  254.          while(l->next!=LISTAVUOTA)
  255.            l=l->next;
  256.          return l;
  257. }
  258.  
  259. position Next(list l, position p) {
  260.          if(!EmptyList(l)) {
  261.                            if(p==LISTAVUOTA) return l;
  262.                            return (p->next);
  263.                            }
  264. }
  265.  
  266. position Locate(list l, tipobaseList x) {
  267.          if(EmptyList(l)) return LISTAVUOTA;
  268.          if(!ConfrontaList(l->info, x)) return LISTAVUOTA;
  269.          while(l->next!=LISTAVUOTA)
  270.             if(!ConfrontaList(l->next->info, x))
  271.                break;
  272.             else l=l->next;
  273.          
  274.          return l;
  275. }
  276.  
  277. void InsertList(list *l, position p, tipobaseList x) {
  278.      struct nodoList * tmp;
  279.      if(!FullList(*l)) {
  280.                         tmp=(struct nodoList *) malloc (sizeof(struct nodoList));
  281.                         tmp->info=x;
  282.                         if(p==LISTAVUOTA) {
  283.                                            tmp->next=*l;
  284.                                            *l=tmp;
  285.                                            }
  286.                         else {
  287.                              tmp->next=p->next;
  288.                              p->next=tmp;
  289.                              }
  290.      }
  291. }
  292.  
  293. void DeleteList(list *l, position p) {
  294.      struct nodoList * tmp;
  295.      if(!EmptyList(*l)) {
  296.                          if(p==LISTAVUOTA) {
  297.                                             tmp=*l;
  298.                                             *l=tmp->next;
  299.                                             }
  300.                          else {
  301.                                tmp=p->next;
  302.                                p->next=tmp->next;
  303.                                }
  304.                          free(tmp);
  305.      }
  306. }
  307.  
  308. /* Svolgimento Compito */
  309. #define DIM 26
  310.  
  311. list archivio[DIM];
  312.  
  313. void Media(abr a, unsigned int *x, unsigned int *numero) {
  314.      
  315.      tipobaseABR y;
  316.      if(!EmptyABR(a)) {if(!EmptyABR(LeftChild(a)))
  317.                            Media(LeftChild(a), x, numero);
  318.                        y=Label(a);
  319.                        *x=*x+EstraiVoto(y);
  320.                        (*numero)=(*numero)+1;
  321.                        if(!EmptyABR(RightChild(a)))
  322.                             Media(RightChild(a), x, numero);
  323.                             }
  324. }
  325.      
  326. void Visitainord(abr a) {
  327.      if(!EmptyABR(a)) {
  328.                       if(!EmptyABR(LeftChild(a)))
  329.                            Visitainord(LeftChild(a));
  330.                       VisualizzaElementoABR(Label(a));
  331.                       if(!EmptyABR(RightChild(a)))
  332.                            Visitainord(RightChild(a));
  333.                       }
  334. }
  335.      
  336.      
  337. void Insord(list *l, tipobaseList x) {
  338.      tipobaseList y;
  339.      position p, u;
  340.      
  341.      if(!FullList(*l)) { p=First(*l);
  342.                         if(EmptyList(*l)) InsertList(l, p, x);
  343.                         else {
  344.                               u=End(*l);
  345.                               if(Locate(*l, x)!=u) printf("\nStudente gia' presente\n");
  346.                               else {
  347.                                     while(p!=u) {
  348.                                                 y=Retrieve(*l, p);
  349.                                                 if(ConfrontaList(y, x)>0)
  350.                                                    break;
  351.                                                 else p=Next(*l, p);
  352.                                                 }
  353.                                     InsertList(l, p, x);
  354.                                     }
  355.                         }
  356.      }
  357. }
  358.      
  359.      
  360.      
  361. void InsMateria(list *l, tipobaseList x, tipobaseABR y) {
  362.      abr a;
  363.      position p, u;
  364.      
  365.      if(EmptyList(*l)) { printf("\nLa lista e' vuota. Prima di inserire la materia "
  366.                               "sara' inserito lo studente\n");
  367.                         LeggiElementoList(&x);
  368.                         Insord(l, x);
  369.                         p=First(*l);
  370.                         }    
  371.      else { p=Locate(*l, x);
  372.            if(p==End(*l)) { printf("\nStudente non trovato. Prima di continuare con l'inserimento\n"
  373.                                   "della materia, verra' inserito lo studente in archivio\n");
  374.                            LeggiElementoList(&x);
  375.                            Insord(l, x);
  376.                            p=Locate(*l, x);
  377.                            }
  378.            else
  379.                  x=Retrieve(*l, p);
  380.                  }
  381.            a=EstraiABR(x);
  382.            if(!FullABR(a)) {
  383.                             InsertABR(&a, y);
  384.                             AggiornaABR(&x, a);
  385.                             DeleteList(l, p);
  386.                             InsertList(l, p, x);
  387.                             }
  388. }                    
  389.      
  390.      
  391.      
  392.      
  393. void VisualizzaMaterie(list l, tipobaseList x) {
  394.      position p, u;
  395.      tipobaseList y;
  396.      abr a;
  397.      unsigned int somma=0, materie=0;
  398.      unsigned int flag=0;
  399.      float media;
  400.      if(EmptyList(l)) printf("\nLista vuota\n");
  401.      else {
  402.            p=First(l);
  403.            u=End(l);
  404.            while(p!=u) {
  405.                        y=Retrieve(l, p);
  406.                        if(!ConfrontaCognome(y, x)) {
  407.                            VisualizzaElementoList(y);
  408.                            flag=1;
  409.                            }
  410.                        p=Next(l, p);
  411.                        }
  412.            if(!flag) printf("\nNon esiste alcuno studente con questo cognome in archivio\n");
  413.            else {
  414.                  printf("\nQual e' lo studente cercato?\n");
  415.                  CercaElementoList(&x);
  416.                  p=Locate(l, x);
  417.                  if(p==u) printf("\nStudente non presente\n");
  418.                  else {
  419.                       x=Retrieve(l, p);
  420.                       a=EstraiABR(x);
  421.                       if(EmptyABR(a)) printf("\nLo studente non ha dato nessuna materia\n");
  422.                       else{
  423.                           Media(a, &somma, &materie);
  424.                           Visitainord(a);
  425.                           media = (float) somma / (float ) materie;
  426.                           printf("\nMedia: %g\n", media);
  427.                           }
  428.                  }
  429.           }
  430.    }
  431. }
  432.  
  433. main() {
  434.         unsigned int scelta, i, indice, materie;
  435.         tipobaseList x;
  436.         tipobaseABR y;
  437.        
  438.         for(i=0; i<DIM; i++)
  439.             MakeNullList(archivio+i);
  440.        
  441.         do {
  442.             printf("\n\n\t\t\tMENU'\n");
  443.             printf("1) Inserisci un nuovo studente\n");
  444.             printf("2) Registra una nuova materia\n");
  445.             printf("3) Visualizza le materie date da uno studente\n");
  446.             printf("4) Esci\n");
  447.             scanf("%u", &scelta);
  448.             FLUSH;
  449.            
  450.             switch(scelta) {
  451.                            case 1:  LeggiElementoList(&x);
  452.                                     indice=Indice(x);
  453.                                     Insord(&(archivio[indice]), x);
  454.                                     printf("\nLo studente ha gia' dato delle materie?\n"
  455.                                           "Scrivere il numero di materie date:  ");
  456.                                     scanf("%u", &materie);
  457.                                     FLUSH;
  458.                                     if(materie>0)
  459.                                         for(i=0; i<materie; i++) {
  460.                                                  LeggiElementoABR(&y);
  461.                                                  InsMateria(&(archivio[indice]), x, y);
  462.                                                  }      
  463.                                     break;
  464.                            
  465.                            case 2:  CercaElementoList(&x);
  466.                                     indice=Indice(x);
  467.                                     LeggiElementoABR(&y);
  468.                                     InsMateria(&(archivio[indice]), x, y);
  469.                                     break;
  470.                            
  471.                            case 3:  CercaCognomeList(&x);
  472.                                     indice=Indice(x);
  473.                                     VisualizzaMaterie(archivio[indice], x);
  474.                                     break;
  475.                            }
  476.             } while(scelta!=4);
  477. }
Advertisement
Add Comment
Please, Sign In to add comment