typedef struct card Card struct card Card 15 Slides: 32 Download presentation 別名の定義 typedef struct card Card; struct card 型の雛形 Card型の雛形 15 構造体型の配列宣言 #define MAXCARD 3 Card x[MAXCARD]; Card型の変数 x[0] x[1] x[2] 16 参照のイメージ Complex z 1; Complex型の雛形 real imag Complex型の変数 real imag z 1 real z 1. real imag z 1. imag 18 イメージ Complex real z 1; imag z 1 (z 1. imag)=2. 0; (z 1. real)=1. 0; imag real z 1. imag z 1. real imag z 1 21 練習2 /*test_struct. c 構造体実験 #include <stdio. h> コメント省略*/ struct complex { double real; double imag; }; typedef /* struct complex 次に続く Complex; */ 24 int { main() Complex z 1; z 2; printf("メンバの読み込みn"); printf("z 1= (real? ) + (imag? )i "); scanf(“%lf %lf", &(z 1. real), &(z 1. imag)); printf("読み込み後n"); printf("z 1=%4. 2 f+(%4. 2 f)in", z 1. real, z 1. imag); printf("z 2=%4. 2 f+(%4. 2 f)in", z 2. real, z 2. imag); /* 続く*/ 25 /* 続き */ printf(“z 2=z 1実行中n"); z 2=z 1; printf("代入後n"); pritnf("z 1=%4. 2 f +(%4. 2 f)in", z 1. real, z 1. imag); pritnf("z 2=%4. 2 f +(%4. 2 f)in", z 2. real, z 2. imag); } return 0; 26 /* 続き */ /*計算処理*/ sum=plus_complex(z 1, z 2); /*出力処理*/ printf(“sum=z 1+z 2 n"); printf("(%4. 2 f+(%6. 2 f)i )=", sum. real, sum. imag); printf("(%4. 2 f+(%4. 2 f)i)+", z 1. real, z 1. imag); printf("(%4. 2 f+(%4. 2 f)i)n", z 2. real, z 2. imag); return 0; /*正常終了*/ } /*main関数終了*/ 30 実行結果 $make gcc pluscomplex. c -o pluscomplex $. /pluscomplex 2つの複素数z 1, z 2を入力して下さい。 z 1の実部は? 1. 0 z 1の虚部は? 2. 0 z 2の実部は? 3. 0 z 2の虚部は? 4. 0 sum=z 1+z 2 (4. 00+(6. 00)i)=(1. 00+(2. 00)i)+(3. 00+(4. 00)i) $ 32