Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define BUFFER_SIZE 20  //It's said that thecolor has at most 20 character
  6.                         //A number lower than 1000 also has less than 20 characters
  7.  
  8. typedef struct {
  9.     char color[BUFFER_SIZE + 1];    //+1 for /0
  10.     float radius;                   //When we divide the diameter by 2, we may endup with floats
  11. } cup_t;
  12.  
  13.  
  14. int read_cups(cup_t *cups){
  15.  
  16.     char buffer[2*BUFFER_SIZE + 2]; //At most 20chars + 1 space + 20chars + 1 /0
  17.     char *buff1, *buff2;
  18.     int n_cups, num;
  19.    
  20.     scanf("%d\n", &n_cups);
  21.    
  22.     for(int read_cups=0; read_cups<n_cups; ){
  23.  
  24.         fgets(buffer, 2*BUFFER_SIZE + 2, stdin);
  25.    
  26.         if( buffer == NULL ){   //Validate input
  27.             printf("Invalid input. \n");
  28.             continue;
  29.         }
  30.  
  31.         buff1 = strtok(buffer, " ");
  32.         buff2 = strtok(NULL, "\n");
  33.        
  34.         if(buff1 == NULL || buff2 == NULL){ //Validate input
  35.             printf("Invalid input. \n");
  36.             continue;
  37.         }
  38.  
  39.         num = atoi(buff1);
  40.         if( num != 0 ){ //If successful convertion on buff1, then the diameter comes first
  41.             cups[read_cups].radius = num/2.0;
  42.             strcpy( cups[read_cups].color, buff2 );
  43.             read_cups++;                        
  44.         }
  45.         else{ //A failed convertion means the buff1 is not an int, so it must be a color, meaning that the radius should be in buff2
  46.             strcpy( cups[read_cups].color, buff1 );
  47.             num = atoi( buff2 );
  48.            
  49.             if( num != 0){ //Validate input
  50.                 cups[read_cups].radius = num;
  51.                 read_cups++;
  52.             }
  53.             else{
  54.                 printf("Invalid input. \n");
  55.                 continue;
  56.             }
  57.         }
  58.     }
  59.    
  60.     return n_cups;
  61.  
  62. }
  63.  
  64.  
  65. int compare_cup_t(const void * p_a, const void * p_b) {
  66.  
  67.     cup_t *a = (cup_t *)p_a;
  68.     cup_t *b = (cup_t *)p_b;
  69.  
  70.     return (a->radius - b->radius);
  71. }
  72.  
  73.  
  74. int main(void){
  75.    
  76.     cup_t cups[20]; //It's said we have at most 20 cups
  77.     int n_cups = read_cups( cups );
  78.  
  79.     qsort(cups, n_cups, sizeof(cup_t), compare_cup_t);
  80.  
  81.     printf("Sorted:\n");
  82.     for(int i=0; i<n_cups; i++){
  83.         printf("%s %f\n", cups[i].color, cups[i].radius);
  84.     }  
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement