開発環境
- 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-14を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 1-14.
コード
sample.c
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXLENGTH 1000
#define MAXHIST 51
int main()
{
int c, i, j, len;
int cs[MAXLENGTH];
for (i = 0; i < MAXLENGTH; i++) {
cs[i] = 0;
}
printf("char\\hist");
for (i = 1; i < MAXHIST; i++) {
printf(" %-2d" ,i);
}
putchar('\n');
while ((c = getchar()) != EOF) {
cs[c]++;
}
for(i = 0; i < MAXLENGTH; i++) {
len = cs[i];
if (len > 0) {
switch (i) {
case ' ': printf("%9s", "blank"); break;
case '\a': printf("%9s", "\\a"); break;
case '\b': printf("%9s", "\\b"); break;;
case '\f': printf("%9s", "\\f"); break;;
case '\n': printf("%9s", "\\n"); break;;
case '\r': printf("%9s", "\\r"); break;;
case '\t': printf("%9s", "\\t"); break;;
case '\v': printf("%9s", "\\v"); break;;
default: printf("%9c", i); break;
}
if (len >= MAXHIST) {
printf(" overflow");
} else {
for (j = 0; j < len; j++) {
printf(" * ");
}
}
putchar('\n');
}
}
return 0;
}
入出力結果(Terminal)
$ ./a.out < sample.c
char\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
\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
blank overflow
! *
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# * * * * *
% * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * *
( * * * * * * * * * * * * * * * * * * * * * * * * * *
) * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
+ * * * * * * * * * *
, * * * * * * * * * * * * *
- *
. *
0 * * * * * * * * * *
1 * * * *
2 *
5 *
9 * * * * * * * * *
: * * * * * * * * *
; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
< * * * * *
= * * * * * * * * *
> * * *
A * * * * * * *
E * * * * *
F *
G * * * *
H * * * * * * *
I * * * *
L * * * *
M * * * * * * *
N * * * * *
O * *
S * * *
T * * * * * * * *
U *
X * * * * * * *
[ * * * *
\ * * * * * * * * * * * * * * * * * * * * * * * * *
] * * * *
a * * * * * * * * * * * * * * * * * * * * * * * * * *
b * * * * * * * * * * * *
c * * * * * * * * * * * * * * * * * * * * * *
d * * * * * * * *
e * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
f * * * * * * * * * * * * * * * * * * * * * * * * * * *
g *
h * * * * * * * *
i * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
j * * * *
k * * * * * * * * * *
l * * * * * * * * * * *
m *
n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
o * * * * * * *
p * * * * * * * * * * * * * * *
r * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
s * * * * * * * * * * * * * * * * * * * * * * * *
t * * * * * * * * * * * * * * * * * * * * * * * * * *
u * * * * *
v * * *
w * * *
{ * * * * * * * * * *
} * * * * * * * * * *
$ ./a.out < sample.txt
char\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
\n * * * * * * * * * * * * * * * *
blank overflow
! * * * * * * * * * * * * * * * *
# * * * * * * * *
$ * * * * * * * *
% * * * * * * * *
' * * * *
, * * * *
- * * * * * * * * * * * *
@ * * * * * * * *
A * *
D * *
F * *
H * *
I * *
L * *
R * *
S * *
T * * * *
W * *
a * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
b * * * *
c * * * * * * * * * * * *
d * * * * * * * * * * * * * * * * * * * * * *
e overflow
f * * * * * *
g * * * * * * * *
h * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
i * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
l * * * * * * * * * * * * * *
m * * * * * * * *
n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
o * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
p * * * * * * * *
r * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
s * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
t overflow
u * * * * * * * * * * * * * * * *
v * * * *
w * * * * * * * * * *
y * * * * * * * *
$ 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 コメント:
コメントを投稿