This commit is contained in:
Bora Zuenbuelkoek 2025-05-23 07:31:40 +02:00
parent 7f99fb7eee
commit 445e541ddc
4 changed files with 106 additions and 40 deletions

View File

@ -1,35 +1,56 @@
#include <stdio.h> #include <stdio.h>
// Definition einer Struktur zur Speicherung von Zeitangaben
struct zeit { struct zeit {
int tag; int tag; // Tage
int std; int std; // Stunden
int min; int min; // Minuten
int sek; int sek; // Sekunden
}; };
// Funktion: Wandelt eine Zeitstruktur in die Gesamtanzahl an Sekunden um
unsigned long zeit_in_sek(struct zeit z) { unsigned long zeit_in_sek(struct zeit z) {
// 1 Tag = 86400 Sekunden, 1 Stunde = 3600 Sekunden, 1 Minute = 60 Sekunden
return z.tag * 86400 + z.std * 3600 + z.min * 60 + z.sek; return z.tag * 86400 + z.std * 3600 + z.min * 60 + z.sek;
} }
// Funktion: Wandelt eine Anzahl von Sekunden in eine Zeitstruktur um
struct zeit sek_in_zeit(unsigned long sek) { struct zeit sek_in_zeit(unsigned long sek) {
struct zeit z; struct zeit z;
z.tag = sek / 86400; z.tag = sek / 86400; // Ganze Tage berechnen
sek %= 86400; sek %= 86400; // Restsekunden nach Tagen
z.std = sek / 3600; z.std = sek / 3600; // Ganze Stunden berechnen
sek %= 3600; sek %= 3600; // Restsekunden nach Stunden
z.min = sek / 60; z.min = sek / 60; // Ganze Minuten berechnen
z.sek = sek % 60; z.sek = sek % 60; // Restsekunden nach Minuten
return z; return z;
} }
int main() { int main() {
struct zeit z1, z2, summe; struct zeit z1, z2, summe; // Zeitstrukturen für die beiden Eingaben und die Summe
unsigned long s1, s2, sgesamt; unsigned long s1, s2, sgesamt; // Variablen für die Sekundenwerte
// Eingabe der ersten Zeit im Format tt.hh.mm.ss
printf("Gib 1. Zeit ein (tt.hh.mm.ss): "); printf("Gib 1. Zeit ein (tt.hh.mm.ss): ");
scanf("%d.%d.%d.%d", &z1.tag, &z1.std, &z1.min, &z1.sek); scanf("%d.%d.%d.%d", &z1.tag, &z1.std, &z1.min, &z1.sek);
// Eingabe der zweiten Zeit im gleichen Format
printf("Gib 2. Zeit ein (tt.hh.mm.ss): "); printf("Gib 2. Zeit ein (tt.hh.mm.ss): ");
scanf("%d.%d.%d.%d", &z2.tag, &z2.std, &z2.min, &z2.sek); scanf("%d.%d.%d.%d", &z2.tag, &z2.std, &z2.min, &z2.sek);
// Umwandlung der beiden Zeiten in Sekunden
s1 = zeit_in_sek(z1); s1 = zeit_in_sek(z1);
s2 = zeit_in_sek(z2); s2 = zeit_in_sek(z2);
// Addition der Sekundenwerte
sgesamt = s1 + s2; sgesamt = s1 + s2;
// Umwandlung der Gesamtsumme der Sekunden zurück in eine Zeitstruktur
summe = sek_in_zeit(sgesamt); summe = sek_in_zeit(sgesamt);
// Ausgabe des Ergebnisses in Tagen, Stunden, Minuten, Sekunden und Gesamtsekunden
printf("= %d Tage, %d:%d:%d; %lu Gesamtsekunden\n", printf("= %d Tage, %d:%d:%d; %lu Gesamtsekunden\n",
summe.tag, summe.std, summe.min, summe.sek, sgesamt); summe.tag, summe.std, summe.min, summe.sek, sgesamt);
return 0; return 0;
} }

View File

@ -1,28 +1,55 @@
#include <stdio.h> #include <stdio.h>
#include "complex.h" #include "complex.h" // Eigene Header-Datei für komplexe Zahlen
int main() { int main() {
double re1, im1, re2, im2; double re1, im1, re2, im2;
printf("1. Zahl eingeben\nRealteil: "); scanf("%lf", &re1);
printf("Imaginärteil: "); scanf("%lf", &im1); // Eingabe der ersten komplexen Zahl
printf("2. Zahl eingeben\nRealteil: "); scanf("%lf", &re2); printf("1. Zahl eingeben\nRealteil: ");
printf("Imaginärteil: "); scanf("%lf", &im2); scanf("%lf", &re1);
printf("Imaginärteil: ");
scanf("%lf", &im1);
// Eingabe der zweiten komplexen Zahl
printf("2. Zahl eingeben\nRealteil: ");
scanf("%lf", &re2);
printf("Imaginärteil: ");
scanf("%lf", &im2);
// Erzeugen der komplexen Zahlen als Zeiger auf Complex-Strukturen
Complex *x = createComplex(re1, im1); Complex *x = createComplex(re1, im1);
Complex *y = createComplex(re2, im2); Complex *y = createComplex(re2, im2);
// Berechnung der Summe, Differenz, Produkt und Quotient
Complex *sum = addComplex(x, y); Complex *sum = addComplex(x, y);
Complex *diff = subtractComplex(x, y); Complex *diff = subtractComplex(x, y);
Complex *prod = multiplyComplex(x, y); Complex *prod = multiplyComplex(x, y);
Complex *quot = divideComplex(x, y); Complex *quot = divideComplex(x, y);
printf("x = "); printComplex(x);
printf("y = "); printComplex(y); // Ausgabe der Ergebnisse
printf("Summe: x + y = "); printComplex(sum); printf("x = ");
printf("Differenz: x - y = "); printComplex(diff); printComplex(x);
printf("Produkt: x * y = "); printComplex(prod); printf("y = ");
printf("Quotient: x / y = "); printComplex(quot); printComplex(y);
freeComplex(x); freeComplex(y); printf("Summe: x + y = ");
freeComplex(sum); freeComplex(diff); printComplex(sum);
freeComplex(prod); freeComplex(quot); printf("Differenz: x - y = ");
printComplex(diff);
printf("Produkt: x * y = ");
printComplex(prod);
printf("Quotient: x / y = ");
printComplex(quot);
// Speicher wieder freigeben
freeComplex(x);
freeComplex(y);
freeComplex(sum);
freeComplex(diff);
freeComplex(prod);
freeComplex(quot);
return 0; return 0;
} }
//gcc TestComplex.c -L. -lcomplex -o TestComplex // Kompilieren mit:
// gcc TestComplex.c -L. -lcomplex -o TestComplex

View File

@ -3,38 +3,51 @@
#include <math.h> #include <math.h>
#include "complex.h" #include "complex.h"
// Erzeugt eine neue komplexe Zahl und gibt einen Zeiger darauf zurück
Complex* createComplex(double real, double imag) { Complex* createComplex(double real, double imag) {
Complex* z = malloc(sizeof(Complex)); Complex* z = malloc(sizeof(Complex)); // Speicher reservieren
if (z != NULL) { if (z != NULL) {
z->real = real; z->real = real;
z->imag = imag; z->imag = imag;
} }
return z; return z;
} }
// Gibt den Speicher einer komplexen Zahl frei
void freeComplex(Complex* z) { void freeComplex(Complex* z) {
free(z); free(z);
} }
// Addiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
Complex* addComplex(const Complex* a, const Complex* b) { Complex* addComplex(const Complex* a, const Complex* b) {
return createComplex(a->real + b->real, a->imag + b->imag); return createComplex(a->real + b->real, a->imag + b->imag);
} }
// Subtrahiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
Complex* subtractComplex(const Complex* a, const Complex* b) { Complex* subtractComplex(const Complex* a, const Complex* b) {
return createComplex(a->real - b->real, a->imag - b->imag); return createComplex(a->real - b->real, a->imag - b->imag);
} }
// Multipliziert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
Complex* multiplyComplex(const Complex* a, const Complex* b) { Complex* multiplyComplex(const Complex* a, const Complex* b) {
double real = a->real * b->real - a->imag * b->imag; double real = a->real * b->real - a->imag * b->imag;
double imag = a->real * b->imag + a->imag * b->real; double imag = a->real * b->imag + a->imag * b->real;
return createComplex(real, imag); return createComplex(real, imag);
} }
// Dividiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
Complex* divideComplex(const Complex* a, const Complex* b) { Complex* divideComplex(const Complex* a, const Complex* b) {
double denom = b->real * b->real + b->imag * b->imag; double denom = b->real * b->real + b->imag * b->imag; // Nenner berechnen
if (denom == 0.0) return NULL; if (denom == 0.0) return NULL; // Division durch 0 abfangen
double real = (a->real * b->real + a->imag * b->imag) / denom; double real = (a->real * b->real + a->imag * b->imag) / denom;
double imag = (a->imag * b->real - a->real * b->imag) / denom; double imag = (a->imag * b->real - a->real * b->imag) / denom;
return createComplex(real, imag); return createComplex(real, imag);
} }
// Gibt eine komplexe Zahl in der Form (a + bi) aus
void printComplex(const Complex* z) { void printComplex(const Complex* z) {
if (z) if (z)
printf("(%.3f + %.3fi)\n", z->real, z->imag); printf("(%.3f + %.3fi)\n", z->real, z->imag);
else else
printf("NULL (division durch 0?)\n"); printf("NULL -> Division durch 0 nicht möglich\n");
} }

View File

@ -1,14 +1,19 @@
#ifndef COMPLEX_H #ifndef COMPLEX_H
#define COMPLEX_H #define COMPLEX_H
// Definition der Complex-Struktur für komplexe Zahlen
typedef struct { typedef struct {
double real; double real; // Realteil
double imag; double imag; // Imaginärteil
} Complex; } Complex;
Complex* createComplex(double real, double imag);
void freeComplex(Complex* z); // Funktionsprototypen für Operationen mit komplexen Zahlen
Complex* addComplex(const Complex* a, const Complex* b); Complex* createComplex(double real, double imag); // Erzeugt eine neue komplexe Zahl
Complex* subtractComplex(const Complex* a, const Complex* b); void freeComplex(Complex* z); // Gibt den Speicher einer komplexen Zahl frei
Complex* multiplyComplex(const Complex* a, const Complex* b); Complex* addComplex(const Complex* a, const Complex* b); // Addition
Complex* divideComplex(const Complex* a, const Complex* b); Complex* subtractComplex(const Complex* a, const Complex* b); // Subtraktion
void printComplex(const Complex* z); Complex* multiplyComplex(const Complex* a, const Complex* b); // Multiplikation
Complex* divideComplex(const Complex* a, const Complex* b); // Division
void printComplex(const Complex* z); // Ausgabe
#endif #endif