Compare commits
3 Commits
437d5a9b2e
...
8705b7b629
Author | SHA1 | Date | |
---|---|---|---|
8705b7b629 | |||
3168349326 | |||
da74a8a87e |
115
datediff.c
Normal file
115
datediff.c
Normal file
@ -0,0 +1,115 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define SIZE_EINGABE 11
|
||||
|
||||
typedef struct {
|
||||
int tag;
|
||||
int monat;
|
||||
int jahr;
|
||||
} Datum;
|
||||
|
||||
|
||||
int datum_einlesen(Datum *datum, int i) {
|
||||
char eingabe[SIZE_EINGABE] = {0};
|
||||
printf("%d. Datum (tt.mm.jjjj): ", i);
|
||||
|
||||
if (fgets(eingabe, SIZE_EINGABE, stdin) != NULL) {
|
||||
|
||||
int laenge = strlen(eingabe);
|
||||
if (eingabe[laenge - 1] == '\n') {
|
||||
eingabe[laenge - 1] = '\0';
|
||||
}
|
||||
|
||||
if (sscanf(eingabe, "%d.%d.%d", &datum->tag, &datum->monat, &datum->jahr) == 3) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int schaltjahr_berechnen(int jahr) {
|
||||
return (jahr % 4 == 0 && (jahr % 100 != 0 || jahr % 400 == 0)) ? 1 : 0;
|
||||
}
|
||||
|
||||
int alle_tage_bis_datum_berechnen(Datum *datum) {
|
||||
int aktuelles_datum_tage = 0;
|
||||
const int monate_kein_schaltjahr[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
const int monate_schaltjahr[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
|
||||
for(int i = 0; i < datum->jahr; i++){
|
||||
int schaltjahr = schaltjahr_berechnen(i);
|
||||
if(schaltjahr == 1) {
|
||||
aktuelles_datum_tage += 366;
|
||||
}
|
||||
else if(schaltjahr == 0) {
|
||||
aktuelles_datum_tage += 365;
|
||||
}
|
||||
}
|
||||
for(int j = 0; j < (datum->monat) - 1 ; j++) {
|
||||
int schaltjahr = schaltjahr_berechnen(datum->jahr);
|
||||
if(schaltjahr == 1) {
|
||||
aktuelles_datum_tage += monate_schaltjahr[j];
|
||||
}
|
||||
else if(schaltjahr == 0) {
|
||||
aktuelles_datum_tage += monate_kein_schaltjahr[j];
|
||||
}
|
||||
}
|
||||
for(int k = 0; k < datum->tag; k++) {
|
||||
aktuelles_datum_tage++;
|
||||
}
|
||||
return aktuelles_datum_tage;
|
||||
}
|
||||
|
||||
|
||||
int tage_diff(Datum *datum_1, Datum *datum_2) {
|
||||
|
||||
int tage_diff_gesamt = 0;
|
||||
|
||||
int tage_datum_1 = alle_tage_bis_datum_berechnen(datum_1);
|
||||
int tage_datum_2 = alle_tage_bis_datum_berechnen(datum_2);
|
||||
|
||||
tage_diff_gesamt = abs(tage_datum_1 - tage_datum_2);
|
||||
|
||||
return tage_diff_gesamt;
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
Datum datum_1, datum_2;
|
||||
|
||||
|
||||
if (!datum_einlesen(&datum_1, 1)) {
|
||||
printf("Fehler beim Einlesen von Datum 1.\n");
|
||||
}
|
||||
|
||||
fflush(stdin);
|
||||
|
||||
if (!datum_einlesen(&datum_2, 2)) {
|
||||
printf("Fehler beim Einlesen von Datum 2.\n");
|
||||
}
|
||||
|
||||
int tage_diff_aus = tage_diff(&datum_1, &datum_2); {
|
||||
|
||||
}
|
||||
|
||||
printf("..... Differenz: %d Tage .....", tage_diff_aus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Created by Lennart Pecher on 16.05.25.
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// Created by Lennart Pecher on 16.05.25.
|
||||
//
|
Loading…
x
Reference in New Issue
Block a user