Advertisement
Megaddd

Multilayer Turing Machine

Oct 26th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <iterator>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <chrono>
  8. #include <thread>
  9. #include <Windows.h>
  10. using namespace std;
  11.  
  12. struct Console
  13. {
  14. void gotoxy(int x, int y)
  15. {
  16. COORD coord;
  17. coord.X = x;
  18. coord.Y = y;
  19. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  20. }
  21. int wherey()
  22. {
  23. CONSOLE_SCREEN_BUFFER_INFO csbi;
  24. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  25. COORD result = csbi.dwCursorPosition;
  26. return result.Y;
  27. }
  28. };
  29.  
  30. struct rules
  31. {
  32. string label_in;
  33. char sym_in;
  34. char sym_out;
  35. char direction;
  36. string label_out;
  37. };
  38.  
  39. int find_rule(string& label, char& symbol, vector<rules>& check)
  40. {
  41. for (unsigned int i = 0; i < check.size(); i++)
  42. {
  43. if ((check[i].label_in == label) && (check[i].sym_in == symbol))
  44. {
  45. return i;
  46. }
  47. }
  48. for (unsigned int i = 0; i < check.size(); i++)
  49. {
  50. if ((check[i].label_in == label) && (check[i].sym_in == '*'))
  51. {
  52. return i;
  53. }
  54. }
  55. return -1;
  56. }
  57.  
  58. class Machine
  59. {
  60. private:
  61. vector<rules> lines;
  62. string line;
  63. int n;
  64. string tape;
  65. int head;
  66. string state;
  67. int i_rule = 0;
  68. bool running;
  69. string haltReason;
  70. public:
  71. string cur_tape = tape;
  72. string cur_state = state;
  73. bool is_running = running;
  74. string halt_reason = haltReason;
  75. Machine() {}
  76. void readfile(string filename)
  77. {
  78. ifstream r;
  79. r.open(filename); r >> n;
  80. r >> tape;
  81.  
  82. while (getline(r, line))
  83. {
  84. istringstream ss(line);
  85. istream_iterator<string> begin(ss), end;
  86. vector<string> members(begin, end);
  87.  
  88. if (members.size() < 5) continue;
  89.  
  90. rules a{ members[0], members[1][0], members[2][0], members[3][0], members[4] };
  91.  
  92. lines.push_back(a);
  93. }
  94.  
  95. r.close();
  96.  
  97. state = "0";
  98. head = n - 1;
  99. running = true;
  100. }
  101. void iterate()
  102. {
  103. if (running)
  104. {
  105. if (head < 0 || head > tape.length()) {
  106. haltReason = "halt: iterator out of bounds.";
  107. running = false;
  108. return;
  109. }
  110. i_rule = find_rule(state, tape[head], lines);
  111. if (i_rule != -1)
  112. {
  113. if (lines[i_rule].sym_out != '*')
  114. {
  115. tape[head] = lines[i_rule].sym_out;
  116. }
  117. if (lines[i_rule].direction == 'R') head++;
  118. else if (lines[i_rule].direction == 'L') head--;
  119. state = lines[i_rule].label_out;
  120. }
  121. else
  122. {
  123. haltReason = (string)"no rule found for '" + tape[head] + "' under state '" + state + "'.";
  124. running = false;
  125. }
  126. }
  127. cur_tape = tape;
  128. cur_state = state;
  129. is_running = running;
  130. halt_reason = haltReason;
  131. }
  132. };
  133.  
  134. class Renderer
  135. {
  136. private:
  137. Console CMD;
  138. int startingY;
  139. string msg_on = "< running >";
  140. string msg_off = "< stopped >";
  141. public:
  142. Renderer() { startingY = CMD.wherey(); }
  143. void init() { startingY = CMD.wherey(); }
  144. void display(vector<Machine>& targets)
  145. {
  146. CMD.gotoxy(0, startingY);
  147. for (int i = 0; i < targets.size(); i++)
  148. {
  149. string pre = (to_string(i+1) + ". ");
  150. string sub = pre.substr(0, 3);
  151. cout << endl << endl << sub << targets[i].cur_tape << endl;
  152. if (targets[i].is_running)
  153. {
  154. cout << msg_on << " state: " << targets[i].cur_state;
  155. }
  156. else
  157. {
  158. cout << msg_off << " reason: " << targets[i].halt_reason;
  159. }
  160. }
  161. }
  162. };
  163.  
  164. int main() {
  165. vector<Machine> M;
  166. cout << "'Turing' job filename: ";
  167. string input;
  168. while (getline(cin, input)) //file reading loop
  169. {
  170. ifstream file(input);
  171. if (input != "")
  172. {
  173. if (file.good())
  174. {
  175. Machine P;
  176. M.push_back(P);
  177. M[M.size() - 1].readfile(input);
  178. cout << endl << "File '" << input << "' added to job list." << endl << endl << "'Turing' job filename: ";
  179. }
  180. else
  181. {
  182. cout << endl << "File '" << input << "' does not exist." << endl << endl << "'Turing' job filename: ";
  183. }
  184. }
  185. else
  186. {
  187. if (M.size() > 0)
  188. {
  189. break;
  190. }
  191. else
  192. {
  193. cout << endl << "File '" << input << "' does not exist." << endl << endl << "'Turing' job filename: ";
  194. }
  195. }
  196. }
  197. cout << "Cycle delay (ms): ";
  198. unsigned short slowdown; cin >> slowdown;
  199. cout << "Running Turing Machine with delay of (" << slowdown << ")." << endl;
  200. Renderer R;
  201. while (true)
  202. {
  203. for (int i = 0; i < M.size(); i++)
  204. {
  205. M[i].iterate();
  206. }
  207. R.display(M);
  208. int alive = 0;
  209. for (int i = 0; i < M.size(); i++)
  210. {
  211. alive += M[i].is_running;
  212. }
  213. if (alive == 0)
  214. {
  215. cout << endl << endl << "All Turing Machines halted." << endl;
  216. break;
  217. }
  218. this_thread::sleep_for(chrono::milliseconds(slowdown));
  219. }
  220. return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement