#include #include /* 複素数型の定義 */ typedef struct { double re, im; } Complex; /* 複素数の初期化関数 */ Complex ComplexInit(double re, double im) { Complex c; c.re = re; c.im = im; return (c); } /* 複素数を表示する関数 */ void ComplexPrint(char *str, Complex c) { if (c.im < 0.0) { printf("%s %f - %f i\n", str, c.re, -c.im); } else { printf("%s %f + %f i\n", str, c.re, c.im); } } /* 複素数の積を計算する関数 */ Complex ComplexProduct(Complex a, Complex b) { Complex c; c.re = a.re*b.re - a.im*b.im; c.im = a.re*b.im + a.im*b.re; return (c); } /* メイン関数 */ int main() { Complex a = {1.0, 1.0}; Complex b, c; b = ComplexInit(1.0, -1.0); ComplexPrint("a = ", a); ComplexPrint("b = ", b); c = ComplexProduct(a, b); ComplexPrint("a*b = ", c); return (EXIT_SUCCESS); }