This commit is contained in:
Daniel Zwanzig 2025-06-09 15:19:02 +02:00
parent dbe7ecfd43
commit 38fbd0d2a8

View File

@ -7,26 +7,32 @@ void ausgabeBinaer(int wert) {
for (int i = 31; i >= 0; i--) { for (int i = 31; i >= 0; i--) {
int bit = (wert >> i) & 1; int bit = (wert >> i) & 1;
printf("%d", bit); printf("%d", bit);
if (i % 8 == 0 && i != 0) if (i % 8 == 0 && i != 0)
printf(" "); printf(" ");
} }
printf("\n"); printf("\n");
} }
int main() int main(int argc, char *argv[])
{ {
char eingabe[100] = {"--bo 127 --h"}; char eingabe[100] = {0};
for (int i = 1; i < argc; i++) {
strcat(eingabe, argv[i]);
if (i < argc - 1)
strcat(eingabe, " ");
}
char *pptr, *nptr; char *pptr, *nptr;
int wert; char fehlerChar;
bool b = false, o = false, h = false; int wert, fehlerWert = 0;
do{ bool b = false, o = false, h = false, fehler = false;
if(strchr(eingabe, ' ') == NULL) do {
if (!(strpbrk(eingabe, " -123456789")))
break; break;
nptr = strchr(eingabe, ' ') + 1; nptr = strchr(eingabe, ' ') + 1;
pptr = strtok(eingabe, " "); pptr = strtok(eingabe, " ");
if(pptr[0] == '-' && pptr[1]) if (pptr[0] == '-' && pptr[1] == '-')
for (int i = 0; pptr[i]; ++i) { for (int i = 0; pptr[i]; ++i) {
switch (pptr[i]) { switch (pptr[i]) {
case 'b': case 'b':
@ -38,27 +44,45 @@ int main()
case 'h': case 'h':
h = true; h = true;
break; break;
case '-':
break;
default:
fehlerChar = pptr[i];
fehler = true;
break;
} }
} }
else if(pptr[0] >= '0' && pptr[0] <= '9') else if ((pptr[0] >= '0' && pptr[0] <= '9') || (pptr[0] == '-' && pptr[1] != '-')) {
wert = atoi(pptr); if (fehlerWert == 0) {
strcpy(eingabe, nptr); wert = atoi(pptr);
} while (nptr != NULL); fehlerWert++;
} else
fehler = true;
}
if (nptr != NULL + 1)
strcpy(eingabe, nptr);
} while (nptr != NULL + 1);
printf("Dezimal: %d\n", wert); if (fehler == false) {
if(b){ printf("Dezimal: %d\n", wert);
printf("Dual: "); if (b) {
ausgabeBinaer(wert); 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);
}
} else if (fehler == true) {
if (fehlerChar)
printf("...... Unerlaubte Option --%c\n", fehlerChar);
if (fehlerWert == 1)
printf("usage: zahlsys [--boh] zahl\n...... Es muss genau eine Zahl angegeben sein\n");
} }
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);
}
}