Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct mconfig {
  6. struct mconfig** next;
  7. char** behaviour;
  8. int* behavlengths;
  9. } mconfig;
  10.  
  11. /*
  12. Legacy function, doesn't work right now.
  13. */
  14. mconfig* createmconfig(mconfig** next, char** behaviour, int behavlengths[]) {
  15. mconfig* mcc = malloc(sizeof(mconfig));
  16. mcc->behaviour = behaviour;
  17. mcc->next = next;
  18. mcc->behavlengths = behavlengths;
  19. return mcc;
  20. }
  21.  
  22. void getAlpha(char **a) { //Gotta do this on linux...
  23.  
  24. //char test[5];
  25.  
  26.  
  27. //printf("Which Symbols do you need?\n");
  28. //scanf("%ms", a);
  29. //printf("%s\n", *a);
  30. //strcpy(a, test);
  31. /*
  32. test[0] = 'a';
  33. test[1] = 'a';
  34. test[2] = 'a';
  35. test[3] = 'a';
  36.  
  37. test[4] = '\0';
  38. */
  39. char *t = "e1x ";
  40. *a = t;
  41. }
  42.  
  43. void getmconfigs(mconfig** mcs) { //do this on linux...
  44. printf("How many mconfigs do you need?\n");
  45. int mcsum = 0;
  46. scanf("%d", &mcsum);
  47. mconfig mcarray[mcsum];
  48. for (int a = 0; a < mcsum; a++) {
  49. printf("%d", a);
  50. }
  51. printf("\n");
  52.  
  53. }
  54.  
  55. /*
  56. @param ptr pointer to the initial block of memory that will be moved
  57. @param l length of the block of memory pointed to by ptr that will be moved
  58.  
  59. More momory-efficient implementation of memmove(), specifically for this program. Moves the block of memory pointed to by ptr one byte to the left.
  60. */
  61. void memspace(char *ptr, int l) {
  62. for (int a = l; a > 0; a--) {
  63. memcpy(ptr + a, ptr + a - 1, 1);
  64. }
  65. }
  66.  
  67. /*
  68. @param ptr pointer to the block of memory (char-array/tape)
  69. @param l length of the block of memory (char-array/tape)
  70. @param p pointer to the integer storing the current position of the turing machine on the tape
  71. @param chr character to be written in the new segment
  72.  
  73. Function to extend the char-array/tape exactly one segment to the right.
  74. */
  75. void extendRight(char *ptr, int* l, int* p, char chr) {
  76.  
  77. realloc(ptr, (*l + 1) * sizeof(char));
  78. memcpy(ptr + *l, &chr, 1);
  79.  
  80. *l += 1;
  81. *p += 1;
  82. }
  83.  
  84. /*
  85. @param ptr pointer to the block of memory (char-array/tape)
  86. @param l length of the block of memory (char-array/tape)
  87. @param p pointer to the integer storing the current position of the turing machine on the tape
  88. @param chr character to be written in the new segment
  89.  
  90. Function to extend the char-array/tape exactly one segment to the left.
  91. */
  92. void extendLeft(char *ptr, int* l, int* p, char chr) {
  93.  
  94. realloc(ptr, (*l + 1) * sizeof(char));
  95. //memmove(ptr + 1, ptr, *l);
  96. memspace(ptr, *l + 1);
  97. memcpy(ptr, &chr, 1);
  98.  
  99. *l += 1;
  100. *p = 0;
  101. }
  102. /*
  103. @param ptr pointer to the block of memory (char-array/tape)
  104. @param l length of the block of memory (char-array/tape)
  105.  
  106. Function to print the whole char-array/tape to the command line.
  107. */
  108.  
  109. void printTape(char* ptr, int l) {
  110. char *p = ptr;
  111. for (int a = 0; a < l; a++) {
  112. printf("%c", *p);
  113. p++;
  114. }
  115. printf("\n");
  116. }
  117.  
  118. /*
  119. @param ptr pointer to the block of memory (char-array/tape)
  120. @param l length of the block of memory (char-array/tape)
  121. @param p pointer to the integer storing the current position of the turing machine on the tape
  122.  
  123. 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.
  124. */
  125. void moveLeft(char* ptr, int* l, int* p) {
  126. if (*p == 0) {
  127. extendLeft(ptr, l, p, ' ');
  128. }
  129. else {
  130. *p -= 1;
  131. }
  132. }
  133.  
  134. /*
  135. @param ptr pointer to the block of memory (char-array/tape)
  136. @param l length of the block of memory (char-array/tape)
  137. @param p pointer to the integer storing the current position of the turing machine on the tape
  138.  
  139. 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.
  140. */
  141. void moveRight(char* ptr, int* l, int* p) {
  142. if (*p == (*l - 1)) {
  143. extendRight(ptr, l, p, ' ');
  144. }
  145. else {
  146. *p += 1;
  147. }
  148. }
  149.  
  150. /*
  151. @param ptr pointer to the block of memory (char-array/tape)
  152. @param l length of the block of memory (char-array/tape)
  153. @param p pointer to the integer storing the current position of the turing machine on the tape
  154. @param mc the starting mconfiguration/state for the machine
  155.  
  156. Starts the turing machine with mconfiguration/state mc, tape ptr with length l, and starting position p.
  157. */
  158. void apply(char* ptr, int* l, int* p, char* alpha, int symbolsum, mconfig mc) {
  159. mconfig *next = NULL;
  160. printf("Apply start\n");
  161. do {
  162. for (int a = 0; a < symbolsum; a++) {
  163. printf("1st for: a, alpha[a], ptr[*p]: %d, %c, %c\n", a, alpha[a], ptr[*p]);
  164. if (alpha[a] == ptr[*p]) {
  165. printf("Characters: %c\t%c\n", alpha[a], ptr[*p]);
  166. printf("a, Behavl: %d, %d\n", a, mc.behavlengths[a]);
  167.  
  168. for (int b = 0; b < (mc.behavlengths)[a]; b++) {
  169. printf("2nd for: %c\n", (mc.behaviour)[a][b]);
  170. if ((mc.behaviour)[a][b] == 'R') {
  171. moveRight(ptr, l, p);
  172. }
  173. if ((mc.behaviour)[a][b] == 'L') {
  174. moveLeft(ptr, l, p);
  175. }
  176. if ((mc.behaviour)[a][b] == 'P') {
  177. ptr[*p] = (mc.behaviour)[a][b+1];
  178. }
  179. if ((mc.behaviour)[a][b] == 'X') {
  180. return;
  181. }
  182. }
  183. next = mc.next[a];
  184. if (mc.next[a] == NULL) {
  185. printf("NULL\n");
  186. }
  187. else {
  188. mc = *next;
  189. printf("MC Switch\n");
  190. }
  191. break;
  192. }
  193. }
  194. printTape(ptr, *l);
  195. } while (next != NULL && *l < 100);
  196.  
  197. }
  198.  
  199. int main(int argc, char **argv) {
  200. char *test = malloc(6* sizeof(char));
  201.  
  202. test[0] = 'e';
  203. test[1] = '1';
  204. test[2] = '1';
  205. test[3] = 'x';
  206. test[4] = '1';
  207. test[5] = '1';
  208.  
  209. int l = 6; //length of the tape
  210. int p = 0; //current position
  211.  
  212. char* alpha; //e1x //String with all symbols that this machine is capable of reading and writing
  213. getAlpha(&alpha); //getting said string
  214. printf("[%s]\n", alpha);
  215.  
  216. /*
  217. creating the MCs, will be moved to own function on linux
  218. */
  219. mconfig *mc1 = malloc(sizeof(mconfig));
  220. mconfig *mc2 = malloc(sizeof(mconfig));
  221. mconfig *mc3 = malloc(sizeof(mconfig));
  222. mconfig *mc4 = malloc(sizeof(mconfig));
  223. mconfig *mcs1[] = { mc1, mc2, NULL, NULL };
  224. mconfig *mcs2[] = { NULL, mc2, mc3, NULL };
  225. mconfig *mcs3[] = { NULL, mc3, NULL, mc4 };
  226. mconfig *mcs4[] = { mc1, mc4, mc4, NULL };
  227. char *behav1[] = { "R", "PeR", "X", "" };
  228. char *behav2[] = { "", "R", "R", "" };
  229. char *behav3[] = { "", "R", "", "P1" };
  230. char *behav4[] = { "", "L", "L", "" };
  231. int behavlengths1[] = { 1, 3, 1, 0 };
  232. int behavlengths2[] = { 0, 1, 1, 0 };
  233. int behavlengths3[] = { 0, 1, 0, 5 };
  234. int behavlengths4[] = { 0, 1, 1, 0 };
  235.  
  236. {
  237. mc1->next = mcs1;
  238. mc1->behaviour = behav1;
  239. mc1->behavlengths = behavlengths1;
  240. }
  241. {
  242. mc2->next = mcs2;
  243. mc2->behaviour = behav2;
  244. mc2->behavlengths = behavlengths2;
  245. }
  246. {
  247. mc3->next = mcs3;
  248. mc3->behaviour = behav3;
  249. mc3->behavlengths = behavlengths3;
  250. }
  251. {
  252. mc4->next = mcs4;
  253. mc4->behaviour = behav4;
  254. mc4->behavlengths = behavlengths4;
  255. }
  256.  
  257. printTape(test, l);
  258. apply(test, &l, &p, alpha, 4, *mc1);
  259.  
  260. printTape(test, l);
  261.  
  262. free(test);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement