Advertisement
Guest User

Untitled

a guest
May 24th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. int rendez(const void *a, const void *b){
  2.     struct country *pa = (struct country*)a;
  3.     struct country *pb = (struct country*)b;
  4.  
  5.     if(pa->area > pb->area)
  6.         return -1;
  7.     if(pa->area < pb->area)
  8.         return 1;
  9.     if(pa->area == pb->area){
  10.         return strcmp(pa->name,pb->name);
  11.     }
  12.  
  13. }
  14.  
  15. struct country *foo(char *s, int ter, int nep){
  16.  
  17.     FILE *f;
  18.     f = fopen(s,"rb");
  19.     struct country orszag;
  20.     struct country *orszagok;
  21.     int db=0,i=0;
  22.     float nepsuruseg;
  23.     float ratio = ter/nep;
  24.  
  25.  
  26.     while(fread(&orszag,sizeof(struct country),1,f)){
  27.         nepsuruseg = orszag.area / orszag.population;
  28.         if(nepsuruseg > ratio){
  29.             db++;
  30.         }
  31.     }
  32.  
  33.     orszagok = malloc( (db+1)*sizeof(struct country));
  34.     rewind(f);
  35.  
  36.     while(fread(&orszag,sizeof(struct country),1,f)){
  37.        nepsuruseg = orszag.area / orszag.population;
  38.         if(nepsuruseg > ratio){
  39.             orszagok[i] = orszag;
  40.             i++;
  41.         }
  42.     }
  43.  
  44.     qsort(orszagok,db,sizeof(struct country),rendez);
  45.  
  46.     orszagok[db].area = -1;
  47.  
  48.     fclose(f);
  49.     return orszagok;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement