108 lines
3.0 KiB
C
108 lines
3.0 KiB
C
/**********************************************************************\
|
|
* Kurzbeschreibung: complex.c
|
|
* Bietet Funktionen zum Rechnen mit komplexen Zahlen
|
|
*
|
|
* Datum: Autor: Grund der Aenderung:
|
|
*
|
|
*
|
|
\**********************************************************************/
|
|
|
|
/*--- #includes ------------------------------------------------------*/
|
|
#include <float.h>
|
|
#include <stdio.h>
|
|
#include "complex.h"
|
|
|
|
/*--- #defines -------------------------------------------------------*/
|
|
|
|
/*--- Lokale Datentypen (typedef) ------------------------------------*/
|
|
|
|
/*--- Modulglobale static Variablen ----------------------------------*/
|
|
|
|
/*--- Prototypen lokaler Funktionen ----------------------------------*/
|
|
|
|
/*--- Funktionsdefinitionen ------------------------------------------*/
|
|
|
|
/*--------------------------------------------------------------------*\
|
|
* Addition komplexer Zahlen: x + y
|
|
* Rueckgabe: Ergebnis im Typ "Complex"
|
|
\*--------------------------------------------------------------------*/
|
|
Complex addComplex(Complex x, Complex y)
|
|
{
|
|
Complex summe;
|
|
|
|
summe.real = x.real + y.real;
|
|
summe.imag = x.imag + y.imag;
|
|
|
|
printf("Summe: x + y = ");
|
|
printComplex(summe);
|
|
|
|
return summe;
|
|
}
|
|
|
|
/*--------------------------------------------------------------------*\
|
|
* Subtraktion komplexer Zahlen: x - y
|
|
* Rueckgabe: Ergebnis im Typ "Complex"
|
|
\*--------------------------------------------------------------------*/
|
|
Complex subComplex(Complex x, Complex y)
|
|
{
|
|
Complex diff;
|
|
|
|
diff.real = x.real - y.real;
|
|
diff.imag = x.imag - y.imag;
|
|
|
|
printf("Differenz: x - y = ");
|
|
printComplex(diff);
|
|
|
|
return diff;
|
|
}
|
|
|
|
/*--------------------------------------------------------------------*\
|
|
* Multiplikation komplexer Zahlen: x * y
|
|
* Rueckgabe: Ergebnis im Typ "Complex"
|
|
\*--------------------------------------------------------------------*/
|
|
Complex mulComplex(Complex y, Complex x) {
|
|
Complex prod;
|
|
|
|
prod.real = (x.real * y.real)-(x.imag * y.imag);
|
|
prod.imag = (x.real * y.imag) + (x.imag * y.real);
|
|
|
|
printf("Produkt: x * y = ");
|
|
printComplex(prod);
|
|
|
|
return prod;
|
|
}
|
|
|
|
/*--------------------------------------------------------------------*\
|
|
* Division komplexer Zahlen: x / y
|
|
* Rueckgabe: Ergebnis im Typ "Complex"; im Fehlerfall 0. bzw. ERROR_C
|
|
\*--------------------------------------------------------------------*/
|
|
Complex divComplex(Complex x, Complex y)
|
|
{
|
|
Complex quotient = {};
|
|
|
|
if(y.real != 0 && y.imag != 0 )
|
|
{
|
|
|
|
quotient.real = (x.real * y.real + x.imag * y.imag) / (y.real * y.real + y.imag * y.imag);
|
|
quotient.imag = (x.imag * y.real - x.real * y.imag) / (y.real * y.real + y.imag * y.imag);
|
|
|
|
printf("Quotient: x / y = ");
|
|
printComplex(quotient);
|
|
|
|
return quotient;
|
|
}
|
|
|
|
return quotient;
|
|
}
|
|
|
|
/*--------------------------------------------------------------------*\
|
|
* Lesbar formatierte Ausgabe einer komplexen Zahl
|
|
\*--------------------------------------------------------------------*/
|
|
int printComplex(Complex z)
|
|
{
|
|
printf("(%.3f + %.3f""i)\n", z.real, z.imag);
|
|
|
|
return 0;
|
|
}
|
|
|