開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第6章(構造体)、6.1(構造体についての基本事項)、6.2(構造体と関数)、6.3(構造体の配列)、演習6-1を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 6-1.
コード
sample.c
#include <stdio.h> #include <ctype.h> #include <string.h> struct key{ char *word; int count; } keytab[] = { "auto", 0, "break", 0, "case", 0, "char", 0, "const", 0, "continue", 0, "default", 0, /* … */ "unsigned", 0, "void", 0, "volatile", 0, "while", 0 }; #define MAXWORD 100 #define NKEYS (sizeof keytab / sizeof keytab[0]) int getword(char *, int); int binsearch(char *, struct key *, int); int main() { int n; char word[MAXWORD]; while (getword(word, MAXWORD) != EOF) { if (isalpha(word[0])) { if ((n = binsearch(word, keytab, NKEYS)) >= 0) { keytab[n].count++; } } } for (n = 0; n < NKEYS; n++) { if (keytab[n].count > 0) { printf("%4d %s\n", keytab[n].count, keytab[n].word); } } return 0; } int binsearch(char *word, struct key tab[], int n) { int cond; int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low + high) / 2; if ((cond = strcmp(word, tab[mid].word)) < 0) { high = mid - 1; } else if (cond > 0) { low = mid + 1; } else { return mid; } } return -1; } int getword(char *word, int lim) { int c, d, getch(void), skipcomment(); void ungetch(int); char *w = word; while (isspace(c = getch())) ; if (c != EOF) { *w++ = c; } if (isalpha(c) || c == '_' || c == '#') { for (; --lim > 0; w++) { if (!isalnum(*w = getch()) && *w != '_') { ungetch(*w); break; } } } else if (c == '\'' || c == '"') { for (; --lim > 0; w++) { if ((*w = getch()) == '\\') { *++w = getch(); } else if (*w == c) { w++; break; } else if (*w == EOF) { break; } } } else if (c == '/') { if ((d = getch()) == '*') { c = skipcomment(); }else { ungetch(d); } } *w = '\0'; return c; } int skipcomment() { int c; while ((c = getch()) != EOF) { if (c == '*') { if ((c = getch()) == '/') { break; } ungetch(c); } } return c; } #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch(void) { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) { if (bufp >= BUFSIZE) { printf("ungetch: too many characters\n"); } else { buf[bufp++] = c; } }
入出力結果(Terminal)
$ ./a.out < sample.c 4 break 8 char 4 void 4 while $
0 コメント:
コメントを投稿