Guest User

Untitled

a guest
Jun 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #define dim 30
  4.      
  5.     typedef struct{char nomeEsame[20];
  6.                    int votoEsame;
  7.                    } esame;
  8.                    
  9.     void scrivi(esame a[],int*m);
  10.     void visualizza(esame a[],int m);
  11.     void salvasufile(FILE*out,esame a[],int m);
  12.     void leggifile(FILE*out);
  13.     void ordina(esame a[],int m);
  14.     void stampavettore(esame a[],int m);
  15.      
  16.     int main()
  17.     {
  18.         esame esami[dim];
  19.         int n;
  20.         FILE*fp;
  21.        
  22.        
  23.        
  24.         scrivi(esami,&n);
  25.        
  26.         visualizza(esami,n);
  27.        
  28.         fp=fopen("verbaleanzi.txt","w");
  29.         salvasufile(fp,esami,n);
  30.         fclose(fp);
  31.        
  32.         fp=fopen("verbaleanzi.txt","r");
  33.         leggifile(fp);
  34.         fclose(fp);
  35.        
  36.         ordina(esami,n);
  37.        
  38.         stampavettore(esami,n);
  39.        
  40.      
  41.       system("PAUSE");    
  42.       return 0;
  43.     }
  44.      
  45.     void scrivi(esame a[],int*m)
  46.     {
  47.          int n;
  48.          printf("quanti esami vuoi inserire? ");
  49.          scanf("%d",&n);
  50.          getchar();
  51.          printf("\ninserisci esami\n\n");
  52.          int i;
  53.          for(i=0;i<n;i++)
  54.          {
  55.                          printf("insersci nome esame\n");
  56.                          gets(a[i].nomeEsame);
  57.                          printf("unserisci voto esame\n");
  58.                          scanf("%d",&a[i].votoEsame);
  59.                          printf("\n");
  60.                          getchar();
  61.                          }
  62.          *m=n;
  63.     }
  64.      
  65.      
  66.     void visualizza(esame a[],int m)
  67.     {
  68.          printf("gli esami inseriti sono\n\n");
  69.          int i;
  70.          for(i=0;i<m;i++)
  71.          {
  72.                          printf("nome esame: %s\n",a[i].nomeEsame);
  73.                          printf("voto esame: %d\n",a[i].votoEsame);
  74.                          printf("\n");
  75.                          }
  76.     }
  77.      
  78.      
  79.     void salvasufile(FILE*out,esame a[],int m)
  80.     {
  81.          if(out==NULL)
  82.          {
  83.                       printf("errore nell'apertura del file in lettura\n");
  84.                       }
  85.          else
  86.          {
  87.               printf("\nSALVATAGGIO SU FILE IN CORSO....\n\n");
  88.               int i;
  89.               for(i=0;i<m;i++)
  90.               {
  91.                          fprintf(out,"nome esame: %s\n",a[i].nomeEsame);
  92.                          fprintf(out,"voto esame: %d\n",a[i].votoEsame);
  93.                          fprintf(out,"\n");
  94.                          }
  95.                          }
  96.     }
  97.      
  98.      
  99.     void leggifile(FILE*out)
  100.     {
  101.          if(out==NULL)
  102.          {
  103.                       printf("errore nell'apertura del file\n");
  104.                       }
  105.          else
  106.          {
  107.              char c;
  108.              printf("******CONTENUTO DEL FILE******\n\n");
  109.              c=fgetc(out);
  110.              while(!feof(out))
  111.              {
  112.                               putchar(c);
  113.                               c=fgetc(out);
  114.                               }
  115.                               }
  116.     }
  117.      
  118.      
  119.     void ordina(esame a[],int m)
  120.     {
  121.          esame app;
  122.          int i,j;
  123.          for(i=1;i<m;i++)
  124.          {
  125.                          app=a[i];
  126.                          j=i-1;
  127.                          while(j>=0 && a[j].votoEsame>app.votoEsame)
  128.                          {
  129.                                     a[j+1]=a[j];
  130.                                     j--;
  131.                                     }
  132.                                     a[j+1]=app;
  133.                                     }
  134.     }
  135.      
  136.      
  137.     void stampavettore(esame a[],int m)
  138.     {
  139.          printf("\n******VETTORE ESAMI ORDINATO RISPETTO AL CAMPO VOTO******\n\n");
  140.          int i;
  141.          for(i=0;i<m;i++)
  142.          {
  143.                          printf("nome esame: %s\n",a[i].nomeEsame);
  144.                          printf("voto esame: %d\n",a[i].votoEsame);
  145.                          printf("\n");
  146.                          }
  147.     }
Add Comment
Please, Sign In to add comment