Advertisement
leo11

2nd task kr

Jun 11th, 2021
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. int a1(const void* a){
  2.     return (*(int*)a % 2 == 0 ? 1 : 0);
  3. }
  4.  
  5. int a2(const void* a){
  6.     if (*(double*)a < 0)
  7.         *(double*)a = *(double*)a * (-1);
  8.  
  9.     int aa = (int)*(double*)a;
  10.  
  11.     int flag_1 = 0;
  12.     if (aa % 2 == 0)
  13.         flag_1 = 1;
  14.     else
  15.         return 0;
  16.  
  17.     int flag_2 = 0;
  18.     if (*(double*)a - aa <= 0.000001)
  19.         flag_2 = 1;
  20.     else return 0;
  21.    
  22.     if (flag_2 == 1 && flag_1 == 1)
  23.         return 1;
  24.     else return 0;
  25. }
  26.  
  27. int count_if(void* base, size_t num, size_t size, int (*pred)(const void*)){
  28.     int res = 0;
  29.  
  30.     for (int i = 0; i < num; i++)
  31.         if(pred((char*)base+i*size) == 1)
  32.             ++res;
  33.     return res;
  34. }
  35.  
  36. int main(){
  37.  
  38.     int count = 20;
  39.     void *arr;
  40.     int size;
  41.     scanf("%d", &size);
  42.     int res = 0;
  43.     switch(size){
  44.         case 4:
  45.             arr = (int*) malloc (count* sizeof(int));
  46.             for (int i = 0; i < count; i++)
  47.                 scanf("%d", (int*)arr+i);
  48.            
  49.             res = count_if(arr, count, sizeof(int), a1);
  50.             break;
  51.        
  52.         case 8:
  53.             arr = (double*) malloc (count* sizeof(double));
  54.             for (int i = 0; i < count; i++)
  55.                 scanf("%lf", (double*)arr+i);
  56.  
  57.             res = count_if(arr, count, sizeof(double), a2);
  58.             break;
  59.     }
  60.  
  61.     printf("%d\n", res);
  62.  
  63.  
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement