Advertisement
leo11

task2FromDef

Mar 12th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #include<string.h>
  5. #define SIZE 10000
  6.  
  7. int cmp(const void *a, const void *b)
  8. {
  9.     const double *ad, *bd;
  10.    
  11.     ad = (const double*)a;
  12.     bd = (const double*)b;
  13.    
  14.     if (*ad < *bd)
  15.     {  
  16.         return -1;  
  17.     }  
  18.     else if (*ad > *bd)
  19.     {  
  20.         return 1;
  21.     }  
  22.     else
  23.     {  
  24.         return 0;
  25.     }  
  26. }
  27.  
  28. int main()
  29. {
  30.     char *nums = (char*) malloc (SIZE * sizeof(char));
  31.  
  32.     fgets(nums, SIZE, stdin);
  33.     nums[(strlen(nums)) - 1] = '\0';
  34.  
  35.     double *numses = (double *) malloc (sizeof(double));
  36.     char *pch;
  37.     size_t i = 1;
  38.     pch = strtok(nums, " ");
  39.     while (pch != NULL){
  40.         numses[i-1] = atof(pch);
  41.         pch = strtok(NULL, " ");
  42.         ++i;
  43.         numses = realloc(numses, i * sizeof(double));
  44.     }
  45.     // for (int j = 0; j < i - 1; j++) printf("%f ", numses[j]);
  46.     puts("!!");
  47.  
  48.     qsort(numses, i-1, sizeof(double), cmp);
  49.  
  50.     for (int j = 0; j < i - 1; j++) printf("%g ", numses[j]);
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement