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.99 / turno1 / parte4.c < prev   
C/C++ Source or Header  |  1999-06-16  |  3KB  |  141 lines

  1. /* Una possibile soluzione Esame di Laboratorio di Algoritmi 15 giugno 1999
  2.  * Turno 1 */
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX 20
  9.  
  10. /* definizione dei tipi */
  11.  
  12. typedef  struct stackel * stack;    /* uno stack  e' un puntatore */
  13. typedef  struct stackel{ int el; stack next; } stck;
  14.  
  15. /* operazioni sullo stack */ 
  16.  
  17. stack   empty(void);       /* restituisce uno stack vuoto, cioe' restituisce NULL */
  18. stack push(int,stack);     /* aggiunge in testa        */
  19. void  print(stack);        /* stampa lo stack */
  20. stack makestackel(void);   /* alloca una nuova cella */
  21. void even_odd(stack s, stack * s1, stack * s2);
  22.  
  23. /* main */  
  24.  
  25. main()
  26. {
  27.   
  28.     FILE * fp;
  29.     char nome[MAX];
  30.     int n;
  31.     stack s,s1=empty(),s2=empty();
  32.  
  33.   
  34.     /* creo stack vuoto */
  35.  
  36.         s = empty();
  37.  
  38.     /* leggo il nome del file */
  39.  
  40.         printf("Fornisci il nome del file: ");
  41.     scanf("%s", nome);
  42.  
  43.     /* apro il file in lettura */
  44.  
  45.     fp = fopen(nome,"r");
  46.  
  47.  
  48.     /* carico lo stack con i dati letti da file 
  49.      * (assumendo il file del formato corretto */
  50.  
  51.     while (fscanf(fp,"%d",&n) && n != -1)
  52.         s = push(n,s);
  53.  
  54.     /* stampo lo stack */
  55.  
  56.     print(s);
  57.  
  58.  
  59.     /* creo i nuovi file */
  60.  
  61.     even_odd(s,&s1,&s2);
  62.  
  63.     /* stampo i nuovi file */
  64.  
  65.     print(s1);
  66.     print(s2);
  67. }
  68.  
  69.  
  70. /* funzioni */
  71.  
  72.  
  73. stack empty()
  74.  { 
  75.   return NULL;  
  76.  }
  77.  
  78.  
  79.  
  80. stack push(int el, stack s) 
  81.  { 
  82.    stack t;
  83.  
  84.    t=makestackel();      
  85.    t->el=el;
  86.    t->next=s;
  87.    return t; 
  88.  } 
  89.  
  90.  
  91.  
  92. stack makestackel(){ return (stack)malloc(sizeof(stck)); }
  93.  
  94.  
  95. void print(stack s) 
  96. {  
  97.     printf("\nLo stack ha il seguente contenuto: ");
  98.     for(; s !=NULL ; s = s -> next)
  99.         printf(" %d",s->el);
  100.     printf("\n\n");
  101.  } 
  102.  
  103.  
  104.  
  105. void even_odd(stack s, stack * s1, stack * s2)
  106. {
  107.   stack aux1=empty(),aux2=empty();
  108.  
  109.   for (; s != empty(); s = s-> next)
  110.     { 
  111.       if (s->el %2 == 0)          /* elemento pari */
  112.          if (aux1 == empty())       /*primo elemento */
  113.                 {
  114.                    *s1 = s;
  115.                    aux1 = s;
  116.                 }
  117.          else                    /* elementi successivi */
  118.                 {
  119.                     aux1 -> next = s;
  120.                     aux1 = s;
  121.                 }
  122.                 
  123.       else                          /* elemento dispari */
  124.          if (aux2 == empty())       /*primo elemento */
  125.                 {
  126.                    *s2 = s;
  127.                    aux2 = s;
  128.                 }
  129.          else                    /* elementi successivi */
  130.                 {
  131.                     aux2 -> next = s;
  132.                     aux2 = s;
  133.                 }    
  134.     }
  135.  
  136.   if (aux1 != empty())
  137.       aux1->next = empty();
  138.   if (aux2 != empty())
  139.       aux2 -> next = empty();
  140. }
  141.