This commit is contained in:
Daniel Zwanzig 2025-05-12 18:15:52 +02:00
parent 317225170d
commit 746742c044
3 changed files with 211 additions and 0 deletions

52
TestComplex.c Normal file
View File

@ -0,0 +1,52 @@
/**********************************************************************\
* Kurzbeschreibung:
* "Funktionstest" - 2 komplexe Zahlen einlesen
* und alle implementierten Funktionen aus complex.c zum Test aufrufen
*
* Datum: Autor:
*
*
\**********************************************************************/
/*--- #includes ------------------------------------------------------*/
#define _CRT_SECURE_NO_WARNINGS //VC++: keine scanf() Warnungen
#include <stdio.h>
#include "complex.h"
/*--- #defines -------------------------------------------------------*/
/*--- Lokale Datentypen (typedef) ------------------------------------*/
/*--- Modulglobale static Variablen ----------------------------------*/
/*--- Prototypen lokaler Funktionen ----------------------------------*/
/*--- Funktionsdefinitionen ------------------------------------------*/
/*--- main -----------------------------------------------------------*/
int main(void)
{
double real = 0, imag = 0;
printf("1. Zahl eingeben\nRealteil: ");
scanf("%lf", &real);
printf("Imaginaerteil:");
scanf("%lf", &imag);
Complex zahl1 = {real, imag};
printf("2. Zahl eingeben\nRealteil: ");
scanf("%lf", &real);
printf("Imaginaerteil:");
scanf("%lf", &imag);
Complex zahl2 = {real, imag};
printf("\nx = (%.3lf + %.3lf""i)", zahl1.real, zahl1.imag);
printf("\ny = (%.3lf + %.3lf""i)\n\n", zahl2.real, zahl2.imag);
addComplex(zahl1 ,zahl2);
subComplex(zahl1 ,zahl2);
mulComplex(zahl1 ,zahl2);
divComplex(zahl1 ,zahl2);
}

103
complex.c Normal file
View File

@ -0,0 +1,103 @@
/**********************************************************************\
* 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;
}
}
/*--------------------------------------------------------------------*\
* Lesbar formatierte Ausgabe einer komplexen Zahl
\*--------------------------------------------------------------------*/
int printComplex(Complex z)
{
printf("(%.3f + %.3f""i)\n", z.real, z.imag);
return 0;
}

56
complex.h Normal file
View File

@ -0,0 +1,56 @@
/**********************************************************************\
* Kurzbeschreibung: complex.h
* Bietet Funktionen zum Rechnen mit komplexen Zahlen
*
* Datum: Autor: Grund der Aenderung:
* 01.10.2015 Dr. Lurz Neuerstellung
*
\**********************************************************************/
#ifndef COMPLEX_H
#define COMPLEX_H
/*--- #includes ------------------------------------------------------*/
/*--- #defines -------------------------------------------------------*/
#define ERROR_C 0.
/*--- Datentypen (typedef) -------------------------------------------*/
typedef struct t_complex {
double real;
double imag;
} Complex;
/*--- Prototypen globaler Funktionen ---------------------------------*/
/*--------------------------------------------------------------------*\
* Addition komplexer Zahlen: x + y
* Rueckgabe: Ergebnis im Typ "Complex"
\*--------------------------------------------------------------------*/
Complex addComplex(Complex x, Complex y);
/*--------------------------------------------------------------------*\
* Subtraktion komplexer Zahlen: x - y
* Rueckgabe: Ergebnis im Typ "Complex"
\*--------------------------------------------------------------------*/
Complex subComplex(Complex x, Complex y);
/*--------------------------------------------------------------------*\
* Multiplikation komplexer Zahlen: x * y
* Rueckgabe: Ergebnis im Typ "Complex"
\*--------------------------------------------------------------------*/
Complex mulComplex(Complex x, Complex y);
/*--------------------------------------------------------------------*\
* Division komplexer Zahlen: x / y
* Rueckgabe: Ergebnis im Typ "Complex"; im Fehlerfall 0. bzw. ERROR_C
\*--------------------------------------------------------------------*/
Complex divComplex(Complex x, Complex y);
/*--------------------------------------------------------------------*\
* Lesbar formatierte Ausgabe einer komplexen Zahl
\*--------------------------------------------------------------------*/
int printComplex(Complex z);
#endif /*COMPLEX_H*/