Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<math.h>
- #define SL 256
- #define FILE_INPUT "dati.bin"
- #define FILE_IN "clean.txt"
- #define FILE_OUT "noise.txt"
- typedef FILE* pfile;
- typedef float* pfloat;
- typedef double* pdouble;
- int main(void)
- {
- int numero_coppie,i,j;
- pfile p_dati,p_noise,p_clean;
- pfloat p_x;
- pdouble p_y;
- 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;
- if((p_dati=fopen (FILE_INPUT, "rb"))==NULL) goto fopen_err;
- if(fread (&numero_coppie, sizeof(int),1,p_dati)!=1) goto fread_err;
- if ((p_x=(pfloat) malloc(sizeof(float) *numero_coppie))==NULL) goto mem_err;
- if ((p_y=(pdouble)malloc(sizeof(double)*numero_coppie))==NULL) goto mem_err;
- if(fread (p_x, sizeof(float), numero_coppie,p_dati)!=numero_coppie) goto fread_err;
- if(fread (p_y, sizeof(double),numero_coppie,p_dati)!=numero_coppie) goto fread_err;
- fclose(p_dati);
- for(i=0;i<numero_coppie;i++)
- {
- somma_x+=p_x[i];
- somma_y+=p_y[i];
- somma_xquadra+=p_x[i]*p_x[i];
- somma_yquadra+=p_y[i]*p_y[i];
- }
- media_x=somma_x/numero_coppie;
- media_y=somma_y/numero_coppie;
- var_x=(((somma_xquadra)/numero_coppie)-((media_x)*(media_x))); /*non facciamo la radice quadrata perchè per l'equazione dell'ellisse non ci serve */
- var_y=(((somma_yquadra)/numero_coppie)-((media_y)*(media_y)));
- if((p_clean=fopen (FILE_IN, "w"))==NULL) goto fopen_err;
- if((p_noise=fopen (FILE_OUT, "w"))==NULL) goto fopen_err;
- for(i=0,j=(numero_coppie-1);j>=0; i++,j--)
- {
- tmp_1=p_x[i]-media_x;
- tmp_2=p_y[j]-media_y;
- p_dati=((((tmp_1*tmp_1)/var_x)+((tmp_2)*(tmp_2)/var_y))<1)?p_clean:p_noise;
- fprintf(p_dati, "%.2f %.2f\n", p_x[i],p_y[j]);
- }
- fclose(p_noise);
- fclose(p_clean);
- free(p_x);
- free(p_y);
- return EXIT_SUCCESS;
- fopen_err: printf("C'è un errore nell'apertura del file"); return EXIT_FAILURE;
- mem_err: printf("Non c'e'abbastanza spazio in memoria"); return EXIT_FAILURE;
- fread_err: printf("Il numero acquisito è diverso dal numero inserito\n"); return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment