2013年11月9日土曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の10章(Cプリプロセッサ)、10.2(条件付きコンパイル)、10.3(インクルードファイル)、10.4(パラメータをとるマクロ)、10.5(高度な機能)、10.8(プログラミング実習)実習10-3を解いてみる。

その他参考書籍

実習 10-3.

コード

sample.c

#include <stdio.h>
#include <string.h>
#define is_digit(c) ((c) >= '0' && (c) <= '9')
#define is_hex(c) (is_digit(c) || ((c) >= 'A' && (c) <= 'F') || \
    ((c) >= 'a' && (c) <= 'f'))

int main()
{
    char line[100];
    char c;
    int i;
    do {
        fgets(line, sizeof(line), stdin);
        line[strlen(line) - 1] = '\0';
        if (line[0] == '\0'){
            break;
        }
        for (i = 0; line[i] != '\0' && is_hex(line[i]); i++){
            ;
        }
        if (line[i] == '\0'){
            printf("16進数。\n");
        } else {
            printf("16進数ではない。\n");
        }
    } while (1);
    return (0);
}

makefile

CC=cc
CFLAGS=-g

sample: sample.c
 $(CC) $(CFLAGS) -o sample sample.c

clean:
 rm -f sample

入出力結果(Terminal)

$ ./sample
1
16進数。
2
16進数。
3
16進数。
4
16進数。
5
16進数。
a
16進数。
b
16進数。
c
16進数。
d
16進数。
e
16進数。
f
16進数。
A
16進数。
B
16進数。
C
16進数。
D
16進数。
E
16進数。
F
16進数。
G
16進数ではない。
g
16進数ではない。

$

0 コメント:

コメントを投稿