1-17.c
演習 1-17 80字より長い行を全て印字するプログラムを書け。
#include <stdio.h> #define LENGTHNOTPRINT 80 main() { int c, i, j; int cache[LENGTHNOTPRINT]; i = 0; while ((c = getchar()) != EOF) { ++i; if (i <= LENGTHNOTPRINT) cache[i-1] = c; if (i == LENGTHNOTPRINT+1) { for (j = 0; j < LENGTHNOTPRINT; ++j) putchar(cache[j]); } if (i > LENGTHNOTPRINT) { putchar(c); } if (c == '\n') i = 0; } return 0; }
テスト用テキストファイル test.txt
79:........................................................................... 80:............................................................................ 81:............................................................................. 82:.............................................................................. 90:......................................................................................
$ ./a.out < test.txt
81:.............................................................................
82:..............................................................................
90:......................................................................................
カンニング用サイトは関数使って複雑。もう俺ダメだな。