elelomb

16_nov_17

May 19th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3.  
  4. #define SL           256
  5. #define FILE_INPUT   "dati_in.bin"
  6. #define FILE_OUT     "dati_out.bin"
  7.  
  8. typedef FILE*        pfile;
  9. typedef float*       pfloat;
  10.  
  11. int main(void)
  12.   {
  13.     int     n_d,j;
  14.     float   somma,media;
  15.     pfile   p_in,p_out;
  16.     pfloat  p_d,p_1,p_2;
  17.    
  18.     if((p_in=fopen(FILE_INPUT, "rb"))==NULL)                goto fopen_err;
  19.     if(fread (&n_d,sizeof(int),1,p_in)!=1)                  goto fread_err;
  20.     if ((p_d=(pfloat)malloc(sizeof(float)*(n_d+4)))==NULL)  goto mem_err;
  21.    
  22.     *(p_d)=0.0; *(p_d+1)=0.0; *(p_d+n_d-1)=0.0; *(p_d+n_d-2)=0.0;
  23.     if(fread(p_d+=2,sizeof(float),n_d,p_in)!=n_d)           goto fread_err;
  24.     if((p_out=fopen(FILE_OUT,"wb"))==NULL)                  goto fopen_err;
  25.     fwrite(&n_d,sizeof(int),1,p_out); /*sto scrivendo anche nel file out il numero di dati che ho*/
  26.    
  27.     for(p_1=p_d,p_2=p_d+3;p_1<=p_d+n_d-5,p_2<=p_d+n_d-2;p_1++,p_2++)
  28.       {
  29.         for(j=0;j<=1;j++)
  30.           {
  31.             somma+=p_1[j]+p_2[j];
  32.           }
  33.         media=somma/4;
  34.         if(media<(*(p_2-1)/2))
  35.           printf("Il valore %.2f è stato sostituito da --> %.2f\n",*(p_2-1),media);
  36.           *(p_2-1)=media;
  37.           fwrite(p_d,sizeof(float),1,p_out);
  38.       }  
  39.        
  40.     fclose(p_in);
  41.     fclose(p_out);
  42.    
  43.     return EXIT_SUCCESS;
  44.     fopen_err: printf("C'è un errore nell'apertura del file");                return EXIT_FAILURE;
  45.     mem_err:   printf("Non c'e'abbastanza spazio in memoria");                return EXIT_FAILURE;
  46.     fread_err: printf("Il numero acquisito è diverso dal numero inserito\n"); return EXIT_FAILURE;
  47.   }
Advertisement
Add Comment
Please, Sign In to add comment