From dbe7ecfd43e29a5dbd617131d83580cf012e2c2d Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 5 Jun 2025 18:05:06 +0200 Subject: [PATCH] 26 --- zahlsys.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 zahlsys.c diff --git a/zahlsys.c b/zahlsys.c new file mode 100644 index 0000000..b468409 --- /dev/null +++ b/zahlsys.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +void ausgabeBinaer(int wert) { + for (int i = 31; i >= 0; i--) { + int bit = (wert >> i) & 1; + printf("%d", bit); + + if (i % 8 == 0 && i != 0) + printf(" "); + } + printf("\n"); +} + +int main() +{ + char eingabe[100] = {"--bo 127 --h"}; + + char *pptr, *nptr; + int wert; + bool b = false, o = false, h = false; + do{ + if(strchr(eingabe, ' ') == NULL) + break; + nptr = strchr(eingabe, ' ') + 1; + pptr = strtok(eingabe, " "); + if(pptr[0] == '-' && pptr[1]) + for (int i = 0; pptr[i]; ++i) { + switch (pptr[i]) { + case 'b': + b = true; + break; + case 'o': + o = true; + break; + case 'h': + h = true; + break; + } + } + else if(pptr[0] >= '0' && pptr[0] <= '9') + wert = atoi(pptr); + strcpy(eingabe, nptr); + } while (nptr != NULL); + + printf("Dezimal: %d\n", wert); + if(b){ + printf("Dual: "); + ausgabeBinaer(wert); + } + if(o) + printf("Oktal: %o\n", wert); + if(h) + printf("Hexadezimal: %x\n", wert); + if(b == 0 && o == 0 && h == 0){ + printf("Dual: "); + ausgabeBinaer(wert); + printf("Oktal: %o\n", wert); + printf("Hexadezimal: %x\n", wert); + } + +} \ No newline at end of file