ADTs de 1ª ordem: números complexos: Difference between revisions
From Wiki**3
No edit summary |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 6: | Line 6: | ||
typedef struct complex *Complex; | typedef struct complex *Complex; | ||
Complex COMPLEXinit(double, double); | Complex '''COMPLEXinit'''(double, double); | ||
double COMPLEXre(Complex); | double '''COMPLEXre'''(Complex); | ||
double COMPLEXim(Complex); | double '''COMPLEXim'''(Complex); | ||
Complex COMPLEXmult(Complex, Complex); | Complex '''COMPLEXmult'''(Complex, Complex); | ||
#endif | #endif | ||
| Line 20: | Line 20: | ||
struct complex { double re, im; }; | struct complex { double re, im; }; | ||
Complex COMPLEXinit(double re, double im) { | Complex '''COMPLEXinit'''(double re, double im) { | ||
Complex t = (Complex)malloc(sizeof(*t)); | Complex t = (Complex)malloc(sizeof(*t)); | ||
t->re = re; t->im = im; return t; | t->re = re; t->im = im; return t; | ||
} | } | ||
double COMPLEXre(Complex c) { return c->re; } | double '''COMPLEXre'''(Complex c) { return c->re; } | ||
double COMPLEXim(Complex c) { return c->im; } | double '''COMPLEXim'''(Complex c) { return c->im; } | ||
Complex COMPLEXmult(Complex a, Complex b) { | Complex '''COMPLEXmult'''(Complex a, Complex b) { | ||
return COMPLEXinit(COMPLEXre(a) * COMPLEXre(b) – COMPLEXim(a) * COMPLEXim(b), | return '''COMPLEXinit'''('''COMPLEXre'''(a) * '''COMPLEXre'''(b) – '''COMPLEXim'''(a) * '''COMPLEXim'''(b), | ||
COMPLEXre(a) * COMPLEXre(b) + COMPLEXim(b) * COMPLEXim(b)); | '''COMPLEXre'''(a) * '''COMPLEXre'''(b) + '''COMPLEXim'''(b) * '''COMPLEXim'''(b)); | ||
} | } | ||
Latest revision as of 08:30, 12 November 2008
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));
}