Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <time.h>
- double eps = 1;
- struct vector {
- int x;
- int y;
- int z;
- };
- struct abc {
- struct vector vect;
- double len;
- };
- int intRand(int min, int max) {
- double r = (double) rand()/RAND_MAX;
- return (int) (r*(max-min) + 1);
- }
- double vect_len(struct vector a) {
- double result = pow(a.x*a.x + a.y*a.y + a.z*a.z, 0.5);
- return result;
- }
- void f_d(struct abc *a, int n) {
- for (int i = 0; i < n; i++)
- (*(a + i)).len = vect_len((*(a + i)).vect);
- }
- void print_struct(struct abc st) {
- printf("X: %d, Y: %d, Z: %d \n", st.vect.x, st.vect.y, st.vect.z);
- printf("vector's length: %lf \n", st.len);
- }
- int vector_cmp(const void *a, const void *b) {
- struct abc *as = (struct abc *) a;
- struct abc *bs = (struct abc *) b;
- return ((*as).len > (*bs).len) - ((*as).len < (*bs).len);
- }
- int vector_cmp_eps(const void *a, const void *b) {
- double *as = (double *) a;
- struct abc *bs = (struct abc *) b;
- return (*as > ((*bs).len - eps)) - (*as < ((*bs).len + eps));
- }
- void vect_find_eps(struct abc *V, int n, double len) {
- struct abc *el = bsearch(&len, V, n, sizeof(struct abc), vector_cmp_eps);
- if (el == NULL) {
- printf("Wektor o podanej dlugoci nie istnieje. \n");
- return;
- }
- struct abc *ptr = el;
- while(ptr > V) {
- if (ptr->len >= len - eps) print_struct(*ptr);
- ptr--;
- }
- ptr = el;
- ptr++;
- while(ptr < V + n) {
- if (ptr->len <= len + eps) print_struct(*ptr);
- ptr++;
- }
- }
- void vect_find(struct abc *V, int n, double len) {
- double a = len - eps;
- double b = len + eps;
- int min;
- int max;
- for (int i = 0; i < n; i++) {
- if ( (*(V + i)).len >= a) {
- min = i;
- break;
- }
- }
- for (int i = n - 1; i >= 0; i--) {
- if ( (*(V + i)).len <= b) {
- max = i;
- break;
- }
- }
- for(int i = min; i < max + 1; i++)
- print_struct(*(V+i));
- }
- int main(int argc, char **argv) {
- srand(time(0));
- int n = atoi(argv[1]);
- struct abc *TAB = (struct abc *) malloc(n*sizeof(struct abc));
- if (TAB == NULL) {
- printf("Alokacja pamieci sie nie powiodla. \n");
- return 0;
- }
- for (int i = 0; i < n; i++) {
- TAB[i].vect.x = intRand(0, 21) - 1;
- TAB[i].vect.y = intRand(0, 21) - 1;
- TAB[i].vect.z = intRand(0, 21) - 1;
- }
- f_d(TAB, n);
- printf("Wektory przed sortowaniem: \n");
- for (int i = 0; i < n; i++)
- print_struct(TAB[i]);
- qsort(TAB, n, sizeof(TAB[0]), vector_cmp);
- printf("\n");
- printf("Wektory po sortowaniu: \n");
- for (int i = 0; i < n; i++)
- print_struct(TAB[i]);
- printf("Podaj szukana dlugosc: \n");
- double slen;
- scanf("%lf", &slen);
- printf("\nWektory o podanej dlugosci +/- eps = 1(metoda liniowa) // Dla sprawdzenia poprawnosci ponizszego wyszukiwania z bsearch: \n");
- vect_find(TAB, n, slen);
- printf("\nWektory o podanej dlugosci +/- eps = 1(metoda z uzyciem bsearch): \n");
- vect_find_eps(TAB, n, slen);
- free(TAB);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement