開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.6(配列)の演習1-13を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 1-13.
コード
sample.c
#include <stdio.h> #define IN 1 #define OUT 0 #define MAXWORDLENGTH 11 #define MAXHIST 51 int main() { int c, i, count, state, overflow, len; int wl[MAXWORDLENGTH]; for(i = 0; i < MAXWORDLENGTH; i++) { wl[i] = 0; } count = 0; state = OUT; while ((c = getchar()) != EOF) { if (c == ' ' || c == '\t' || c == '\n') { state = OUT; if (count > 0) { if (count < MAXWORDLENGTH) { wl[count]++; } else { overflow++; } } count = 0; } else { count++; if (state == OUT) { state = IN; } } } printf("length\\hist"); for (i = 1; i < MAXHIST; i++) { printf(" %2d", i); } putchar('\n'); for (i = 1; i < MAXWORDLENGTH; i++) { printf("%11d", i); len = wl[i] > 0 ? wl[i] : 0; while (len > 0) { printf(" *"); len--; } putchar('\n'); } printf("%11s %d\n", "overflow", overflow); return 0; }
入出力結果(Terminal)
$ ./a.out < sample.c length\hist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3 * * * * * * * * * * * 4 * * * * * * * * * * * * * 5 * * * * * * * * * * * * * 6 * * * * * * * * * 7 * * * * * 8 * * * * * 9 * * 10 * * overflow 13 $ ./a.out < sample.txt length\hist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 * * * * 2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3 * * * * * * * * * * * * * * * * * * * * 4 * * * * * * * * * * * * * * * * * * * * 5 * * * * * * * * * * * * * * * * * * 6 * * * * * * * * * * * * 7 * * * * * * * * * * * * * * * * * * * * 8 * * * * * * 9 10 * * overflow 2 $ cat sample.txt Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! ah love! could you and i with fate conspire to grasp this sorry scheme of things entire, would not we shatter it to bits -- and then re-mould it nearer to the heart's desire! !@#$%Ah Love! could you and I with Fate conspire !@#$%To grasp this sorry Scheme of Things entire, !@#$%Would not we shatter it to bits -- and then !@#$%Re-mould it nearer to the Heart's Desire! !@#$%ah love! could you and i with fate conspire !@#$%to grasp this sorry scheme of things entire, !@#$%would not we shatter it to bits -- and then !@#$%re-mould it nearer to the heart's desire! $
0 コメント:
コメントを投稿