http://www.sw.it.aoyama.ac.jp/2015/CP1/lecture6.html
© 2005-15 Martin J. Dürst 青山学院大学
typedef| 05A1 | 05B1 | 05B2 | 05B3 | 05C1 | 05C2 | |
| 100点 | 87 | 58 | 76 | 38 | 22 | 7 |
| 60点 | 8 | 37 | 17 | 54 | 54 | 52 |
| エラー | - | - | 2 | 1 | 9 | 9 |
| 未提出 | - | - | - | 2 | 10 | 27 |
[昨年度資料につき削除]
[昨年度資料につき削除]
本授業について、一番分かることと一番分からないことそれぞれ三つ列挙しなさい。
(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 の拡張)
引数でも戻り値でも構造体が使用可能
具体例:
Complex add(Complex a, Complex b)
{
Complex result;
/* a と b の実部と虚部から result の実部と虚部を計算 */
return result;
}
enum)int)例: 03C2、03C3 (階段じゃんけん) の分かりやすい改造:
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 は常識入出力の詳細に注意