http://www.sw.it.aoyama.ac.jp/2014/CP1/lecture6.html
© 2005-14 Martin J. Dürst 青山学院大学
typedefswitch (プログラム)
からテーブル (データ) に変更main 関数は超簡単return)本授業について、一番分かることと一番分からないことそれぞれ三つ列挙しなさい。
(basic types)
char, short, int,
long)float, double)(derived types)
struct)、共用体
(union)、列挙型 (enum)struct) の簡単な例typedef struct { // 型 Person の宣言
char name[40];
double height; /* m */
int weight; /* kg */
} Person;
Person student = { "Taro Aoyama", 1.70, 70 }; // 初期化
double bmi = student.weight / // 利用
(student.height * student.height);
(structure)
typedef(教科書 pp. 272-273 参照)
typedef は struct 以外でも使用可能typedef の作成方法と使用方法例: 4×4 の行列 (三次元グラフィックス、ゲーム等で多用)
double a[4][4];double Matrix[4][4];typedef の完成: 定義全体の前に
typedef を付与typedef double Matrix[4][4];typedef の使用: 既存の型名と同様Matrix a;struct Person {...};
(本授業で ×)typedefstruct Person
{...};
typedef struct Person Person;
(本授業で ×)typedef
による型名は同一でも別でもよい)typedef struct Person {...}
Person; (本授業で ×)typedef struct {...} Person; (本授業で ◎)Person p1;(前スライドの (2), (3), (4))
struct Person p1;
(前スライドの (1), (2), (3))
double bmi (Person p)
{
return p.weight
/ (p.height * p.height);
}
int
などと同様)展望: データ + 機能 (関数) ⇒ オブジェクト (object)
(C++ の class は struct の拡張)
enum)int)例: 03C2 (階段じゃんけん) の分かりやすい改造:
typedef enum
{Rock, Scissors, Paper} Janken;
Janken moveA, moveB;
...
if (moveA == Paper)
pointA += 5;
union)struct と同じが、
struct はメンバごとに場所を確保union
はメンバを重ねるので同時に一種のデータしか納まらないtypedef struct { double x1, x2, y1, y2; } Point;typedef struct { double x, y; } Point; Point p1,
p2;typedef は常識