処理する必要があるテキスト ファイルがあります。形式は次のとおりです。
dd - 日 mm - 月 yyyy - 年 x,y,w,z - 気温
dd,mm,yyyy,x,y,z,w
1,1,2010,20.8,19.2,29.3,20.9
2,1,2010,22.5,15.5,30.7,23.3
3,1,2010,21.4,14.5,21.5,18.9
4,1,2010,27.6,13.4,23.9,18.2
5,1,2010,25,16,26.1,18.3
6,1,2010,23.6,16.1,27.6,21.8
...
29,1,2010,23.5,17.5,30.2,19.6
30,1,2010,36.2,13.4,27.3,20.5
31,1,2010,37.2,17.1,26.6,21.5
1,2,2010,24.9,16.9,27.7,22.6
2,2,2010,35.2,16.7,27.7,22.7
3,2,2010,34.8,21.6,27.3,21.4
...
1,12,2010,26.6,16.5,20.1,17.2
2,12,2010,27.2,17.2,24.3,18.5
3,12,2010,30,17.2,24.4,19.8
...
30,12,2010,23.7,14.2,26.5,20
31,12,2010,41.1,14.9,27.2,21.4
このデータを処理するために配列を使用することは教えられていません。関数、if/else ステートメント、ループ (while/for)、および操作のみです。
各月のデータを個別に処理する必要があります。私のメインコードはwhile (scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &x, &y, &z, &w) ==7) {}
ループに含まれています。
以下は私のコードです:
while (scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &melmax, &melmin, &sydmax, &sydmin) ==7) {
if (totallinesread==0) {
currentmonth = mm;
printf("Stage 4\n=======\n");
}
if (mm == currentmonth) {
daysinmonthcounted+=1;
/*other totals*/
}else if (mm !=currentmonth){
/*can execute code here as needed such as generating graphs etc*/
printf("0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)\n");
printf("%d\n", daysinmonthcounted);
/* make new currentmonth = mm, reset days counted = 1 */
currentmonth = mm;
daysinmonthcounted = 1;
}
totallinesread+=1;
}
出力:
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
28
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
(Note missing december 31 days at the end)
これが機能し、希望どおりに出力されることがわかりました(現在、各月の合計日数のみを印刷しています)が、先月は完全にスキップされています。ファイルの終わり (EOF) と関係があると考えています。if ステートメントで使用する次の「mm」値がないため ( currentmonth != mm
)、停止しており、12 月の追加の計算コードを処理できません。 .
これが機能する方法はありますか?私はそれを読みましたがscanf(EOF) = -1
、どちらで動作するはずcurrentmonth != mm
ですか?
データ テキスト ファイルにはエラーや重複エントリがなく、昇順になっていると見なすことができます。
どんな助けでも大歓迎です。
ありがとうございました!