C: sum of multiples + complex numbers
This commit is contained in:
96
c/complex-numbers/src/complex_numbers.c
Normal file
96
c/complex-numbers/src/complex_numbers.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "complex_numbers.h"
|
||||
|
||||
#ifdef USE_COMPLEX_FUNCTIONS
|
||||
|
||||
complex_t c_add(complex_t a, complex_t b)
|
||||
{
|
||||
return (complex_t) {
|
||||
.real = a.real+b.real,
|
||||
.imag = a.imag+b.imag
|
||||
};
|
||||
}
|
||||
|
||||
complex_t c_sub(complex_t a, complex_t b)
|
||||
{
|
||||
return (complex_t) {
|
||||
.real = a.real-b.real,
|
||||
.imag = a.imag-b.imag
|
||||
};
|
||||
}
|
||||
|
||||
complex_t c_mul(complex_t a, complex_t b)
|
||||
{
|
||||
return (complex_t) {
|
||||
.real = a.real*b.real - a.imag*b.imag,
|
||||
.imag = a.imag*b.real + a.real*b.imag,
|
||||
};
|
||||
}
|
||||
|
||||
complex_t c_div(complex_t a, complex_t b)
|
||||
{
|
||||
double d = b.real*b.real + b.imag*b.imag;
|
||||
return (complex_t) {
|
||||
.real = (a.real*b.real + a.imag*b.imag)/d,
|
||||
.imag = (a.imag*b.real - a.real*b.imag)/d
|
||||
};
|
||||
}
|
||||
|
||||
double c_abs(complex_t x)
|
||||
{
|
||||
return sqrt(x.real*x.real + x.imag*x.imag);
|
||||
}
|
||||
|
||||
complex_t c_conjugate(complex_t x)
|
||||
{
|
||||
return (complex_t) {
|
||||
.real = x.real,
|
||||
.imag = -x.imag
|
||||
};
|
||||
}
|
||||
|
||||
double c_real(complex_t x)
|
||||
{
|
||||
return x.real;
|
||||
}
|
||||
|
||||
double c_imag(complex_t x)
|
||||
{
|
||||
return x.imag;
|
||||
}
|
||||
|
||||
complex_t c_exp(complex_t x)
|
||||
{
|
||||
return (complex_t) {
|
||||
.real = exp(x.real)*cos(x.imag),
|
||||
.imag = exp(x.real)*sin(x.imag)
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* See GNUmakefile below for explanation
|
||||
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||
*/
|
||||
#ifdef UNIT_TEST
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int arg=1;
|
||||
complex_t c1, c2, c3;
|
||||
|
||||
for (; arg<ac-3; ++arg, ++arg) {
|
||||
c1.real=atof(av[arg]);
|
||||
c1.imag=atof(av[arg+1]);
|
||||
|
||||
c2.real=atof(av[arg+2]);
|
||||
c2.imag=atof(av[arg+3]);
|
||||
c3 = c_add(c1, c2);
|
||||
printf("(%f + %fi) + (%f + %fi) = (%f + %fi)\n",
|
||||
c1.real, c1.imag,
|
||||
c2.real, c2.imag,
|
||||
c3.real, c3.imag);
|
||||
}
|
||||
}
|
||||
#endif
|
70
c/complex-numbers/src/complex_numbers.h
Normal file
70
c/complex-numbers/src/complex_numbers.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef _COMPLEX_NUMBERS_H_
|
||||
#define _COMPLEX_NUMBERS_H_
|
||||
#include <math.h>
|
||||
|
||||
typedef struct {
|
||||
double real;
|
||||
double imag;
|
||||
} complex_t;
|
||||
|
||||
/* default is to use macros */
|
||||
#ifdef USE_COMPLEX_FUNCTIONS
|
||||
|
||||
complex_t c_add(complex_t a, complex_t b);
|
||||
complex_t c_sub(complex_t a, complex_t b);
|
||||
complex_t c_mul(complex_t a, complex_t b);
|
||||
complex_t c_div(complex_t a, complex_t b);
|
||||
double c_abs(complex_t x);
|
||||
complex_t c_conjugate(complex_t x);
|
||||
double c_real(complex_t x);
|
||||
double c_imag(complex_t x);
|
||||
complex_t c_exp(complex_t x);
|
||||
|
||||
#else
|
||||
|
||||
#define c_add(a,b) (complex_t) { \
|
||||
(a).real+(b).real, \
|
||||
(a).imag+(b).imag }
|
||||
|
||||
#define c_sub(a,b) (complex_t) { \
|
||||
(a).real-(b).real, \
|
||||
(a).imag-(b).imag }
|
||||
|
||||
#define c_mul(a,b) (complex_t) { \
|
||||
(a).real*(b).real - (a).imag*(b).imag, \
|
||||
(a).imag*(b).real + (a).real*(b).imag }
|
||||
|
||||
#define c_div(a,b) (complex_t) { \
|
||||
((a).real*(b).real + (a).imag*(b).imag)/ \
|
||||
((b).real*(b).real + (b).imag*(b).imag), \
|
||||
((a).imag*(b).real - (a).real*(b).imag)/ \
|
||||
((b).real*(b).real + (b).imag*(b).imag) }
|
||||
|
||||
#define c_exp(x) (complex_t) { \
|
||||
exp((x).real) * cos((x).imag), \
|
||||
exp((x).real) * sin((x).imag) }
|
||||
|
||||
#define c_abs(x) { sqrt((x).real*(x).real + (x).imag*(x).imag) }
|
||||
|
||||
#define c_conjugate(x) (complex_t){ (x).real, -(x).imag }
|
||||
|
||||
#define c_real(x) (x).real
|
||||
|
||||
#define c_imag(x) (x).imag
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* See GNUmakefile below for explanation
|
||||
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||
*/
|
||||
#if defined UNIT_TEST || defined DEBUG
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifdef TESTALL
|
||||
#undef TEST_IGNORE
|
||||
#define TEST_IGNORE() {}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user