diff --git a/adturing.c b/adturing.c new file mode 100644 index 0000000..08ea5ba --- /dev/null +++ b/adturing.c @@ -0,0 +1,23 @@ +#include +#include +#include "turing.h" + +// Simuliert die Turingmaschine zur binären Addition von 1 +void ausfuehren(char *band, int pos) { + int carry = 1; // Wir wollen +1 addieren → also mit Übertrag starten + + while (carry && pos >= 0) { + if (band[pos] == '0') { + band[pos] = '1'; // 0 + 1 = 1 → fertig + carry = 0; + } else if (band[pos] == '1') { + band[pos] = '0'; // 1 + 1 = 0 mit Übertrag → weiter nach links + pos--; + } else if (band[pos] == '#') { + band[pos] = '1'; // Wenn wir am Ende des Bandes sind und Übertrag haben → neue 1 + carry = 0; + } else { + fehler("Ungültiges Zeichen auf dem Band."); // Nur 0, 1 und # sind erlaubt + } + } +} diff --git a/bildsch.c b/bildsch.c new file mode 100644 index 0000000..930f5fe --- /dev/null +++ b/bildsch.c @@ -0,0 +1,13 @@ +#include +#include "turing.h" + +// Zeigt das Band an und markiert die aktuelle Position (mit eckigen Klammern) +void anzeigen(char *band, int pos) { + for (int i = 0; band[i] != '\0'; i++) { + if (i == pos) + printf("[%c]", band[i]); // aktuelle Position + else + printf(" %c ", band[i]); // normal + } + printf("\n"); +} diff --git a/dualaddi.c b/dualaddi.c new file mode 100644 index 0000000..648fce4 --- /dev/null +++ b/dualaddi.c @@ -0,0 +1,20 @@ +#include +#include +#include "turing.h" + +int main() { + // Anfangszustand des Bandes + char band[100] = "1100#"; // Das ist z.B. die Zahl 12 in Binär + int pos = strlen(band) - 2; // Position des letzten Bits vor dem # + + printf("Vorher: %s\n", band); + anzeigen(band, pos); // Zeigt das Band mit Positionsmarkierung + ausfuehren(band, pos); // Führt die Addition durch + printf("Nachher: %s\n", band); + anzeigen(band, pos); // Zeigt das neue Band + + // Zur Ausgabe vom Program folgender Befehlt in die Konsole: gcc -o dualaddi dualaddi.c adturing.c bildsch.c fehler.c + //./dualaddi + + return 0; +} diff --git a/dualaddi.exe b/dualaddi.exe new file mode 100644 index 0000000..6736512 Binary files /dev/null and b/dualaddi.exe differ diff --git a/fehler.c b/fehler.c new file mode 100644 index 0000000..40ad810 --- /dev/null +++ b/fehler.c @@ -0,0 +1,8 @@ +#include +#include + +// Gibt eine Fehlermeldung aus und beendet das Programm +void fehler(char *text) { + printf("FEHLER: %s\n", text); + exit(1); +} diff --git a/turing.h b/turing.h new file mode 100644 index 0000000..96e02a1 --- /dev/null +++ b/turing.h @@ -0,0 +1,14 @@ +#ifndef INFORMATIK2_PR_TURING_H +#define INFORMATIK2_PR_TURING_H + +#endif //INFORMATIK2_PR_TURING_H + +#ifndef TURING_H +#define TURING_H + +// Deklarationen der Funktionen, die in mehreren Dateien verwendet werden +void fehler(char *text); +void anzeigen(char *band, int pos); +void ausfuehren(char *band, int pos); + +#endif \ No newline at end of file