1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
void Universal_Turing_Machine(string current_state[], char current_symbol[], char new_symbol[], char direction[], string new_state[], string tape, int head, int number_of_rows)
{
int n = tape.length();
string next_state = "0";
string cursor = " ";
for (auto i = 0; i < n - 1; i++)
{
cursor.append(" ");
}
cout << n;
cout << cursor << endl;
cursor[head] = '^';
while (next_state != "X")
{
for (auto i = 0; i < number_of_rows; i++)
{
if (next_state == current_state[i])
{
if (current_symbol[i] == tape[head])
{
tape[head] = new_symbol[i];
next_state = new_state[i];
if (next_state == "X")
{
break;
}
if (direction[i] == 'R')
{
cursor[head] = ' ';
head++;
cursor[head] = '^';
}
else
{
cursor[head] = ' ';
head--;
cursor[head] = '^';
}
}
}
}
system("cls");
cout << tape << endl;
cout << cursor << endl;
sleep_for(milliseconds(60));
}
}
int main()
{
string tape, current_state[100], new_state[100];
int head, file = 0, number_of_rows = 0;
char direction[100], current_symbol[100], new_symbol[100];
ifstream fin;
while (file > 4 || file < 1)
{
cout << "Choose a text file!" << endl;
cout << "1) 1.txt\n2) 2.txt\n3) 3.txt\n4) 4.txt" << endl;
cin >> file;
switch (file)
{
case 1: { fin.open("1.txt"); break; }
case 2: { fin.open("2.txt"); break; }
case 3: { fin.open("3.txt"); break; }
case 4: { fin.open("4.txt"); break; }
default: { cout << "Your choice is unavailable! Pick between 1, 2, 3, 4!"; sleep_for(seconds(2)); system("cls"); break; }
}
}
cout << "You chose " << file << ".txt!" << endl;
fin >> head;
fin >> tape;
while (!fin.eof())
{
fin >> current_state[number_of_rows] >> current_symbol[number_of_rows] >> new_symbol[number_of_rows] >> direction[number_of_rows] >> new_state[number_of_rows];
number_of_rows++;
}
fin.close();
number_of_rows--;
cout << "Data from the text file was read successfully!" << endl;
sleep_for(seconds(2));
system("cls");
cout << tape << endl;
Universal_Turing_Machine(current_state, current_symbol, new_symbol, direction, new_state, tape, head, number_of_rows);
}
|