/* Copyright (C) 2014 by Alexandru Cojocaru */ /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include typedef struct unit unit; struct unit { int q1; char s1, s2; char d; int q2; unit *next; }; static unit *head; static char tape[100]; static unit * newunit (char *s) { unit *u = malloc (sizeof (*u)); assert (u); u->q1 = s[0]-'0'; u->s1 = s[1]; u->s2 = s[2]; u->d = s[3]; if (s[4] == '-') u->q2 = -1; else u->q2 = s[4]-'0'; u->next = NULL; return u; } static unit * nextunit (unit *head, int ti, int q1) { unit *p = head; while (p) { if (p->q1 == q1 && p->s1 == tape[ti]) return p; p = p->next; } assert (0); } static void run (unit *head) { unit *p; int q1 = 0; int ti = 0; while (q1 != -1) { p = nextunit (head, ti, q1); tape[ti] = p->s2; q1 = p->q2; ti += p->d == 'R' ? 1 : -1; assert (ti >= 0 && ti < sizeof (tape)/sizeof (tape[0])); } } int main (void) { strcpy (tape, "111+11111_"); printf ("%s\n", tape); unit *p; head = newunit ("011R0"); p = head; p->next = newunit ("0+1R1"); p = p->next; p->next = newunit ("111R1"); p = p->next; p->next = newunit ("1__L2"); p = p->next; p->next = newunit ("21_L-"); run (head); printf ("%s\n", tape); return 0; }