開発環境
- 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 <ctype.h>
#define MAXLINE 50
int main(int argc, char *argv[])
{
int c;
int i;
int col;
int inc(int, int);
col = 1;
for (i = 0; i < MAXLINE; i++) {
if (col == 10) {
col = 0;
}
printf("%d", col);
col++;
}
putchar('\n');
i = 0;
while ((c = getchar()) != EOF) {
if (iscntrl(c) || c == ' ') {
i = inc(i, 6);
printf(" \\x%02x ", c);
if (c == '\n') {
i = 0;
putchar('\n');
}
} else {
i = inc(i, 1);
putchar(c);
}
}
return 0;
}
int inc(int i, int n)
{
if (i + n <= MAXLINE) {
return i + n;
}
putchar('\n');
return n;
}
入出力結果(Terminal)
$ ./a.out < sample.c
12345678901234567890123456789012345678901234567890
#include \x20 <stdio.h> \x0a
#include \x20 <ctype.h> \x0a
\x0a
#define \x20 MAXLINE \x20 50 \x0a
\x0a
int \x20 main(int \x20 argc, \x20 char \x20 *argv[
]) \x0a
{ \x0a
\x20 \x20 \x20 \x20 int \x20 c; \x0a
\x20 \x20 \x20 \x20 int \x20 i; \x0a
\x20 \x20 \x20 \x20 int \x20 col; \x0a
\x20 \x20 \x20 \x20 int \x20 inc(int, \x20 int
); \x0a
\x20 \x20 \x20 \x20 \x0a
\x20 \x20 \x20 \x20 col \x20 = \x20 1; \x0a
\x20 \x20 \x20 \x20 for \x20 (i \x20 = \x20 0;
\x20 i \x20 < \x20 MAXLINE; \x20 i++) \x20 {
\x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 if
\x20 (col \x20 == \x20 10) \x20 { \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 col \x20 = \x20 0; \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 }
\x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 pr
intf("%d", \x20 col); \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 co
l++; \x0a
\x20 \x20 \x20 \x20 } \x0a
\x20 \x20 \x20 \x20 putchar('\n'); \x0a
\x20 \x20 \x20 \x20 i \x20 = \x20 0; \x0a
\x20 \x20 \x20 \x20 while \x20 ((c \x20 =
\x20 getchar()) \x20 != \x20 EOF) \x20 { \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 if
\x20 (iscntrl(c) \x20 || \x20 c \x20 == \x20 '
\x20 ') \x20 { \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 i \x20 = \x20 inc(i, \x20
6); \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 printf(" \x20 \\x%02x
\x20 ", \x20 c); \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 if \x20 (c \x20 == \x20 '\
n') \x20 { \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 i
\x20 = \x20 0; \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 pu
tchar('\n'); \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 } \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 }
\x20 else \x20 { \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 i \x20 = \x20 inc(i, \x20
1); \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20
\x20 \x20 \x20 \x20 putchar(c); \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 }
\x0a
\x20 \x20 \x20 \x20 } \x0a
\x20 \x20 \x20 \x20 return \x20 0; \x0a
} \x0a
\x0a
int \x20 inc(int \x20 i, \x20 int \x20 n) \x0a
{ \x0a
\x20 \x20 \x20 \x20 if \x20 (i \x20 + \x20 n
\x20 <= \x20 MAXLINE) \x20 { \x0a
\x20 \x20 \x20 \x20 \x20 \x20 \x20 \x20 re
turn \x20 i \x20 + \x20 n; \x0a
\x20 \x20 \x20 \x20 } \x0a
\x20 \x20 \x20 \x20 putchar('\n'); \x0a
\x20 \x20 \x20 \x20 return \x20 n; \x0a
} \x0a
$
0 コメント:
コメントを投稿