2014年2月1日土曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド)、長いエクササイズ(p.228-229)を解いてみる。

その他参考書籍

長いエクササイズ(p.228-229)

コード(BBEdit, Emacs)

sample187.c

#include <stdio.h>

struct exercise {
    const char *description;
    float duration;
};

struct meal {
    const char *ingredients;
    float weight;
};

struct preferences {
    struct meal food;
    struct exercise exercise;
};

struct fish {
    const char *name;
    const char *species;
    int teeth;
    int age;
    struct preferences care;
};

void catalog(struct fish f)
{
    printf("%sは%sであり、歯は%i本あります。年齢は%i才です。\n",
        f.name, f.species, f.teeth, f.age);
}

void label(struct fish a)
{
    printf("名前: %s\n種類: %s\n%i本の歯、%i才\n", a.name, a.species, a.teeth,
        a.age);
    printf("餌は%2.2fキロの%sを与え、%sを%2.2f時間行わせます。\n",
        a.care.food.weight, a.care.food.ingredients,
        a.care.exercise.description, a.care.exercise.duration);
}

int main(int argc, char *argv[])
{
    struct fish snappy = {"スナッピー", "ピラニア", 69, 4,
        {{"肉", 0.1}, {"ジャクジーでの泳ぎ", 7.5}}};

    label(snappy);
    
    return (0);
}

Makefile

sample228: sample228.c
 cc -g -o sample228 sample228.c

clean:
 rm sample228

入出力結果(Terminal)

$ make sample228 && ./sample228
cc -g -o sample228 sample228.c
名前: スナッピー
種類: ピラニア
69本の歯、4才
餌は0.10キロの肉を与え、ジャクジーでの泳ぎを7.50時間行わせます。
$

0 コメント:

コメントを投稿