diff --git a/exercises/komplex/Makefile b/exercises/komplex/Makefile new file mode 100755 index 0000000000000000000000000000000000000000..1edba9a511df61bf7be21f9b73c2a448878f0fed --- /dev/null +++ b/exercises/komplex/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -O -std=gnu11 +CFLAGS += -Wall +LDFLAGS = -I. +LDLIBS = -lm + +all: out.txt + cat ./$< + +out.txt: main + ./$< > $@ + +main: main.o komplex.o + +main.o komplex.o: komplex.h + +clean: + $(RM) main main.o komplex.o out.txt \ No newline at end of file diff --git a/exercises/komplex/komplex.c b/exercises/komplex/komplex.c new file mode 100755 index 0000000000000000000000000000000000000000..996934679726286ea5b3410bf353ad30394c9cae --- /dev/null +++ b/exercises/komplex/komplex.c @@ -0,0 +1,21 @@ +#include"komplex.h" +#include<stdio.h> + +void komplex_print(char *s, komplex a) { + printf("%s (%g,%g)\n",s,a.re,a.im); +} + +komplex komplex_new(double x, double y) { + komplex z = {x,y}; + return z; +} + +void komplex_set(komplex* z, double x, double y) { + (*z).re = x; + (*z).im = y; +} + +komplex komplex_add (komplex a, komplex b) { + komplex result = { a.re + b.re , a.im + b.im }; + return result; +} \ No newline at end of file diff --git a/exercises/komplex/komplex.h b/exercises/komplex/komplex.h new file mode 100755 index 0000000000000000000000000000000000000000..daf95d5ce1c2cf9578eb07c3fd3176cbde29e856 --- /dev/null +++ b/exercises/komplex/komplex.h @@ -0,0 +1,13 @@ +#ifndef HAVE_KOMPLEX_H +#define HAVE_KOMPLEX_H + +struct komplex {double re; double im;}; +typedef struct komplex komplex; + +void komplex_print(char* s, komplex z); +void komplex_set (komplex* z, double x, double y); +komplex komplex_new (double x, double y); +komplex komplex_add (komplex a, komplex b); +komplex komplex_sub (komplex a, komplex b); + +#endif \ No newline at end of file diff --git a/exercises/komplex/main.c b/exercises/komplex/main.c new file mode 100755 index 0000000000000000000000000000000000000000..2c465c352f183a8cdd2c330d153e764872a96adc --- /dev/null +++ b/exercises/komplex/main.c @@ -0,0 +1,15 @@ +#include"komplex.h" +#include"stdio.h" +#define TINY 1e-6 + +int main(){ + komplex a = {1,2}, b = {3,4}; + + printf("testing komplex_add...\n"); + komplex r = komplex_add(a,b); + komplex R = {4,6}; + komplex_print("a=",a); + komplex_print("b=",b); + komplex_print("a+b should = ", R); + komplex_print("a+b actually = ", r); +} \ No newline at end of file