Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct mconfig {
- struct mconfig** next;
- char** behaviour;
- int* behavlengths;
- } mconfig;
- /*
- Legacy function, doesn't work right now.
- */
- mconfig* createmconfig(mconfig** next, char** behaviour, int behavlengths[]) {
- mconfig* mcc = malloc(sizeof(mconfig));
- mcc->behaviour = behaviour;
- mcc->next = next;
- mcc->behavlengths = behavlengths;
- return mcc;
- }
- void getAlpha(char **a) { //Gotta do this on linux...
- //char test[5];
- //printf("Which Symbols do you need?\n");
- //scanf("%ms", a);
- //printf("%s\n", *a);
- //strcpy(a, test);
- /*
- test[0] = 'a';
- test[1] = 'a';
- test[2] = 'a';
- test[3] = 'a';
- test[4] = '\0';
- */
- char *t = "e1x ";
- *a = t;
- }
- void getmconfigs(mconfig** mcs) { //do this on linux...
- printf("How many mconfigs do you need?\n");
- int mcsum = 0;
- scanf("%d", &mcsum);
- mconfig mcarray[mcsum];
- for (int a = 0; a < mcsum; a++) {
- printf("%d", a);
- }
- printf("\n");
- }
- /*
- @param ptr pointer to the initial block of memory that will be moved
- @param l length of the block of memory pointed to by ptr that will be moved
- More momory-efficient implementation of memmove(), specifically for this program. Moves the block of memory pointed to by ptr one byte to the left.
- */
- void memspace(char *ptr, int l) {
- for (int a = l; a > 0; a--) {
- memcpy(ptr + a, ptr + a - 1, 1);
- }
- }
- /*
- @param ptr pointer to the block of memory (char-array/tape)
- @param l length of the block of memory (char-array/tape)
- @param p pointer to the integer storing the current position of the turing machine on the tape
- @param chr character to be written in the new segment
- Function to extend the char-array/tape exactly one segment to the right.
- */
- void extendRight(char *ptr, int* l, int* p, char chr) {
- realloc(ptr, (*l + 1) * sizeof(char));
- memcpy(ptr + *l, &chr, 1);
- *l += 1;
- *p += 1;
- }
- /*
- @param ptr pointer to the block of memory (char-array/tape)
- @param l length of the block of memory (char-array/tape)
- @param p pointer to the integer storing the current position of the turing machine on the tape
- @param chr character to be written in the new segment
- Function to extend the char-array/tape exactly one segment to the left.
- */
- void extendLeft(char *ptr, int* l, int* p, char chr) {
- realloc(ptr, (*l + 1) * sizeof(char));
- //memmove(ptr + 1, ptr, *l);
- memspace(ptr, *l + 1);
- memcpy(ptr, &chr, 1);
- *l += 1;
- *p = 0;
- }
- /*
- @param ptr pointer to the block of memory (char-array/tape)
- @param l length of the block of memory (char-array/tape)
- Function to print the whole char-array/tape to the command line.
- */
- void printTape(char* ptr, int l) {
- char *p = ptr;
- for (int a = 0; a < l; a++) {
- printf("%c", *p);
- p++;
- }
- printf("\n");
- }
- /*
- @param ptr pointer to the block of memory (char-array/tape)
- @param l length of the block of memory (char-array/tape)
- @param p pointer to the integer storing the current position of the turing machine on the tape
- Moves the current position of the turing machine on the tape one segment to the left. If the previous position was on the left end of the tape, the tape will be extended by one segment to the left.
- */
- void moveLeft(char* ptr, int* l, int* p) {
- if (*p == 0) {
- extendLeft(ptr, l, p, ' ');
- }
- else {
- *p -= 1;
- }
- }
- /*
- @param ptr pointer to the block of memory (char-array/tape)
- @param l length of the block of memory (char-array/tape)
- @param p pointer to the integer storing the current position of the turing machine on the tape
- Moves the current position of the turing machine on the tape one segment to the right. If the previous position was on the right end of the tape, the tape will be extended by one segment to the right.
- */
- void moveRight(char* ptr, int* l, int* p) {
- if (*p == (*l - 1)) {
- extendRight(ptr, l, p, ' ');
- }
- else {
- *p += 1;
- }
- }
- /*
- @param ptr pointer to the block of memory (char-array/tape)
- @param l length of the block of memory (char-array/tape)
- @param p pointer to the integer storing the current position of the turing machine on the tape
- @param mc the starting mconfiguration/state for the machine
- Starts the turing machine with mconfiguration/state mc, tape ptr with length l, and starting position p.
- */
- void apply(char* ptr, int* l, int* p, char* alpha, int symbolsum, mconfig mc) {
- mconfig *next = NULL;
- printf("Apply start\n");
- do {
- for (int a = 0; a < symbolsum; a++) {
- printf("1st for: a, alpha[a], ptr[*p]: %d, %c, %c\n", a, alpha[a], ptr[*p]);
- if (alpha[a] == ptr[*p]) {
- printf("Characters: %c\t%c\n", alpha[a], ptr[*p]);
- printf("a, Behavl: %d, %d\n", a, mc.behavlengths[a]);
- for (int b = 0; b < (mc.behavlengths)[a]; b++) {
- printf("2nd for: %c\n", (mc.behaviour)[a][b]);
- if ((mc.behaviour)[a][b] == 'R') {
- moveRight(ptr, l, p);
- }
- if ((mc.behaviour)[a][b] == 'L') {
- moveLeft(ptr, l, p);
- }
- if ((mc.behaviour)[a][b] == 'P') {
- ptr[*p] = (mc.behaviour)[a][b+1];
- }
- if ((mc.behaviour)[a][b] == 'X') {
- return;
- }
- }
- next = mc.next[a];
- if (mc.next[a] == NULL) {
- printf("NULL\n");
- }
- else {
- mc = *next;
- printf("MC Switch\n");
- }
- break;
- }
- }
- printTape(ptr, *l);
- } while (next != NULL && *l < 100);
- }
- int main(int argc, char **argv) {
- char *test = malloc(6* sizeof(char));
- test[0] = 'e';
- test[1] = '1';
- test[2] = '1';
- test[3] = 'x';
- test[4] = '1';
- test[5] = '1';
- int l = 6; //length of the tape
- int p = 0; //current position
- char* alpha; //e1x //String with all symbols that this machine is capable of reading and writing
- getAlpha(&alpha); //getting said string
- printf("[%s]\n", alpha);
- /*
- creating the MCs, will be moved to own function on linux
- */
- mconfig *mc1 = malloc(sizeof(mconfig));
- mconfig *mc2 = malloc(sizeof(mconfig));
- mconfig *mc3 = malloc(sizeof(mconfig));
- mconfig *mc4 = malloc(sizeof(mconfig));
- mconfig *mcs1[] = { mc1, mc2, NULL, NULL };
- mconfig *mcs2[] = { NULL, mc2, mc3, NULL };
- mconfig *mcs3[] = { NULL, mc3, NULL, mc4 };
- mconfig *mcs4[] = { mc1, mc4, mc4, NULL };
- char *behav1[] = { "R", "PeR", "X", "" };
- char *behav2[] = { "", "R", "R", "" };
- char *behav3[] = { "", "R", "", "P1" };
- char *behav4[] = { "", "L", "L", "" };
- int behavlengths1[] = { 1, 3, 1, 0 };
- int behavlengths2[] = { 0, 1, 1, 0 };
- int behavlengths3[] = { 0, 1, 0, 5 };
- int behavlengths4[] = { 0, 1, 1, 0 };
- {
- mc1->next = mcs1;
- mc1->behaviour = behav1;
- mc1->behavlengths = behavlengths1;
- }
- {
- mc2->next = mcs2;
- mc2->behaviour = behav2;
- mc2->behavlengths = behavlengths2;
- }
- {
- mc3->next = mcs3;
- mc3->behaviour = behav3;
- mc3->behavlengths = behavlengths3;
- }
- {
- mc4->next = mcs4;
- mc4->behaviour = behav4;
- mc4->behavlengths = behavlengths4;
- }
- printTape(test, l);
- apply(test, &l, &p, alpha, 4, *mc1);
- printTape(test, l);
- free(test);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement