開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第7章(入出力)、7.2(書式付き出力 - printf)、演習7-2を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 7-2.
コード
sample.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXLINE 50
int main()
{
int c, pos;
pos = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t') {
pos += 5;
if (pos >= MAXLINE) {
putchar('\n');
pos = 5;
}
printf("\\0x%02x", c);
} else {
if (++pos > MAXLINE) {
putchar('\n');
pos = 1;
}
putchar(c);
}
if (c == '\n')
pos = 0;
}
return 0;
}
入出力結果(Terminal)
$ ./a.out < sample.c
#include\0x20<stdio.h>
#include\0x20<string.h>
#include\0x20<ctype.h>
#define\0x20MAXLINE\0x2050
int\0x20main()
{
\0x20\0x20\0x20\0x20int\0x20c,\0x20pos;
\0x20\0x20\0x20\0x20
\0x20\0x20\0x20\0x20pos\0x20=\0x200;
\0x20\0x20\0x20\0x20while\0x20((c\0x20=\0x20getcha
r())\0x20!=\0x20EOF)\0x20{
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20if\0x20(c
\0x20==\0x20'\0x20'\0x20||\0x20c\0x20==\0x20'\t')
\0x20{
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20pos\0x20+=\0x205;
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20if\0x20(pos\0x20>=\0x20MAXLINE)
\0x20{
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20\0x20\0x20\0x20\0x20putchar('\n');
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20\0x20\0x20\0x20\0x20pos\0x20=\0x205
;
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20}
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20printf("\\0x%02x",\0x20c);
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20}\0x20else
\0x20{
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20if\0x20(++pos\0x20>\0x20MAXLINE)
\0x20{
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20\0x20\0x20\0x20\0x20putchar('\n');
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20\0x20\0x20\0x20\0x20pos\0x20=\0x201
;
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20}
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20putchar(c);
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20}
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20if\0x20(c
\0x20==\0x20'\n')
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20pos\0x20=\0x200;
\0x20\0x20\0x20\0x20}
\0x20\0x20\0x20\0x20\0x20\0x20\0x20\0x20
\0x20\0x20\0x20\0x20return\0x200;
}$ 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!
$ ./a.out < sample.txt
Ah\0x20Love!\0x20could\0x20you\0x20and\0x20I\0x20w
ith\0x20Fate\0x20conspire
To\0x20grasp\0x20this\0x20sorry\0x20Scheme\0x20of
\0x20Things\0x20entire,
Would\0x20not\0x20we\0x20shatter\0x20it\0x20to
\0x20bits\0x20--\0x20and\0x20then
Re-mould\0x20it\0x20nearer\0x20to\0x20the\0x20Hear
t's\0x20Desire!
ah\0x20love!\0x20could\0x20you\0x20and\0x20i\0x20w
ith\0x20fate\0x20conspire
to\0x20grasp\0x20this\0x20sorry\0x20scheme\0x20of
\0x20things\0x20entire,
would\0x20not\0x20we\0x20shatter\0x20it\0x20to
\0x20bits\0x20--\0x20and\0x20then
re-mould\0x20it\0x20nearer\0x20to\0x20the\0x20hear
t's\0x20desire!
!@#$%Ah\0x20Love!\0x20could\0x20you\0x20and\0x20I
\0x20with\0x20Fate\0x20conspire
!@#$%To\0x20grasp\0x20this\0x20sorry\0x20Scheme
\0x20of\0x20Things\0x20entire,
!@#$%Would\0x20not\0x20we\0x20shatter\0x20it\0x20t
o\0x20bits\0x20--\0x20and\0x20then
!@#$%Re-mould\0x20it\0x20nearer\0x20to\0x20the
\0x20Heart's\0x20Desire!
!@#$%ah\0x20love!\0x20could\0x20you\0x20and\0x20i
\0x20with\0x20fate\0x20conspire
!@#$%to\0x20grasp\0x20this\0x20sorry\0x20scheme
\0x20of\0x20things\0x20entire,
!@#$%would\0x20not\0x20we\0x20shatter\0x20it\0x20t
o\0x20bits\0x20--\0x20and\0x20then
!@#$%re-mould\0x20it\0x20nearer\0x20to\0x20the
\0x20heart's\0x20desire!
$
こんな感じでいいのかなぁ。。
0 コメント:
コメントを投稿