From fd62edac6b05377b3af579df13f2f4a617b9621d Mon Sep 17 00:00:00 2001 From: Bora Date: Thu, 22 May 2025 08:57:58 +0200 Subject: [PATCH] halo --- TestComplex.c | 25 +++++++++++++++++++++++++ complex.c | 40 ++++++++++++++++++++++++++++++++++++++++ complex.h | 14 ++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 TestComplex.c create mode 100644 complex.c create mode 100644 complex.h diff --git a/TestComplex.c b/TestComplex.c new file mode 100644 index 0000000..ca51400 --- /dev/null +++ b/TestComplex.c @@ -0,0 +1,25 @@ +#include +#include "complex.h" +int main() { + double re1, im1, re2, im2; + printf("1. Zahl eingeben\nRealteil: "); scanf("%lf", &re1); + printf("Imaginärteil: "); scanf("%lf", &im1); + printf("2. Zahl eingeben\nRealteil: "); scanf("%lf", &re2); + printf("Imaginärteil: "); scanf("%lf", &im2); + Complex *x = createComplex(re1, im1); + Complex *y = createComplex(re2, im2); + Complex *sum = addComplex(x, y); + Complex *diff = subtractComplex(x, y); + Complex *prod = multiplyComplex(x, y); + Complex *quot = divideComplex(x, y); + printf("x = "); printComplex(x); + printf("y = "); printComplex(y); + printf("Summe: x + y = "); printComplex(sum); + printf("Differenz: x - y = "); printComplex(diff); + printf("Produkt: x * y = "); printComplex(prod); + printf("Quotient: x / y = "); printComplex(quot); + freeComplex(x); freeComplex(y); + freeComplex(sum); freeComplex(diff); + freeComplex(prod); freeComplex(quot); + return 0; +} \ No newline at end of file diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..2cbee57 --- /dev/null +++ b/complex.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include "complex.h" + +Complex* createComplex(double real, double imag) { + Complex* z = malloc(sizeof(Complex)); + if (z != NULL) { + z->real = real; + z->imag = imag; + } + return z; +} +void freeComplex(Complex* z) { + free(z); +} +Complex* addComplex(const Complex* a, const Complex* b) { + return createComplex(a->real + b->real, a->imag + b->imag); +} +Complex* subtractComplex(const Complex* a, const Complex* b) { + return createComplex(a->real - b->real, a->imag - b->imag); +} +Complex* multiplyComplex(const Complex* a, const Complex* b) { + double real = a->real * b->real - a->imag * b->imag; + double imag = a->real * b->imag + a->imag * b->real; + return createComplex(real, imag); +} +Complex* divideComplex(const Complex* a, const Complex* b) { + double denom = b->real * b->real + b->imag * b->imag; + if (denom == 0.0) return NULL; + double real = (a->real * b->real + a->imag * b->imag) / denom; + double imag = (a->imag * b->real - a->real * b->imag) / denom; + return createComplex(real, imag); +} +void printComplex(const Complex* z) { + if (z) + printf("(%.3f + %.3fi)\n", z->real, z->imag); + else + printf("NULL (division durch 0?)\n"); +} \ No newline at end of file diff --git a/complex.h b/complex.h new file mode 100644 index 0000000..ab0c16c --- /dev/null +++ b/complex.h @@ -0,0 +1,14 @@ +#ifndef COMPLEX_H +#define COMPLEX_H +typedef struct { + double real; + double imag; +} Complex; +Complex* createComplex(double real, double imag); +void freeComplex(Complex* z); +Complex* addComplex(const Complex* a, const Complex* b); +Complex* subtractComplex(const Complex* a, const Complex* b); +Complex* multiplyComplex(const Complex* a, const Complex* b); +Complex* divideComplex(const Complex* a, const Complex* b); +void printComplex(const Complex* z); +#endif \ No newline at end of file