Advertisement
HappyButter

Untitled

Apr 29th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <time.h>
  6. double eps = 1;
  7.  
  8. struct vector {
  9.     int x;
  10.     int y;
  11.     int z;
  12. };
  13.  
  14. struct abc {
  15.     struct vector vect;
  16.     double len;
  17. };
  18.  
  19. int intRand(int min, int max) {
  20.     double r = (double) rand()/RAND_MAX;
  21.     return (int) (r*(max-min) + 1);
  22. }
  23.  
  24. double vect_len(struct vector a) {
  25.     double result = pow(a.x*a.x + a.y*a.y + a.z*a.z, 0.5);
  26.     return result;
  27. }
  28.  
  29. void f_d(struct abc *a, int n) {
  30.     for (int i = 0; i < n; i++)
  31.         (*(a + i)).len = vect_len((*(a + i)).vect);
  32. }
  33.  
  34. void print_struct(struct abc st) {
  35.     printf("X: %d, Y: %d, Z: %d \n", st.vect.x, st.vect.y, st.vect.z);
  36.     printf("vector's length: %lf \n", st.len);
  37. }
  38.  
  39. int vector_cmp(const void *a, const void *b) {
  40.     struct abc *as = (struct abc *) a;
  41.     struct abc *bs = (struct abc *) b;
  42.     return ((*as).len > (*bs).len) - ((*as).len < (*bs).len);
  43. }
  44.  
  45. int vector_cmp_eps(const void *a, const void *b) {
  46.     double *as = (double *) a;
  47.     struct abc *bs = (struct abc *) b;
  48.     return (*as > ((*bs).len - eps)) - (*as < ((*bs).len + eps));
  49.    
  50. }
  51.  
  52. void vect_find_eps(struct abc *V, int n, double len) {
  53.     struct abc *el = bsearch(&len, V, n, sizeof(struct abc), vector_cmp_eps);
  54.     if (el == NULL) {
  55.         printf("Wektor o podanej dlugoci nie istnieje. \n");
  56.         return;
  57.     }
  58.     struct abc *ptr = el;
  59.     while(ptr > V) {
  60.         if (ptr->len >= len - eps) print_struct(*ptr); 
  61.         ptr--;
  62.     }
  63.     ptr = el;
  64.     ptr++;
  65.     while(ptr < V + n) {
  66.         if (ptr->len <= len + eps) print_struct(*ptr); 
  67.         ptr++;
  68.     }
  69. }
  70.  
  71. void vect_find(struct abc *V, int n, double len) {
  72.     double a = len - eps;
  73.     double b = len + eps;
  74.     int min;
  75.     int max;
  76.     for (int i = 0; i < n; i++) {
  77.         if ( (*(V + i)).len >= a) {
  78.             min = i;
  79.             break;
  80.         }
  81.     }
  82.     for (int i = n - 1; i >= 0; i--) {
  83.         if ( (*(V + i)).len <= b) {
  84.             max = i;
  85.             break;
  86.         }
  87.     }
  88.     for(int i = min; i < max + 1; i++)
  89.         print_struct(*(V+i));
  90.    
  91.  
  92. }
  93.  
  94. int main(int argc, char **argv) {
  95.     srand(time(0));
  96.     int n = atoi(argv[1]);
  97.     struct abc *TAB = (struct abc *) malloc(n*sizeof(struct abc));
  98.     if (TAB == NULL) {
  99.         printf("Alokacja pamieci sie nie powiodla. \n");
  100.         return 0;
  101.     }
  102.  
  103.     for (int i = 0; i < n; i++) {
  104.         TAB[i].vect.x = intRand(0, 21) - 1;
  105.         TAB[i].vect.y = intRand(0, 21) - 1;
  106.         TAB[i].vect.z = intRand(0, 21) - 1;
  107.     }
  108.     f_d(TAB, n);
  109.  
  110.     printf("Wektory przed sortowaniem: \n");
  111.     for (int i = 0; i < n; i++)
  112.         print_struct(TAB[i]);
  113.  
  114.     qsort(TAB, n, sizeof(TAB[0]), vector_cmp);
  115.     printf("\n");
  116.  
  117.     printf("Wektory po sortowaniu: \n");
  118.     for (int i = 0; i < n; i++)
  119.         print_struct(TAB[i]);
  120.  
  121.     printf("Podaj szukana dlugosc: \n");
  122.     double slen;
  123.     scanf("%lf", &slen);
  124.  
  125.     printf("\nWektory o podanej dlugosci +/- eps = 1(metoda liniowa) // Dla sprawdzenia poprawnosci ponizszego wyszukiwania z bsearch: \n");
  126.  
  127.     vect_find(TAB, n, slen);
  128.     printf("\nWektory o podanej dlugosci +/- eps = 1(metoda z uzyciem bsearch): \n");
  129.     vect_find_eps(TAB, n, slen);
  130.     free(TAB);
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement