report6-1result.txt |
---|
1 Algeria Africa 1520 2382 31599 58991 2 Argentina Latin-America 8380 2767 37032 54522 3 Australia Oceanea 20090 7713 18838 25286 7 Bolivia Latin-America 830 1099 8329 16966 8 Brazil Latin-America 4400 8512 169202 243259 12 Canada North-Americal 19020 9976 30679 36352 14 China Asia 750 9561 1276301 1516664 15 Colombia Latin-America 2140 1139 38905 62284 19 Egypt Africa 1080 1001 68119 115480 20 Ethiopia Africa 100 1097 66175 212732 28 India Asia 380 3288 1006770 1532674 29 Indonesia Asia 1080 1905 212565 318264 30 Iran Asia 2300 1648 76429 170269 35 Kazakstan Asia 1350 2671 16928 22260 42 Mali Africa 240 1240 12559 36817 43 Mexico Latin-America 3670 1958 98881 154120 49 Niger Africa 200 1267 10805 34576 53 Peru Latin-America 2420 1285 25562 42292 58 Russia Europe 2410 17075 146196 114318 60 South-Africa Africa 3520 1221 46257 91466 61 Saudi-Arabia Asia 6800 2150 21661 59812 73 U.S.A. North-America 28020 9809 277825 347543 |
report6-1.c |
---|
#include<stdio.h> #include<stdlib.h> #define BUF_SIZE 1024 int main(void){ FILE *fp; int num, area, gdp, pop2000, pop2050; char *ch_fgets; int ch_sscanf; char nation[256], region[256]; char buf[BUF_SIZE]; fp=fopen("for_file5.5.1.txt", "r"); if(fp==NULL){ printf("Can't Open File\n"); exit(1); } while(1){ ch_fgets=fgets(buf, BUF_SIZE, fp); if(ch_fgets==NULL){ break; } ch_sscanf=sscanf(buf, "%d%s%s%d%d%d%d", &num, nation, region, &gdp, &area, &pop2000, &pop2050); if(ch_sscanf!=7){ printf("Read Error\n"); break; } if(area>=1000 && area<20000){ printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\n", num, nation, region, gdp, area, pop2000, pop2050); } } fclose(fp); return 0; } |
report6-2result.txt |
---|
5 Bangladesh Asia 260 144 128310 218188 14 China Asia 750 9561 1276301 1516664 28 India Asia 380 3288 1006770 1532674 29 Indonesia Asia 1080 1905 212565 318264 30 Iran Asia 2300 1648 76429 170269 32 Israel Asia 15870 21 6077 9144 34 Japan Asia 40940 378 126428 109546 35 Kazakstan Asia 1350 2671 16928 22260 37 Korea(South) Asia 10610 99 46883 52146 38 Kuwait Asia 18720 18 1966 3406 41 Malaysia Asia 4370 330 22299 38089 46 Nepal Asia 210 141 24347 53621 52 Pakistan Asia 480 796 156007 357353 54 Philippines Asia 1160 300 75037 130511 59 Singapore Asia 30550 1 2848 3355 61 Saudi-Arabia Asia 6800 2150 21661 59812 63 Sri-Lanka Asia 740 66 18821 26995 67 Thailand Asia 2960 513 60495 72969 69 Turly Asia 2830 779 65732 97911 75 Vietnam Asia 290 332 80549 129763 76 Yemen Asia 380 528 18118 61129 |
strstr
では,"South-Asia"にもヒットしてしまいます.
(今回扱っているファイルにはないけれど)
report6-2.c |
---|
#include<stdio.h> #include<stdlib.h> #include<string.h> #define BUF_SIZE 1024 int main(void){ FILE *fp; int num, area, gdp, pop2000, pop2050; char *ch_fgets; int ch_sscanf; char nation[256], region[256]; char buf[BUF_SIZE]; fp=fopen("for_file5.5.1.txt", "r"); if(fp==NULL){ printf("Can't Open File\n"); exit(1); } while(1){ ch_fgets=fgets(buf, BUF_SIZE, fp); if(ch_fgets==NULL){ break; } ch_sscanf=sscanf(buf, "%d%s%s%d%d%d%d", &num, nation, region, &gdp, &area, &pop2000, &pop2050); if(ch_sscanf!=7){ printf("Read Error\n"); break; } if(strcmp(region, "Asia")==0){ printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\n", num, nation, region, gdp, area, pop2000, pop2050); } } fclose(fp); return 0; } |
report6-3.c |
---|
#include<stdio.h> #include<stdlib.h> #define BUF_SIZE 1024 int main(void){ FILE *fp; int num, area, gdp, pop2000, pop2050; char *ch_fgets; int ch_sscanf; char nation[256], region[256]; char buf[BUF_SIZE]; int sum=0; int num_nation=0; fp=fopen("for_file5.5.1.txt", "r"); if(fp==NULL){ printf("Can't Open File\n"); exit(1); } while(1){ ch_fgets=fgets(buf, BUF_SIZE, fp); if(ch_fgets==NULL){ break; } ch_sscanf=sscanf(buf, "%d%s%s%d%d%d%d", &num, nation, region, &gdp, &area, &pop2000, &pop2050); if(ch_sscanf!=7){ printf("Read Error\n"); break; } sum+=gdp; num_nation++; } printf("Average GDP:%f\n", ((double)sum)/((double)num_nation)); fclose(fp); return 0; } |
report6-4.c |
---|
#include<stdio.h> #include<stdlib.h> #include<string.h> #define BUF_SIZE 1024 int main(void){ FILE *fp; int num, area, gdp, pop2000, pop2050; char *ch_fgets; int ch_sscanf; char nation[256], region[256]; char buf[BUF_SIZE]; int max_area=0; char max_area_nation[256]; fp=fopen("for_file5.5.1.txt", "r"); if(fp==NULL){ printf("Can't Open File\n"); exit(1); } while(1){ ch_fgets=fgets(buf, BUF_SIZE, fp); if(ch_fgets==NULL){ break; } ch_sscanf=sscanf(buf, "%d%s%s%d%d%d%d", &num, nation, region, &gdp, &area, &pop2000, &pop2050); if(ch_sscanf!=7){ printf("Read Error\n"); break; } if(max_area<area){ max_area=area; strcpy(max_area_nation, nation); } } printf("Nation with maximal area:%s\n", max_area_nation); fclose(fp); return 0; } |
con
は,あくまでもアドレスを保持する変数であって,
単独では文字列を格納できません.
nation
と同じ要素数を持つchar
型配列を用意して,
それにコピーしなければなりません.
MAX_nat=nation;
は配列nation
の先頭アドレスをMAX_nat
に代入することになります.
if(max<=area)
の判定にヒットして
MAX_nat=nation;
が実行された後に,
sscanf
関数呼び出しによって,nation
の要素は書き換わります.
最終的に,MAX_nat
には,最後のsscanf
関数呼び出しによって"Zimbabwe"が格納されたnation
の先頭アドレスを保持していることになってしまいます.
j=0 while(1){ if(nation == '\0')break; max_area_nation[j]=nation[j]; j++; } |
nation[j]
と'\0'
を比較しなければなりません.
while(1){ ... if(ch_fgets==NULL){ break; } ... } |
for(;;){ ... if(ch_fgets==NULL){ break; } ... } |
if(n==0) { max=area; } |
max
をあり得る最小値(この場合では0)に初期化していないのが原因です.