23
This commit is contained in:
parent
6a0d1b94ca
commit
51a2aa9776
23
adturing.c
Normal file
23
adturing.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
bildsch.c
Normal file
13
bildsch.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#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");
|
||||||
|
}
|
20
dualaddi.c
Normal file
20
dualaddi.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#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;
|
||||||
|
}
|
BIN
dualaddi.exe
Normal file
BIN
dualaddi.exe
Normal file
Binary file not shown.
8
fehler.c
Normal file
8
fehler.c
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Gibt eine Fehlermeldung aus und beendet das Programm
|
||||||
|
void fehler(char *text) {
|
||||||
|
printf("FEHLER: %s\n", text);
|
||||||
|
exit(1);
|
||||||
|
}
|
14
turing.h
Normal file
14
turing.h
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user