Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. int comp(const void * p1, const void * p2);
  6. struct wektor
  7. {
  8. float x;
  9. float y;
  10. float z;
  11. };
  12.  
  13.  
  14.  
  15. int main()
  16. {
  17. struct wektor p[5];
  18. //p = (struct wektor *) malloc(sizeof(struct wektor));
  19.  
  20. srand(time(NULL));
  21. FILE * plik;
  22. plik = fopen("WEKTORY.BIN","w+b");
  23.  
  24. for(int i = 0; i < 5; i++)
  25. {
  26. p[i].x = rand()%10;
  27. p[i].y = rand()%10;
  28. p[i].z = rand()%10;
  29. }
  30.  
  31. for(int i = 0; i < 5; i++)
  32. {
  33. printf("[%.2f,",p[i].x);
  34. printf("%.2f,",p[i].y);
  35. printf("%.2f]",p[i].z);
  36. printf(" %.2f\n",sqrt((pow(p[i].x,2))+(pow(p[i].y,2))+(pow(p[i].z,2))));
  37. }
  38.  
  39. fwrite(p,sizeof(struct wektor),1,plik);
  40.  
  41. rewind(plik);
  42.  
  43. qsort(p,5,sizeof(struct wektor),comp);
  44.  
  45. fwrite(p,sizeof(struct wektor),1,plik);
  46. rewind(plik);
  47. fread(p,sizeof(struct wektor),1,plik);
  48. puts("");
  49. for(int i = 0; i < 5; i++)
  50. {
  51. printf("[%.2f,",p[i].x);
  52. printf("%.2f,",p[i].y);
  53. printf("%.2f]",p[i].z);
  54. printf(" %.2f\n",sqrt((pow(p[i].x,2))+(pow(p[i].y,2))+(pow(p[i].z,2))));
  55. }
  56.  
  57.  
  58. return 0;
  59. }
  60.  
  61. int comp(const void * p1, const void * p2)
  62. {
  63. struct wektor *w1 = (struct wektor*)p1;
  64. struct wektor *w2 = (struct wektor*)p2;
  65. if(sqrt((pow(w1->x,2))+(pow(w1->y,2))+(pow(w1->z,2))) == sqrt((pow(w2->x,2))+(pow(w2->y,2))+(pow(w2->z,2))))
  66. return 0;
  67. else if(sqrt((pow(w1->x,2))+(pow(w1->y,2))+(pow(w1->z,2))) < sqrt((pow(w2->x,2))+(pow(w2->y,2))+(pow(w2->z,2))))
  68. return -1;
  69. else
  70. return 1;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement