Advertisement
Guest User

anyádévagyszar

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