28.2.2
This commit is contained in:
parent
ac4d2cbb64
commit
c1f18f8639
123
kommaadd.c
123
kommaadd.c
@ -1,5 +1,124 @@
|
||||
#include <stdio.h>
|
||||
/*#include <stdio.h>
|
||||
|
||||
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 <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user