This commit is contained in:
Bora Zuenbuelkoek 2025-05-05 15:59:38 +02:00
parent ebc9ac45db
commit 97d3228fcf
4 changed files with 41 additions and 37 deletions

View File

@ -1,35 +1,36 @@
#include <string.h> #include <string.h>
// Addiert 1 zu einer Dualzahl (als String) und speichert das Ergebnis in output
void dual_addiere_eins(const char *input, char *output, int maxlen) { void dual_addiere_eins(const char *input, char *output, int maxlen) {
int len = strlen(input); int len = strlen(input); // Länge der Eingabe bestimmen
int carry = 1; int carry = 1; // Übertrag (wir addieren 1, daher am Anfang 1)
int i, j; int i, j; // Schleifenvariablen
// Das Ergebnis wird rückwärts in ein temporäres Array geschrieben char temp[maxlen]; // Temporäres Array für das Ergebnis (rückwärts)
char temp[maxlen]; temp[maxlen-1] = '\0'; // String-Ende setzen
temp[maxlen-1] = '\0'; // String-Ende
// Von hinten (letztes Zeichen) nach vorne durchgehen
for (i = len - 1, j = maxlen - 2; i >= 0 && j >= 0; --i, --j) { for (i = len - 1, j = maxlen - 2; i >= 0 && j >= 0; --i, --j) {
if (input[i] == '1') { if (input[i] == '1') { // Wenn aktuelle Ziffer 1 ist
temp[j] = carry ? '0' : '1'; temp[j] = carry ? '0' : '1'; // Mit Übertrag: 1+1=0 (Übertrag bleibt), sonst 1
if (carry) carry = 1; // bleibt 1 // carry bleibt 1, wenn carry==1 und input[i]=='1'
} else if (input[i] == '0') { } else if (input[i] == '0') { // Wenn aktuelle Ziffer 0 ist
temp[j] = carry ? '1' : '0'; temp[j] = carry ? '1' : '0'; // Mit Übertrag: 0+1=1 (Übertrag wird 0), sonst 0
carry = 0; carry = 0; // Übertrag ist jetzt weg
} else { } else {
output[0] = '\0'; // Fehlerfall output[0] = '\0'; // Fehlerfall: ungültiges Zeichen
return; return;
} }
} }
// Falls noch Übertrag übrig ist // Falls nach der Schleife noch ein Übertrag übrig ist (z.B. 111 + 1 = 1000)
if (carry && j >= 0) { if (carry && j >= 0) {
temp[j--] = '1'; temp[j--] = '1'; // Setze noch eine 1 ganz vorne
} }
// Rest auffüllen // Rest mit Nullen auffüllen (falls nötig)
while (j >= 0) temp[j--] = '0'; while (j >= 0) temp[j--] = '0';
// Jetzt führende Nullen entfernen // Führende Nullen entfernen (außer die letzte Ziffer)
int start = 0; int start = 0;
while (temp[start] == '0' && temp[start+1] != '\0') ++start; while (temp[start] == '0' && temp[start+1] != '\0') ++start;
strcpy(output, temp + start); strcpy(output, temp + start); // Ergebnis in output kopieren
} }}

View File

@ -1,16 +1,18 @@
#include <stdio.h> #include <stdio.h>
// Liest eine Dualzahl als Zeichenkette vom Benutzer ein
void dualzahl_einlesen(char *buffer, int maxlen) { void dualzahl_einlesen(char *buffer, int maxlen) {
printf("Geben Sie eine Dualzahl ein: "); printf("Geben Sie eine Dualzahl ein: "); // Benutzer auffordern
if (fgets(buffer, maxlen, stdin) == NULL) { if (fgets(buffer, maxlen, stdin) == NULL) { // Zeichenkette einlesen
buffer[0] = '\0'; buffer[0] = '\0'; // Falls Fehler: leeren String setzen
return; return;
} }
// Zeilenumbruch entfernen // Zeilenumbruch am Ende entfernen, falls vorhanden
for (int i = 0; buffer[i]; ++i) for (int i = 0; buffer[i]; ++i)
if (buffer[i] == '\n') buffer[i] = '\0'; if (buffer[i] == '\n') buffer[i] = '\0';
} }
// Gibt die Dualzahl aus
void dualzahl_ausgeben(const char *buffer) { void dualzahl_ausgeben(const char *buffer) {
printf("Ergebnis: %s\n", buffer); printf("Ergebnis: %s\n", buffer); // Ergebnis anzeigen
} }

View File

@ -1,34 +1,34 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// Funktionsprototypen // Funktionsprototypen (damit der Compiler weiß, dass es sie gibt)
void fehlerausgabe(const char *msg); void fehlerausgabe(const char *msg);
void dualzahl_einlesen(char *buffer, int maxlen); void dualzahl_einlesen(char *buffer, int maxlen);
void dualzahl_ausgeben(const char *buffer); void dualzahl_ausgeben(const char *buffer);
void dual_addiere_eins(const char *input, char *output, int maxlen); void dual_addiere_eins(const char *input, char *output, int maxlen);
#define MAXLEN 65 #define MAXLEN 65 // Maximale Länge der Dualzahl (64 Bit + 1 für \0)
int main() { int main() {
char input[MAXLEN]; char input[MAXLEN]; // Eingabepuffer für die Dualzahl
char output[MAXLEN]; char output[MAXLEN]; // Ausgabepuffer für das Ergebnis
dualzahl_einlesen(input, MAXLEN); dualzahl_einlesen(input, MAXLEN); // Dualzahl vom Benutzer einlesen
// Eingabe prüfen // Prüfen, ob die Eingabe nur aus 0 und 1 besteht
for (int i = 0; input[i]; ++i) { for (int i = 0; input[i]; ++i) {
if (input[i] != '0' && input[i] != '1') { if (input[i] != '0' && input[i] != '1') {
fehlerausgabe("Nur 0 und 1 erlaubt!"); fehlerausgabe("Nur 0 und 1 erlaubt!"); // Fehler ausgeben und beenden
} }
} }
dual_addiere_eins(input, output, MAXLEN); dual_addiere_eins(input, output, MAXLEN); // 1 zur Dualzahl addieren
if (output[0] == '\0') { if (output[0] == '\0') { // Falls Fehler bei der Addition
fehlerausgabe("Fehler bei der Addition!"); fehlerausgabe("Fehler bei der Addition!");
} }
dualzahl_ausgeben(output); dualzahl_ausgeben(output); // Ergebnis ausgeben
return 0; return 0; // Programm erfolgreich beenden
} }

View File

@ -1,7 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// Gibt eine Fehlermeldung aus und beendet das Programm
void fehlerausgabe(const char *msg) { void fehlerausgabe(const char *msg) {
fprintf(stderr, "Fehler: %s\n", msg); fprintf(stderr, "Fehler: %s\n", msg); // Fehlermeldung auf dem Fehlerkanal ausgeben
exit(1); exit(1); // Programm mit Fehlercode beenden
} }