halo
This commit is contained in:
parent
d1ea8cc429
commit
fd62edac6b
25
TestComplex.c
Normal file
25
TestComplex.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
40
complex.c
Normal file
40
complex.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#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");
|
||||
}
|
14
complex.h
Normal file
14
complex.h
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user