mittimus

Compito(lista-vettore-contatore)

Jul 17th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define FLUSH while(getchar()!='\n')
  5. #define LISTAVUOTA NULL
  6. #define N 15
  7.  
  8. typedef struct{
  9.         char codice[N],marca[N];
  10.         unsigned int prezzo,presenti;
  11.         }tipobaselist;
  12.        
  13. void leggistringa(char s[],short dim){
  14.      short i;
  15.      for(i=0;(s[i]=getchar())!='\n' && i<dim-1;i++);
  16.      s[i]='\0';
  17.      if(i==dim-1) FLUSH;}
  18.      
  19. void acquisisci(tipobaselist *x){
  20.      printf("\ninserire il codice: ");
  21.      leggistringa(x->codice,N);
  22.      printf("inserire la marca: ");
  23.      leggistringa(x->marca,N);
  24.      printf("prezzo: ");
  25.      scanf("%u", &x->prezzo);
  26.      printf("pezzi presenti in magazzino: ");
  27.      scanf("%u", &x->presenti);
  28.      FLUSH;}
  29.      
  30. void mostra(tipobaselist x){
  31.      printf("\nmarca: %s\n", x.marca);
  32.      printf("prezzo: %u\n", x.prezzo);
  33.      printf("pezzi presenti: %u\n", x.presenti);}
  34.      
  35. void cerca(tipobaselist *x){
  36.      printf("\ninserire il codice da ricercare: ");
  37.      leggistringa(x->codice,N);}
  38.      
  39. int confronta(tipobaselist a,tipobaselist b){
  40.     return(strcmp(a.codice,b.codice));}
  41.    
  42. typedef struct nodolist{
  43.         tipobaselist info;
  44.         struct nodolist *next;
  45.         }*list;
  46.        
  47. typedef list position;
  48.  
  49. typedef short boolean;
  50.  
  51. void makenulllist(list *l){
  52.      *l=LISTAVUOTA;}
  53.      
  54. boolean emptylist(list l){
  55.         return(l==LISTAVUOTA);}
  56.        
  57. boolean fulllist(list l){
  58.         struct nodolist *tmp;
  59.         tmp=(struct nodolist *)malloc(sizeof(struct nodolist));
  60.         if(tmp==LISTAVUOTA)
  61.         return 1;
  62.         free(tmp);
  63.         return 0;}
  64.        
  65. position first(list l){
  66.          return(LISTAVUOTA);}
  67.          
  68. position end(list l){
  69.          if(l==LISTAVUOTA) return(LISTAVUOTA);
  70.          while(l->next!=LISTAVUOTA)
  71.          l=l->next;
  72.          return(l);}
  73.          
  74. void insertlist(list *l,position p,tipobaselist x){
  75.      struct nodolist *tmp;
  76.      if(!fulllist(*l)){
  77.                        tmp=(struct nodolist *)malloc(sizeof(struct nodolist));
  78.                        tmp->info=x;
  79.                        if(p==LISTAVUOTA){
  80.                                          tmp->next=*l;
  81.                                          *l=tmp;}
  82.                        else{
  83.                             tmp->next=p->next;
  84.                             p->next=tmp;}}}
  85.                            
  86. void deletelist(list *l,position p){
  87.      struct nodolist *tmp;
  88.      if(!emptylist(*l))
  89.         if(p==LISTAVUOTA){
  90.                            tmp=(*l)->next;
  91.                            free(*l);
  92.                            *l=tmp;}
  93.         else{
  94.              tmp=p->next;
  95.              p->next=tmp->next;
  96.              free(tmp);}}
  97.              
  98. position locate(list l,tipobaselist x){
  99.          if(!emptylist(l)){
  100.                            if(!confronta(l->info,x)) return(LISTAVUOTA);
  101.                            while(l->next!=LISTAVUOTA){
  102.                                                       if(!confronta(l->next->info,x)) return l;
  103.                                                       l=l->next;}
  104.                            return l;}}
  105.                            
  106. tipobaselist retrieve(list l,position p){
  107.              if(!emptylist(l))
  108.                if(p==LISTAVUOTA) return(l->info);
  109.                else return(p->next->info);}
  110.                
  111. position next(list l,position p){
  112.          if(p==LISTAVUOTA) return(l);
  113.          else return(p->next);}
  114.          
  115.          
  116. struct nodovettore{
  117.        unsigned int contatore;
  118.        list lista;
  119.        };
  120.  
  121. void allocalist(list **);
  122. void allocavet(struct nodovettore **,short );
  123. void insord(list *,tipobaselist);
  124. void inizializza(struct nodovettore *,short);
  125. void inserisci_prodotto(struct nodovettore *,tipobaselist,unsigned int,unsigned int *);
  126. void mostra_prodotto(struct nodovettore *,tipobaselist, unsigned int);
  127.  
  128. main()
  129. {
  130.       unsigned int scelta,n;
  131.       struct nodovettore *archivio;
  132.       tipobaselist elem;
  133.       unsigned int index=-1;
  134.       printf("inserire la cardinalita' del vettore: ");
  135.       scanf("%u", &n);
  136.       allocavet(&archivio,n);
  137.       inizializza(archivio,n);
  138.       do{
  139.                               printf("1 inserire un nuovo prodotto\n");
  140.                               printf("2 mostra prodotto\n");
  141.                               printf("3 esci\n");
  142.                               printf("cosa si desidera fare: ");
  143.                               scanf("%u", &scelta);
  144.                               FLUSH;
  145.                               switch(scelta){
  146.                                              case 1:
  147.                                                   acquisisci(&elem);
  148.                                                   inserisci_prodotto(archivio,elem,n,&index);
  149.                                                   break;
  150.                                              case 2:
  151.                                                   cerca(&elem);
  152.                                                   mostra_prodotto(archivio,elem,n);
  153.                                                   break;
  154.                                              case 3:
  155.                                                   exit(1);
  156.                                              default:
  157.                                                      printf("valore non presente\n");
  158.                                              }
  159.       }while(scelta!=3);
  160. }
  161.                                              
  162.      
  163.      
  164. void allocalist(list **l){
  165.      *l=(list *)malloc(sizeof(list));}
  166.      
  167. void allocavet(struct nodovettore **v,short dim){
  168.      *v=(struct nodovettore *)malloc(sizeof(struct nodovettore));}
  169.      
  170. void inizializza(struct nodovettore *v,short dim){
  171.      short i;
  172.      for(i=0;i<dim;i++){
  173.                         v[i].contatore=0;
  174.                         allocalist(&(v[i].lista));
  175.                         makenulllist(&(v[i].lista));}}
  176.                        
  177. void insord(list *l,tipobaselist x){
  178.      position p,u;
  179.      tipobaselist tmp;
  180.      if(emptylist(*l)) insertlist(l,first(*l),x);
  181.      else{
  182.           p=first(*l);
  183.           u=end(*l);
  184.           while(p!=u){
  185.                       tmp=retrieve(*l,p);
  186.                       if(confronta(tmp,x)<0) p=next(*l,p);
  187.                       else break;}
  188.           insertlist(l,p,x);}}
  189.          
  190.  
  191. void inserisci_prodotto(struct nodovettore *v,tipobaselist x,unsigned int n,unsigned int *indice){  
  192.      position pos;
  193.      unsigned int i;
  194.      unsigned int min;
  195.      short xx=0;
  196.      if((*indice) ==-1){
  197.                   insord(&(v[0].lista),x);
  198.                   (v[0].contatore)++ ;
  199.                   (*indice)=0;
  200.                   printf("prodotto inserito\n");}
  201.      else{
  202.          min=(v[0].contatore);
  203.          
  204.        for(i=0;i<n;i++) {
  205.                         if(!emptylist(v[i].lista)) pos=locate((v[i].lista),x);
  206.                         if(emptylist(v[i].lista) || pos==end(v[i].lista));
  207.                         else{
  208.                              printf("prodotto gia' presente\n");
  209.                              xx++;
  210.                              break;}}
  211.        if(xx==0){
  212.        for(i=0;i<n;i++)
  213.        if((v[i].contatore)<min){
  214.                                 min=v[i].contatore;
  215.                                 (*indice)=i; }
  216.        
  217.        if(!fulllist(v[*indice].lista)){
  218.                                         insord(&(v[*indice].lista),x);
  219.                                         (v[*indice].contatore)++ ;
  220.                                         printf("prodotto inserito\n");}
  221.          
  222.           else printf("archivio pieno\n");}}}
  223.          
  224.          
  225. void mostra_prodotto(struct nodovettore *v,tipobaselist x, unsigned int n){
  226.      position pos;
  227.      tipobaselist tmp;
  228.      unsigned int i;
  229.      short counter=0;
  230.      for(i=0;i<n;i++){
  231.                       if(emptylist(v[i].lista));
  232.                       else{
  233.                       pos=locate(v[i].lista,x);
  234.                       if(pos!=end(v[i].lista)){
  235.                                                tmp=retrieve((v[i].lista),pos);
  236.                                                mostra(tmp);
  237.                                                counter++;
  238.                                                break;}}}
  239.    if(counter==0) printf("codice non presente\n");}
Advertisement
Add Comment
Please, Sign In to add comment