Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- int comp(const void * p1, const void * p2);
- struct wektor
- {
- float x;
- float y;
- float z;
- };
- int main()
- {
- struct wektor p[5];
- //p = (struct wektor *) malloc(sizeof(struct wektor));
- srand(time(NULL));
- FILE * plik;
- plik = fopen("WEKTORY.BIN","w+b");
- for(int i = 0; i < 5; i++)
- {
- p[i].x = rand()%10;
- p[i].y = rand()%10;
- p[i].z = rand()%10;
- }
- for(int i = 0; i < 5; i++)
- {
- printf("[%.2f,",p[i].x);
- printf("%.2f,",p[i].y);
- printf("%.2f]",p[i].z);
- printf(" %.2f\n",sqrt((pow(p[i].x,2))+(pow(p[i].y,2))+(pow(p[i].z,2))));
- }
- fwrite(p,sizeof(struct wektor),1,plik);
- rewind(plik);
- qsort(p,5,sizeof(struct wektor),comp);
- fwrite(p,sizeof(struct wektor),1,plik);
- rewind(plik);
- fread(p,sizeof(struct wektor),1,plik);
- puts("");
- for(int i = 0; i < 5; i++)
- {
- printf("[%.2f,",p[i].x);
- printf("%.2f,",p[i].y);
- printf("%.2f]",p[i].z);
- printf(" %.2f\n",sqrt((pow(p[i].x,2))+(pow(p[i].y,2))+(pow(p[i].z,2))));
- }
- return 0;
- }
- int comp(const void * p1, const void * p2)
- {
- struct wektor *w1 = (struct wektor*)p1;
- struct wektor *w2 = (struct wektor*)p2;
- 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))))
- return 0;
- 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))))
- return -1;
- else
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement