Interface
#ifndef __COMPLEX_H__
#define __COMPLEX_H__
typedef struct complex *Complex;
Complex COMPLEXinit(double, double);
double COMPLEXre(Complex);
double COMPLEXim(Complex);
Complex COMPLEXmult(Complex, Complex);
#endif
Implementação
#include <stdlib.h>
#include "COMPLEX.h"
struct complex { double re, im; };
Complex COMPLEXinit(double re, double im) {
Complex t = (Complex)malloc(sizeof(*t));
t->re = re; t->im = im; return t;
}
double COMPLEXre(Complex c) { return c->re; }
double COMPLEXim(Complex c) { return c->im; }
Complex COMPLEXmult(Complex a, Complex b) {
return COMPLEXinit(COMPLEXre(a) * COMPLEXre(b) – COMPLEXim(a) * COMPLEXim(b),
COMPLEXre(a) * COMPLEXre(b) + COMPLEXim(b) * COMPLEXim(b));
}