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-9.00 / parte2.c < prev    next >
C/C++ Source or Header  |  2001-01-31  |  1KB  |  98 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 30
  5.  
  6. /* TIPI */
  7.  
  8. typedef struct nodo *tree;
  9.  
  10. struct nodo
  11. {
  12.     tree dx;
  13.     tree sx;
  14.     int info;
  15. };
  16.  
  17.  
  18. /* PROTOTIPI */
  19.  
  20. tree empty(void);
  21. void insert( int, tree *);
  22. void print(tree);
  23. void decr(tree);
  24.  
  25. /* FUNZIONI */
  26.  
  27. tree empty()
  28. {
  29.     return NULL;
  30. }
  31.  
  32. void insert(int i,tree *t)
  33. {
  34.     if(i>=0)
  35.     {   
  36.     if((*t)==empty())
  37.     {
  38.         (*t)=(tree)malloc(sizeof(struct nodo));
  39.         (*t)->info=i;
  40.         (*t)->sx=empty();
  41.         (*t)->dx=empty();
  42.     }
  43.     else 
  44.      if(i<(*t)->info)
  45.         insert(i,&((*t)->sx));
  46.      else if(i>(*t)->info)
  47.         insert(i,&((*t)->dx));
  48. }
  49. }
  50.  
  51. void decr(tree t)
  52. {
  53.     if(t!=empty())
  54.     {
  55.         if(t->info!=0)
  56.             t->info=t->info-1;
  57.         decr(t->sx);
  58.         decr(t->dx);                   
  59.     }
  60. }
  61.  
  62. void print(tree t)
  63. {
  64.     if(t!=empty())
  65.     {
  66.         print(t->sx);
  67.         printf("%d  ",t->info);
  68.         print(t->dx);
  69.  
  70.     }
  71. }
  72.  
  73. /* MAIN */
  74.  
  75. void main()
  76. {
  77.     char nome[MAX];
  78.     tree t;
  79.     int i;
  80.     FILE *fd=NULL;
  81.     t=empty();
  82.  
  83.     printf("Dammi il nome del file:\n");
  84.     scanf("%s",nome);
  85.  
  86.     if(fd=fopen(nome,"r"))
  87.     {
  88.         while(fscanf(fd,"%d",&i) && i!=-1)
  89.             insert(i,&t);
  90.         decr(t);
  91.         print(t);
  92.  
  93.         fclose(fd);
  94.     }
  95.     else
  96.         printf("Errore di apertura del file");
  97. }
  98.