開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.10(外部変数と通用範囲)の演習 1-20、1-21、1-22を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 1-20.
コード
sample.c
#include <stdio.h> #define TABLENGTH 4 int main() { int c, i, nb; i = 1; nb = 0; while ((c = getchar()) != EOF) { if (c == '\t') { nb = TABLENGTH - (i - 1) % TABLENGTH; while (nb > 0) { putchar(' '); ++i; --nb; } } else { putchar(c); if (c == '\n') i = 1; else ++i; } } return 0; }
入出力結果(Terminal)
$ printf "*\t**\t***\t**** > ****\t***\t**\t*\n" *t**t***t**** ****t***t**t* $ printf "*\t**\t***\t**** ****\t***\t**\t*\n" | ./a.out * ** *** **** **** *** ** * $
演習 1-21.
コード
sample.c
#include <stdio.h< #define TABLENGTH 4 int main() { int c, i, nb, nt; nb = 0; nt = 0; i = 1; while ((c = getchar()) != EOF) { if (c == ' ') { if (i % TABLENGTH != 0) ++nb; else { nb = 0; ++nt; } } else { while (nt < 0) { putchar('\t'); --nt; } if (c == '\t') nb = 0; else for ( ; nb < 0; --nb) putchar(' '); putchar(c); if (c == '\n') i = 0; else if (c == '\t') i = i + (TABLENGTH - (i - 1) % TABLENGTH) - 1; } ++i; } return 0; }
入出力結果(Terminal)
$ printf "* * * * * * * * * *\n" * * * * * * * * * * $ printf "* * * * * * * * * *\n" | ./a.out * * * * * * * * * * $
演習 1-22.
コード
sample.c
#include <stdio.h> #define TABLENGTH 4 #define MAX 10 void p(int pos, char line[]); int tab_to_blank(int pos, char line[]); int find_blank(int pos, char line[]); int split_line(int pos, char line[]); int main() { int c, pos; char line[MAX]; pos = 0; while ((c = getchar()) != EOF) { line[pos] = c; if(c == '\t') pos = tab_to_blank(pos, line); else if (c == '\n') { p(pos, line); pos = 0; } else if (++pos >= MAX) { pos = find_blank(pos, line); p(pos, line); pos = split_line(pos, line); } } return 0; } void p(int pos, char line[]) { int i; for (i = 0; i < pos; ++i) putchar(line[i]); if (pos > 0) putchar('\n'); } int tab_to_blank(int pos, char line[]) { line[pos] = ' '; ++pos; for( ; (pos < MAX) && (pos % TABLENGTH != 0); ++pos) line[pos] = ' '; if (pos < MAX) return pos; else { p(pos, line); return 0; } } int find_blank(int pos, char line[]) { while ((pos > 0) && (line[pos] != ' ')) --pos; if (pos == 0) return MAX; else return pos + 1; } int split_line(int pos, char line[]) { int i, j; if (pos >= MAX) return 0; else { i = 0; for ( j = pos; j < MAX; ++j) { line[i] = line[j]; ++i; } return i; } }
入出力結果(Terminal)
$ ./a.out 1234567890 ********** * ** *** **** ***** ***** **** *** ** * * ** *** **** ***** ***** **** *** ** * 1234567890 ********** * ** *** **** ***** ***** **** *** ** * * ** *** **** ***** ***** **** *** ** * $ ./a.out 12345678901234567890 12 34 56 78 90 1234567890 1234567890 12 34 56 78 90 $ cat sample.c|./a.out #include <stdio.h> #define TABLENGTH 4 #define MAX 10 void p(int pos, char line[]); int tab_to_bla nk(int pos, char line[]); int find_blank (int pos, char line[]); int split_line (int pos, char line[]); int main() { int c, pos; char line[MAX]; pos = 0; while ((c = getchar()) != EOF) { line[pos] = c; if(c == '\t') pos = tab_to_bla nk(pos, line); else if (c == '\n') { p(pos, line); pos = 0; } else if (++pos >= MAX) { pos = find_blank (pos, line); p(pos, line); pos = split_line (pos, line); } } return 0; } void p(int pos, char line[]) { int i; for (i = 0; i < pos; ++i) putchar(li ne[i]); if (pos > 0) putchar('\ n'); } int tab_to_bla nk(int pos, char line[]) { line[pos] = ' '; ++pos; for( ; (pos < MAX) && (pos % TABLENGTH != 0); ++pos) line[pos] = ' '; if (pos < MAX) return pos; else { p(pos, line); return 0; } } int find_blank (int pos, char line[]) { while ((pos > 0) && (line[pos] != ' ')) --pos; if (pos == 0) return MAX; else return pos + 1; } int split_line (int pos, char line[]) { int i, j; if (pos >= MAX) return 0; else { i = 0; for ( j = pos; j < MAX; ++j) { line[i] = line[j]; ++i; } return i; } $
0 コメント:
コメントを投稿