Advertisement
caxapexac

Untitled

May 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. int cmpChar (const void* x, const void* y) { return (int)(*(char*)x > *(char*)y) ? 1 : (int)(*(char*)x < *(char*)y) ? - 1: 0; }
  6.  
  7. int cmpInt (const void* x, const void* y) { return *(int*)x > *(int*)y ? 1 : *(int*)x < *(int*)y ? -1 : 0; }
  8.  
  9. int cmpDouble (const void* x, const void* y) { return *(double*)x == *(double*)y ? 0 : (*(double*)x > *(double*)y ? 1 : -1); }
  10.  
  11. int universalMax(void* arr, int length, int size, int (*func)(const void*, const void*))
  12. {
  13.     int max = 0;
  14.     for (int i = 1; i < length; i++) max = func((arr + i * size), (arr + max * size)) > 0 ? i : max;
  15.     return max;
  16. }
  17.  
  18. int main ()
  19. {
  20.     int com;
  21.     char* scan;
  22.     scanf("%d ", &com);
  23.     void* array = malloc(20 * com);
  24.     int (*f)(const void*, const void*);
  25.     switch (com)
  26.     {
  27.         case 1:;
  28.             scan = "%c ";
  29.             f = &cmpChar;
  30.             break;
  31.         case 4:;
  32.             scan = "%d ";
  33.             f = &cmpInt;
  34.             break;
  35.         case 8:;
  36.             scan = "%lf ";
  37.             f = &cmpDouble;
  38.             break;
  39.     }
  40.     for (int i = 0; i < 20; i++) scanf(scan, array + i * com);
  41.     printf("%d\n", universalMax(array, 20, com, f));
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement