Advertisement
Guest User

Untitled

a guest
May 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. struct S
  7. {
  8.     float a;
  9.     float b;
  10.     int * cmp;
  11. };
  12.  
  13.  
  14.  
  15. int cmp2 (const void * x, const void * y)
  16. {
  17.     //float a,b,c,d;
  18.     //a=((struct S)x)->a;
  19.    // b=((struct S)x)->b;
  20.     //c=((struct S)y)->a;
  21.     //d=((struct S)y)->b;
  22. //if (fabs(*((float*)a)+*(float*)b)-(*((float*)a)+*(float*)b)<0.0001) return 0;
  23.   //if (*((float*)a)+(*(float*)b))<(*((float*)c)+*(float*)d) return -1;
  24.   //if (*((float*)a)+(*(float*)b)>(*((float*)c)+*(float*)d) return 1;
  25. }
  26.  
  27. int cmp3 (const void * a, const void * b, const void * c, const void * d)
  28. {
  29.     float x,y,z,w;
  30.     x=*((float*)a);
  31.     y=*((float*)b);
  32.     z=*((float*)c);
  33.     w=*((float*)d);
  34.     if (fabs((float)(x)+(float)(y))-((float)(z)-(float)(w))<0.0001) return 0;
  35.     if (((float)(x)+(float)(y))<((float)(z)-(float)(w))) return -1;
  36.     if (((float)(x)+(float)(y))>((float)(z)-(float)(w))) return 1;
  37. }
  38.  
  39. int universalMax(void* base, size_t num, size_t size)
  40. {
  41.     int i=0;
  42.     int ind_max = 0;
  43.     float a,b,c,d;
  44.     //printf("%s", typeof((struct S*)(base)));
  45.     a=(float)((**(struct S**)(base)).a);
  46.         for (i=1;i<num;i++)
  47.         {
  48.             a=(float)((**(struct S**)(base+i*size)).a);
  49.             b=(float)((**(struct S**)(base+i*size)).b);
  50.             c=(float)((**(struct S**)(base+ind_max*size)).a);
  51.             d=(float)((**(struct S**)(base+ind_max*size)).b);
  52.             if(cmp3(&a,&b,&c,&d)==1)
  53.             ind_max=i;
  54.         }
  55.    return ind_max;
  56. }
  57.  
  58.  
  59. int main()
  60. {
  61.     int length = 20;
  62.     int i;
  63.     int max;
  64.     float a;
  65.     struct S** mass = calloc(3,sizeof(struct S*));
  66.     for(i=0;i<3;i++)
  67.     {
  68.      mass[i] = calloc(1,sizeof(struct S));
  69.     }
  70.  
  71.     mass[0]->a=1.5;
  72.     mass[0]->b=3.0;
  73.     mass[0]->cmp=&cmp3;
  74.     a=mass[0]->a;
  75.     mass[1]->a=5.5;
  76.     mass[1]->b=2.0;
  77.     mass[1]->cmp=&cmp3;
  78.  
  79.     mass[2]->a=10.5;
  80.     mass[2]->b=30.0;
  81.     mass[2]->cmp=&cmp3;
  82.     max = universalMax(mass,3,sizeof(struct S*));
  83.  
  84.  
  85.     printf("%d", max);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement