Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- struct S
- {
- float a;
- float b;
- int * cmp;
- };
- int cmp2 (const void * x, const void * y)
- {
- //float a,b,c,d;
- //a=((struct S)x)->a;
- // b=((struct S)x)->b;
- //c=((struct S)y)->a;
- //d=((struct S)y)->b;
- //if (fabs(*((float*)a)+*(float*)b)-(*((float*)a)+*(float*)b)<0.0001) return 0;
- //if (*((float*)a)+(*(float*)b))<(*((float*)c)+*(float*)d) return -1;
- //if (*((float*)a)+(*(float*)b)>(*((float*)c)+*(float*)d) return 1;
- }
- int cmp3 (const void * a, const void * b, const void * c, const void * d)
- {
- float x,y,z,w;
- x=*((float*)a);
- y=*((float*)b);
- z=*((float*)c);
- w=*((float*)d);
- if (fabs((float)(x)+(float)(y))-((float)(z)-(float)(w))<0.0001) return 0;
- if (((float)(x)+(float)(y))<((float)(z)-(float)(w))) return -1;
- if (((float)(x)+(float)(y))>((float)(z)-(float)(w))) return 1;
- }
- int universalMax(void* base, size_t num, size_t size)
- {
- int i=0;
- int ind_max = 0;
- float a,b,c,d;
- //printf("%s", typeof((struct S*)(base)));
- a=(float)((**(struct S**)(base)).a);
- for (i=1;i<num;i++)
- {
- a=(float)((**(struct S**)(base+i*size)).a);
- b=(float)((**(struct S**)(base+i*size)).b);
- c=(float)((**(struct S**)(base+ind_max*size)).a);
- d=(float)((**(struct S**)(base+ind_max*size)).b);
- if(cmp3(&a,&b,&c,&d)==1)
- ind_max=i;
- }
- return ind_max;
- }
- int main()
- {
- int length = 20;
- int i;
- int max;
- float a;
- struct S** mass = calloc(3,sizeof(struct S*));
- for(i=0;i<3;i++)
- {
- mass[i] = calloc(1,sizeof(struct S));
- }
- mass[0]->a=1.5;
- mass[0]->b=3.0;
- mass[0]->cmp=&cmp3;
- a=mass[0]->a;
- mass[1]->a=5.5;
- mass[1]->b=2.0;
- mass[1]->cmp=&cmp3;
- mass[2]->a=10.5;
- mass[2]->b=30.0;
- mass[2]->cmp=&cmp3;
- max = universalMax(mass,3,sizeof(struct S*));
- printf("%d", max);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement