elelomb

compito_1_feb

May 16th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<math.h>
  4.  
  5. #define SL           256
  6. #define FILE_INPUT   "dati.bin"
  7. #define FILE_IN      "clean.txt"
  8. #define FILE_OUT     "noise.txt"
  9.  
  10. typedef FILE*        pfile;
  11. typedef float*       pfloat;
  12. typedef double*      pdouble;
  13.  
  14. int main(void)
  15.   {
  16.     int     numero_coppie,i,j;
  17.     pfile   p_dati,p_noise,p_clean;
  18.     pfloat  p_x;
  19.     pdouble p_y;
  20.     double  somma_x=0,somma_y=0,somma_xquadra=0,somma_yquadra=0,media_x,media_y,var_x,var_y,tmp_1,tmp_2;
  21.  
  22.     if((p_dati=fopen  (FILE_INPUT, "rb"))==NULL)        goto fopen_err;
  23.     if(fread (&numero_coppie, sizeof(int),1,p_dati)!=1) goto fread_err;
  24.  
  25.     if ((p_x=(pfloat) malloc(sizeof(float) *numero_coppie))==NULL)      goto mem_err;
  26.     if ((p_y=(pdouble)malloc(sizeof(double)*numero_coppie))==NULL)      goto mem_err;
  27.     if(fread (p_x, sizeof(float), numero_coppie,p_dati)!=numero_coppie) goto fread_err;
  28.     if(fread (p_y, sizeof(double),numero_coppie,p_dati)!=numero_coppie) goto fread_err;
  29.  
  30.     fclose(p_dati);  
  31.  
  32.     for(i=0;i<numero_coppie;i++)
  33.       {
  34.         somma_x+=p_x[i];
  35.         somma_y+=p_y[i];
  36.         somma_xquadra+=p_x[i]*p_x[i];
  37.         somma_yquadra+=p_y[i]*p_y[i];
  38.       }
  39.    
  40.     media_x=somma_x/numero_coppie;
  41.     media_y=somma_y/numero_coppie;
  42.     var_x=(((somma_xquadra)/numero_coppie)-((media_x)*(media_x))); /*non facciamo la radice quadrata perchè per l'equazione dell'ellisse non ci serve  */
  43.     var_y=(((somma_yquadra)/numero_coppie)-((media_y)*(media_y)));
  44.        
  45.     if((p_clean=fopen  (FILE_IN,  "w"))==NULL) goto fopen_err;
  46.     if((p_noise=fopen  (FILE_OUT, "w"))==NULL) goto fopen_err;
  47.    
  48.     for(i=0,j=(numero_coppie-1);j>=0; i++,j--)
  49.       {
  50.         tmp_1=p_x[i]-media_x;
  51.         tmp_2=p_y[j]-media_y;
  52.         p_dati=((((tmp_1*tmp_1)/var_x)+((tmp_2)*(tmp_2)/var_y))<1)?p_clean:p_noise;
  53.         fprintf(p_dati, "%.2f %.2f\n", p_x[i],p_y[j]);
  54.       }
  55.  
  56.     fclose(p_noise);
  57.     fclose(p_clean);
  58.     free(p_x);
  59.     free(p_y);
  60.      
  61.     return EXIT_SUCCESS;
  62.     fopen_err: printf("C'è un errore nell'apertura del file");                return EXIT_FAILURE;
  63.     mem_err:   printf("Non c'e'abbastanza spazio in memoria");                return EXIT_FAILURE;
  64.     fread_err: printf("Il numero acquisito è diverso dal numero inserito\n"); return EXIT_FAILURE;
  65.   }
Advertisement
Add Comment
Please, Sign In to add comment