Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <iterator>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <chrono>
- #include <thread>
- #include <Windows.h>
- using namespace std;
- struct Console
- {
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- COORD result = csbi.dwCursorPosition;
- return result.Y;
- }
- };
- struct rules
- {
- string label_in;
- char sym_in;
- char sym_out;
- char direction;
- string label_out;
- };
- int find_rule(string& label, char& symbol, vector<rules>& check)
- {
- for (unsigned int i = 0; i < check.size(); i++)
- {
- if ((check[i].label_in == label) && (check[i].sym_in == symbol))
- {
- return i;
- }
- }
- for (unsigned int i = 0; i < check.size(); i++)
- {
- if ((check[i].label_in == label) && (check[i].sym_in == '*'))
- {
- return i;
- }
- }
- return -1;
- }
- class Machine
- {
- private:
- vector<rules> lines;
- string line;
- int n;
- string tape;
- int head;
- string state;
- int i_rule = 0;
- bool running;
- string haltReason;
- public:
- string cur_tape = tape;
- string cur_state = state;
- bool is_running = running;
- string halt_reason = haltReason;
- Machine() {}
- void readfile(string filename)
- {
- ifstream r;
- r.open(filename); r >> n;
- r >> tape;
- while (getline(r, line))
- {
- istringstream ss(line);
- istream_iterator<string> begin(ss), end;
- vector<string> members(begin, end);
- if (members.size() < 5) continue;
- rules a{ members[0], members[1][0], members[2][0], members[3][0], members[4] };
- lines.push_back(a);
- }
- r.close();
- state = "0";
- head = n - 1;
- running = true;
- }
- void iterate()
- {
- if (running)
- {
- if (head < 0 || head > tape.length()) {
- haltReason = "halt: iterator out of bounds.";
- running = false;
- return;
- }
- i_rule = find_rule(state, tape[head], lines);
- if (i_rule != -1)
- {
- if (lines[i_rule].sym_out != '*')
- {
- tape[head] = lines[i_rule].sym_out;
- }
- if (lines[i_rule].direction == 'R') head++;
- else if (lines[i_rule].direction == 'L') head--;
- state = lines[i_rule].label_out;
- }
- else
- {
- haltReason = (string)"no rule found for '" + tape[head] + "' under state '" + state + "'.";
- running = false;
- }
- }
- cur_tape = tape;
- cur_state = state;
- is_running = running;
- halt_reason = haltReason;
- }
- };
- class Renderer
- {
- private:
- Console CMD;
- int startingY;
- string msg_on = "< running >";
- string msg_off = "< stopped >";
- public:
- Renderer() { startingY = CMD.wherey(); }
- void init() { startingY = CMD.wherey(); }
- void display(vector<Machine>& targets)
- {
- CMD.gotoxy(0, startingY);
- for (int i = 0; i < targets.size(); i++)
- {
- string pre = (to_string(i+1) + ". ");
- string sub = pre.substr(0, 3);
- cout << endl << endl << sub << targets[i].cur_tape << endl;
- if (targets[i].is_running)
- {
- cout << msg_on << " state: " << targets[i].cur_state;
- }
- else
- {
- cout << msg_off << " reason: " << targets[i].halt_reason;
- }
- }
- }
- };
- int main() {
- vector<Machine> M;
- cout << "'Turing' job filename: ";
- string input;
- while (getline(cin, input)) //file reading loop
- {
- ifstream file(input);
- if (input != "")
- {
- if (file.good())
- {
- Machine P;
- M.push_back(P);
- M[M.size() - 1].readfile(input);
- cout << endl << "File '" << input << "' added to job list." << endl << endl << "'Turing' job filename: ";
- }
- else
- {
- cout << endl << "File '" << input << "' does not exist." << endl << endl << "'Turing' job filename: ";
- }
- }
- else
- {
- if (M.size() > 0)
- {
- break;
- }
- else
- {
- cout << endl << "File '" << input << "' does not exist." << endl << endl << "'Turing' job filename: ";
- }
- }
- }
- cout << "Cycle delay (ms): ";
- unsigned short slowdown; cin >> slowdown;
- cout << "Running Turing Machine with delay of (" << slowdown << ")." << endl;
- Renderer R;
- while (true)
- {
- for (int i = 0; i < M.size(); i++)
- {
- M[i].iterate();
- }
- R.display(M);
- int alive = 0;
- for (int i = 0; i < M.size(); i++)
- {
- alive += M[i].is_running;
- }
- if (alive == 0)
- {
- cout << endl << endl << "All Turing Machines halted." << endl;
- break;
- }
- this_thread::sleep_for(chrono::milliseconds(slowdown));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement