#include #include // a complex number is a + bi struct Complex { double real; // real component double imag; // imaginary component }; struct Complex AddComplex(struct Complex x, struct Complex y) { struct Complex retval; retval.real = x.real + y.real; retval.imag = x.imag + y.imag; return retval; } struct Complex MultiplyComplex(struct Complex x, struct Complex y) { struct Complex retval; retval.real = (x.real * y.real) - (x.imag * y.imag); retval.imag = (x.imag * y.real) - (x.real * y.imag); return retval; } struct Complex* AllocComplex(double real, double imag) { struct Complex *retval = (struct Complex *) malloc(sizeof(struct Complex)); if (retval != NULL) { retval->real = real; retval->imag = imag; } return retval; } int main(int argc, char **argv) { struct Complex a = {1, 2}; struct Complex b = {3, 4}; struct Complex c, *d; c = AddComplex(a, b); c = MultiplyComplex(a, c); d = AllocComplex(c.real, c.imag); if (d != NULL) { printf("%lf + %lfi\n", d->real, d->imag); } free(d); return EXIT_SUCCESS; }