開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio 2017
- プログラミング言語: C(Visual C): Visual Studio 2017
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド - 独自の構造を使う)、長いエクササイズ(p. 229)を取り組んでみる。
長いエクササイズ(p. 229)
コード
#include <stdio.h>
#include <stdlib.h>
struct exercise
{
char const *description;
float duration;
};
struct meal
{
char * const ingredents;
float weight;
};
struct preferences
{
struct meal food;
struct exercise exercise;
};
struct fish
{
char const *name;
char const *species;
int teeth;
int age;
struct preferences care;
};
void label(struct fish a)
{
printf("名前: %s\n種類: %s\n%i本の歯、%i才\n"
"餌は%2.2fキロの%sを与え、%sを%2.2f時間行わせます。\n",
a.name, a.species, a.teeth, a.age,
a.care.food.weight, a.care.food.ingredents,
a.care.exercise.description, a.care.exercise.duration);
}
int main()
{
struct fish snappy
= { "スナッピー", "ピラニア", 69, 4,{{"肉", 0.10},{"ジャグジーでの泳ぎ", 7.50}} };
label(snappy);
system("pause");
return 0;
}
入出力結果(コマンドプロンプト)
名前: スナッピー 種類: ピラニア 69本の歯、4才 餌は0.10キロの肉を与え、ジャグジーでの泳ぎを7.50時間行わせます。 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿