2013年6月21日金曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.5(文字入出力)、1.5.2(文字のカウント)、1.5.4(単語のカウント)の演習1-12を解いてみる。

その他参考書籍

演習 1-12.

コード

sample.c

#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{
    int c, state;
    
    state = OUT;
    while ((c = getchar()) != EOF) {
        if (c != '\n' && c != ' ' && c != '\t') {
            state = IN;
            putchar(c);
        } else if (state == IN) {
            putchar('\n');
            state = OUT;
        }
    }
    if (state == IN) {
        putchar('\n');
    }
    return 0;
}

入出力結果(Terminal)

$ ./a.out < sample.c
#include
<stdio.h>
#define
IN
1
#define
OUT
0
int
main()
{
int
c,
state;
state
=
OUT;
while
((c
=
getchar())
!=
EOF)
{
if
(c
!=
'\n'
&&
c
!=
'
'
&&
c
!=
'\t')
{
state
=
IN;
putchar(c);
}
else
if
(state
==
IN)
{
putchar('\n');
state
=
OUT;
}
}
if
(state
==
IN)
{
putchar('\n');
}
return
0;
}
$ 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
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 コメント:

コメントを投稿