Advertisement
elelomb

26febbraio19

Jun 17th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <float.h>
  4.  
  5. #define FILEIN1 "d1.bin"
  6. #define FILEIN2 "d2.bin"
  7. #define FILEOUT "res.bin"
  8.  
  9. typedef struct _dato
  10. {
  11. float t;
  12. float mis;
  13. }dato;
  14.  
  15. typedef dato* pdato;
  16. typedef FILE* pfile;
  17.  
  18. int main (void)
  19. {
  20. /**/ unsigned int cont=0,i;
  21. dato d1,d2;
  22. float soglia=FLT_MAX;
  23. pfile pf1,pf2,pf3;
  24.  
  25.  
  26. if((pf1=fopen(FILEIN1,"rb"))==NULL)
  27. {
  28. printf("Non sono riuscito ad aprire il file %s in lettura\n",FILEIN1);
  29. return EXIT_FAILURE;
  30. }
  31.  
  32. if((pf2=fopen(FILEIN2,"rb"))==NULL)
  33. {
  34. printf("Non sono riuscito ad aprire il file %s in lettura\n",FILEIN2);
  35. return EXIT_FAILURE;
  36. }
  37.  
  38. if((pf3=fopen(FILEOUT,"wb"))==NULL)
  39. {
  40. printf("Non sono riuscito ad aprire il file %s in scrittura\n",FILEOUT);
  41. return EXIT_FAILURE;
  42. }
  43.  
  44. do
  45. {
  46. if((fread(&d1,sizeof(dato),1,pf1))!=1) goto fread_err;
  47. if((fread(&d2,sizeof(dato),1,pf2))!=1) goto fread_err;
  48.  
  49. if(d1.t==0&&d2.t==0) /*if(d1.t==d2.t==0) E' SBAGLIATO*/
  50. {
  51. fwrite(&d1,sizeof(dato),1,pf3);
  52. /**/cont++;
  53. }
  54. else
  55. {
  56. printf("Letti:(t=%.2f,v=%.2f) e (t=%.2f,v=%.2f)-",d1.t,d1.mis,d2.t,d2.mis);
  57. if(d1.mis+d2.mis<1.5*soglia)
  58. {
  59. if(d1.t<=d2.t)
  60. {
  61. fwrite(&d1,sizeof(dato),1,pf3);
  62. fwrite(&d2,sizeof(dato),1,pf3);
  63. }
  64. else
  65. if(d1.t>d2.t)
  66. {
  67. fwrite(&d2,sizeof(dato),1,pf3);
  68. fwrite(&d1,sizeof(dato),1,pf3);
  69. printf("invertiti-");
  70. }
  71. soglia=d1.mis+d2.mis;
  72. /**/cont=cont+2;
  73. printf("Salvati sul file\n");
  74. }
  75. else
  76. {
  77. if(d1.t>d2.t)
  78. printf("invertiti-");
  79. printf("Scartati\n");
  80. }
  81.  
  82. }
  83.  
  84. }
  85. while (d1.t==d2.t==0);
  86.  
  87. fclose(pf1);
  88. fclose(pf2);
  89. fclose(pf3);
  90. /**/
  91. if((pf3=fopen(FILEOUT,"rb"))==NULL)
  92. {
  93. printf("Non sono riuscito ad aprire il file %s in lettura\n",FILEOUT);
  94. return EXIT_FAILURE;
  95. }
  96. for(i=0;i<cont;i++)
  97. {
  98. if((fread(&d1,sizeof(dato),1,pf3))!=1) goto fread_err;
  99. printf("(t=%.2f,d=%.2f)\n",d1.t,d1.mis);
  100. }
  101. fclose(pf3);
  102.  
  103. /**/
  104.  
  105. return EXIT_SUCCESS;
  106.  
  107. fread_err:
  108. printf("Si e' verificato un problema nella lettura del file\n");
  109. return EXIT_FAILURE;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement