開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第2章(データ型・演算子・式)、2.3(定数)、2.4(宣言)、2.5(算術演算子)、2.6(関係演算子と論理演算子)の演習2-2を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 2-2.
コード
sample.c
#include <stdio.h> #define MAXLINE 1000 int my_getline(char s[], int); int main() { int len; int max; char line[MAXLINE]; while (my_getline(line, MAXLINE) > 0) { printf("%s", line); } return 0; } int my_getline(char s[], int lim) { enum boolean { NO, YES }; enum boolean b; int c, i; i = 0; b = YES; while (b) { if (i >= lim - 1) { b = NO; } else if ((c = getchar()) == '\n') { b = NO; } else if (c == EOF) { b = NO; } else { s[i] = c; ++i; } } if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; }
入出力結果(Terminal)
$ ./a.out < sample.c #include <stdio.h> #define MAXLINE 1000 int my_getline(char s[], int); int main() { int len; int max; char line[MAXLINE]; while (my_getline(line, MAXLINE) > 0) { printf("%s", line); } return 0; } int my_getline(char s[], int lim) { enum boolean { NO, YES }; enum boolean b; int c, i; i = 0; b = YES; while (b) { if (i >= lim - 1) { b = NO; } else if ((c = getchar()) == '\n') { b = NO; } else if (c == EOF) { b = NO; } else { s[i] = c; ++i; } } if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } $ ./a.out < 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! $ 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 コメント:
コメントを投稿