halo
This commit is contained in:
parent
7f99fb7eee
commit
445e541ddc
@ -1,35 +1,56 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Definition einer Struktur zur Speicherung von Zeitangaben
|
||||
struct zeit {
|
||||
int tag;
|
||||
int std;
|
||||
int min;
|
||||
int sek;
|
||||
int tag; // Tage
|
||||
int std; // Stunden
|
||||
int min; // Minuten
|
||||
int sek; // Sekunden
|
||||
};
|
||||
|
||||
// Funktion: Wandelt eine Zeitstruktur in die Gesamtanzahl an Sekunden um
|
||||
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;
|
||||
}
|
||||
|
||||
// Funktion: Wandelt eine Anzahl von Sekunden in eine Zeitstruktur um
|
||||
struct zeit sek_in_zeit(unsigned long sek) {
|
||||
struct zeit z;
|
||||
z.tag = sek / 86400;
|
||||
sek %= 86400;
|
||||
z.std = sek / 3600;
|
||||
sek %= 3600;
|
||||
z.min = sek / 60;
|
||||
z.sek = sek % 60;
|
||||
z.tag = sek / 86400; // Ganze Tage berechnen
|
||||
sek %= 86400; // Restsekunden nach Tagen
|
||||
z.std = sek / 3600; // Ganze Stunden berechnen
|
||||
sek %= 3600; // Restsekunden nach Stunden
|
||||
z.min = sek / 60; // Ganze Minuten berechnen
|
||||
z.sek = sek % 60; // Restsekunden nach Minuten
|
||||
return z;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct zeit z1, z2, summe;
|
||||
unsigned long s1, s2, sgesamt;
|
||||
struct zeit z1, z2, summe; // Zeitstrukturen für die beiden Eingaben und die Summe
|
||||
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): ");
|
||||
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): ");
|
||||
scanf("%d.%d.%d.%d", &z2.tag, &z2.std, &z2.min, &z2.sek);
|
||||
|
||||
// Umwandlung der beiden Zeiten in Sekunden
|
||||
s1 = zeit_in_sek(z1);
|
||||
s2 = zeit_in_sek(z2);
|
||||
|
||||
// Addition der Sekundenwerte
|
||||
sgesamt = s1 + s2;
|
||||
|
||||
// Umwandlung der Gesamtsumme der Sekunden zurück in eine Zeitstruktur
|
||||
summe = sek_in_zeit(sgesamt);
|
||||
|
||||
// Ausgabe des Ergebnisses in Tagen, Stunden, Minuten, Sekunden und Gesamtsekunden
|
||||
printf("= %d Tage, %d:%d:%d; %lu Gesamtsekunden\n",
|
||||
summe.tag, summe.std, summe.min, summe.sek, sgesamt);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,28 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include "complex.h"
|
||||
#include "complex.h" // Eigene Header-Datei für komplexe Zahlen
|
||||
|
||||
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);
|
||||
|
||||
// Eingabe der ersten komplexen Zahl
|
||||
printf("1. Zahl eingeben\nRealteil: ");
|
||||
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 *y = createComplex(re2, im2);
|
||||
|
||||
// Berechnung der Summe, Differenz, Produkt und Quotient
|
||||
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);
|
||||
|
||||
// Ausgabe der Ergebnisse
|
||||
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);
|
||||
|
||||
// Speicher wieder freigeben
|
||||
freeComplex(x);
|
||||
freeComplex(y);
|
||||
freeComplex(sum);
|
||||
freeComplex(diff);
|
||||
freeComplex(prod);
|
||||
freeComplex(quot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//gcc TestComplex.c -L. -lcomplex -o TestComplex
|
||||
// Kompilieren mit:
|
||||
// gcc TestComplex.c -L. -lcomplex -o TestComplex
|
21
complex.c
21
complex.c
@ -3,38 +3,51 @@
|
||||
#include <math.h>
|
||||
#include "complex.h"
|
||||
|
||||
// Erzeugt eine neue komplexe Zahl und gibt einen Zeiger darauf zurück
|
||||
Complex* createComplex(double real, double imag) {
|
||||
Complex* z = malloc(sizeof(Complex));
|
||||
Complex* z = malloc(sizeof(Complex)); // Speicher reservieren
|
||||
if (z != NULL) {
|
||||
z->real = real;
|
||||
z->imag = imag;
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
// Gibt den Speicher einer komplexen Zahl frei
|
||||
void freeComplex(Complex* z) {
|
||||
free(z);
|
||||
}
|
||||
|
||||
// Addiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
|
||||
Complex* addComplex(const Complex* a, const Complex* b) {
|
||||
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) {
|
||||
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) {
|
||||
double real = a->real * b->real - a->imag * b->imag;
|
||||
double imag = a->real * b->imag + a->imag * b->real;
|
||||
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) {
|
||||
double denom = b->real * b->real + b->imag * b->imag;
|
||||
if (denom == 0.0) return NULL;
|
||||
double denom = b->real * b->real + b->imag * b->imag; // Nenner berechnen
|
||||
if (denom == 0.0) return NULL; // Division durch 0 abfangen
|
||||
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);
|
||||
}
|
||||
|
||||
// Gibt eine komplexe Zahl in der Form (a + bi) aus
|
||||
void printComplex(const Complex* z) {
|
||||
if (z)
|
||||
printf("(%.3f + %.3fi)\n", z->real, z->imag);
|
||||
else
|
||||
printf("NULL (division durch 0?)\n");
|
||||
printf("NULL -> Division durch 0 nicht möglich\n");
|
||||
}
|
23
complex.h
23
complex.h
@ -1,14 +1,19 @@
|
||||
#ifndef COMPLEX_H
|
||||
#define COMPLEX_H
|
||||
|
||||
// Definition der Complex-Struktur für komplexe Zahlen
|
||||
typedef struct {
|
||||
double real;
|
||||
double imag;
|
||||
double real; // Realteil
|
||||
double imag; // Imaginärteil
|
||||
} 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);
|
||||
|
||||
// Funktionsprototypen für Operationen mit komplexen Zahlen
|
||||
Complex* createComplex(double real, double imag); // Erzeugt eine neue komplexe Zahl
|
||||
void freeComplex(Complex* z); // Gibt den Speicher einer komplexen Zahl frei
|
||||
Complex* addComplex(const Complex* a, const Complex* b); // Addition
|
||||
Complex* subtractComplex(const Complex* a, const Complex* b); // Subtraktion
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user