■やりたい事
コマンド行に整数値の列を収めたファイル名が与えられたとき、そのファイル名のファイルが昇順列を収めたものになっているかどうかを判定する。ここで、整数値の列が昇順列であるとは、並んでいる順にだんだんと値が大きくなる(その列の中で隣り合う2つの整数値 x, y が必ず x < y となっている)ことをいう。
与えられたファイル名のファイルの中身が昇順列であるかどうかを調べ、その結果を書き出すプログラムを作れ。書出しは、ファイル名に続けて「: 」を書き出し、その後に続けて、昇順列であれば「increasing」、そうでなければ「not increasing」と書き出して、最後に改行すること。
なお、空の列は昇順列。
C
1#include <stdio.h> 2#include <stdlib.h> 3int main(int argc, char *argv[]){ 4 5 FILE *f; 6 f= fopen(argv[1],"r"); 7 if( f==NULL ){ 8 printf("%s: can't open.\n",argv[1]); exit(-1); 9 } 10 printf("%s: "); 11 12 ・・・ファイル変数fに対応するファイルの中身を調べて 13 ・・・昇順列なら"increasing"、 14 ・・・そうでないなら"not increasing"と書き出す 15 16 fclose(f); 17 18 return 0; 19}
やりたい事に則した...の部分を教えて頂きたく。
■追記
C
1#include <stdio.h> 2#include <stdlib.h> 3int main(int argc, char *argv[]){ 4 5 FILE *f; 6 f= fopen(argv[1],"r"); 7 8 if( f==NULL ){ 9 printf("%s: can't open.\n",argv[1]); exit(-1); 10 } 11 printf("%s: "); 12{ 13 int increasing = 1; 14 int first = 1; 15 int max; 16 char buffer[30001]; 17 char * t = NULL; 18 19 if(NULL == fgets(buffer, sizeof(buffer) - 1, f)){ 20 printf("%s: can't reed.\n",argv[1]); exit(-1); 21 } 22 buffer[sizeof(buffer) -1] = '¥0'; 23 24 t = strtok(buffer, " "); 25 while(t != NULL) { 26 int i = atoi(t); 27 if(first == 1){ 28 max = i; 29 first = 0; 30 }else if(i < max){ 31 increasing = 0; 32 break; 33 } 34 ptr = strtok(NULL, " "); 35 } 36 37 if(increasing == 1){ 38 printf("increasing\n"); 39 }else{ 40 puts("not increasing\n"); 41 } 42} 43 44 fclose(f); 45 46 return 0; 47}
上記のように追記しましたがコンパイルエラーがでてしまいました。↓
p8-3.c: In function 'main': p8-3.c:11:5: warning: format '%s' expects a matching 'char *' argument [-Wformat=] printf("%s: "); ^ p8-3.c:23:37: warning: multi-character character constant [-Wmultichar] buffer[sizeof(buffer) -1] = '¥0'; ^ p8-3.c:23:9: warning: overflow in implicit constant conversion [-Woverflow] buffer[sizeof(buffer) -1] = '¥0'; ^ p8-3.c:25:5: warning: implicit declaration of function 'strtok' [-Wimplicit-function-declaration] t = strtok(buffer, " "); ^ p8-3.c:25:7: warning: assignment makes pointer from integer without a cast [enabled by default] t = strtok(buffer, " "); ^ p8-3.c:35:9: error: 'ptr' undeclared (first use in this function) ptr = strtok(NULL, " "); ^ p8-3.c:35:9: note: each undeclared identifier is reported only once for each function it appears in
■成功例として
コマンドライン入力
data1.txt
標準出力
data1.txt: increasing
コマンドライン入力
data2.txt
標準出力
data2.txt: not increasing
コマンドライン入力
data3.txt
標準出力
data3.txt: increasing
再度教えて頂きたく。。。
回答4件
あなたの回答
tips
プレビュー