From c1f18f8639c03a0b017b4500917de9c190512ea1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 6 May 2025 21:18:23 +0200 Subject: [PATCH] 28.2.2 --- kommaadd.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) diff --git a/kommaadd.c b/kommaadd.c index 0006424..09a44a9 100644 --- a/kommaadd.c +++ b/kommaadd.c @@ -1,5 +1,124 @@ -#include +/*#include + +typedef struct +{ + unsigned vorkomma; + unsigned nachkomma; +}Komma; + +void additionVor(Komma p){ + int vor = 0; + + vor+= p.vorkomma; +} + +void printKomma(Komma x) +{ + printf("%u,%u", x.vorkomma,x.nachkomma); +}; int main(){ - printf("japanoschlampen") + int vor; + int tmp, nach; + + //printf("Vorkomma: %d\nNachkomma: %f", vorkomma, nachkomma); + + + + printf("Gib Deine Kommazahlen ein (Abschluss mit Leerzeile)"); + scanf("%d", &tmp); + + vor = (int)tmp; + nach = tmp - vor; + + Komma zahl = {vor,nach}; + additionVor(zahl); + printKomma(zahl); + +}*/ + +#include +#include + +#define MAX 100 + +typedef struct { + int vorkomma; + int nachkomma; +} Kommazahl; + +int stringZuInt(char *str) { + int result = 0; + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] >= '0' && str[i] <= '9') { + result = result * 10 + (str[i] - '0'); + } else { + return -1; // Ungültiges Zeichen + } + } + return result; +} + +int nachkommaZuInt(char *str) { + int result = 0; + int i = 0; + for (; str[i] != '\0' && i < 6; i++) { + if (str[i] >= '0' && str[i] <= '9') { + result = result * 10 + (str[i] - '0'); + } else { + return -1; // Ungültiges Zeichen + } + } + for (; i < 6; i++) { + result *= 10; + } + return result; +} + +int main() { + char eingabe[MAX]; + int vor = 0, nach = 0; + Kommazahl zahl; + Kommazahl summe = {0, 0}; + + printf("Gib Deine Kommazahlen ein (Abschluss mit Leerzeile)\n"); + + while (1) { + fgets(eingabe, MAX, stdin); + eingabe[strcspn(eingabe, "\n")] = '\0'; + + if (strlen(eingabe) == 0) break; + + char *kommaPos = strchr(eingabe, ','); + if (kommaPos != NULL) { + *kommaPos = '\0'; + char *nachkommateil = kommaPos + 1; + + zahl.vorkomma = stringZuInt(eingabe); + zahl.nachkomma = nachkommaZuInt(nachkommateil); + } else { + // Nur ganzzahliger Teil + zahl.vorkomma = stringZuInt(eingabe); + zahl.nachkomma = 0; + } + + if (zahl.vorkomma == -1 || zahl.nachkomma == -1) { + printf("Ungültige Zahl: %s\n", eingabe); + continue; + } + + summe.vorkomma += zahl.vorkomma; + summe.nachkomma += zahl.nachkomma; + vor += zahl.vorkomma; + nach += zahl.nachkomma; + if (summe.nachkomma >= 1000000) { + summe.vorkomma += 1; + summe.nachkomma -= 1000000; + } + } + + printf("= %d,%06d (%d, %d) ..... Kontrollwert: %d.%06d\n", + summe.vorkomma, summe.nachkomma, vor, nach, summe.vorkomma, summe.nachkomma); + + return 0; } \ No newline at end of file