home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / testi-esami / labo-6.00 / turno3 / parte3.c < prev    next >
C/C++ Source or Header  |  2000-06-20  |  2KB  |  123 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct cell* set;
  5.  
  6. struct cell{
  7.     int info;
  8.     set next;
  9. };
  10.  
  11.  
  12. set empty(void);
  13. void insert(int,set *);
  14. void print(set);
  15. int member(int,set);
  16. int subset(set,set);
  17.  
  18. void main ()
  19. {
  20.     int c;
  21.     char fname[20];
  22.     set s1=empty(),s2=empty();
  23.     FILE *fp=NULL;
  24.     printf ("Inserisci il nome del file di input: ");
  25.     scanf ("%s",fname);
  26.     
  27.     if(fp =fopen(fname,"r"))
  28.     {
  29.         while(fscanf(fp,"%d",&c) && c!=-1)
  30.             insert(c,&s1);
  31.         while(fscanf(fp,"%d",&c) && c!=-1)
  32.             insert(c,&s2);
  33.                     
  34.     }
  35.     else
  36.     {
  37.         printf("\nErrore nell'apertura del file!\n");
  38.         exit(EXIT_FAILURE);
  39.     }
  40.  
  41.     printf("\nInsieme 1: ");
  42.     print(s1);
  43.     
  44.  
  45.     printf("\nInsieme 2: ");
  46.     print(s2);
  47.      
  48.     if (subset(s1,s2))
  49.         printf ("\nIl primo insieme e' contenuto/uguale al secondo\n"); 
  50.     else
  51.         printf ("\nIl primo insieme non e' contenuto/uguale al secondo\n");        
  52.     fclose (fp);    
  53. }
  54.         
  55.  
  56.  
  57. set empty ()
  58. {
  59.     return NULL;
  60. }
  61.  
  62. void insert (int x, set *s)
  63. {
  64.     set prec=NULL;
  65.     set corr=*s;
  66.     set aux;
  67.     aux=(set) malloc (sizeof(struct cell));
  68.     aux->info=x;
  69.     aux->next=NULL;
  70.     
  71.     while((corr!=NULL) && (x>corr->info))
  72.     {
  73.         prec=corr;
  74.         corr=corr->next;
  75.     }
  76.     
  77.     if ((prec==NULL)&&(corr==NULL || x!=corr->info)) /*inserimento  in testa */
  78.     {
  79.         aux->next=*s;
  80.         *s=aux;
  81.     }
  82.     else if (corr==NULL) /* inserimento in coda */
  83.     {
  84.         prec->next=aux;
  85.         return;
  86.     }
  87.     else if (x!=corr->info) /* inserimento in mezzo */
  88.     {
  89.         prec->next=aux;
  90.         aux->next=corr;
  91.     }
  92. }
  93.  
  94. void print (set s)
  95. {
  96.     for (; s!=NULL; s=s->next)
  97.         printf ("%d\t",s->info);
  98.     printf("\n\n");     
  99. }
  100.  
  101.  
  102. int member (int x, set s)  /*l'implementazione piu' semplice richiedeva l'uso di una funzione member */
  103. {
  104.     for (;s!=NULL;s=s->next)
  105.     {
  106.         if (x==s->info)
  107.             return 1;
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113. int subset (set s1, set s2)
  114. {
  115.     for (;s1!=NULL;s1=s1->next)
  116.     {
  117.         if (!member(s1->info,s2))
  118.             return 0;
  119.     }
  120.     
  121.     return 1;
  122. }
  123.