-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturing.cpp
More file actions
159 lines (126 loc) · 4.76 KB
/
Copy pathturing.cpp
File metadata and controls
159 lines (126 loc) · 4.76 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************/
/* File Name: turing.cpp */
/* Abstraction: Turing machine */
/* Description: Course work */
/* Date: 26.04.2020 */
/* Author: Viacheslav Sipakov */
/*******************************/
#include <iostream>
#include <fstream>
#include <string.h>
#define TAPE_LEN 256
using std::cout;
using std::endl;
using std::cin;
using std::cerr;
using std::ifstream;
int last = 0, condition = 1;
bool detailed_mode = 0;
struct Command{ //структурный тип данных, реализующий команду машине
int q; //состояние
char a; //символ
char move; //шаг
};
void tape_print(char * tape, int cursor, char zero_symbol){ //функция вывода ленты на консоль
bool b = 0;
for (int j = 0; j <= last; j++){
if (b || tape[j] != zero_symbol){
if (cursor == j)
cout << "\e[1m" << tape[j] << "\e[0m";
else cout << tape[j];
b = 1;
}
}
if (detailed_mode) cout << " \tCondition: " << condition;
cout << endl;
}
int main(int argc, char ** argv){
int cursor = -1,m,n,command,steps,j = 0;
short int symbol_num = -1;//cout << n << '-' << endl
char tape_ini[TAPE_LEN],tape[TAPE_LEN],buf[3];
char logo[] = "=============================================================\n"
"= *** **** **** | Nizhny Novgorod Technical University =\n"
"= * * * ** ** ** | Course Work =\n"
"= * * * ** ** | 'Turing Machine' =\n"
"= * * ** ** | Performed by student =\n"
"= * ** ** ** | Viacheslav Sipakov =\n"
"= ** **** | Group number 19-IVT-3 =\n"
"=============================================================\n";
cout << logo;
if (argc < 2 || argc > 3 || argc == 3 && argv[2][0] != '-'){
cerr << "Error: Invalid arguments\n";
exit(1);
}
ifstream in;
in.open(argv[1]);
if (!in.is_open()){
cerr << "Error: Could not open file\n";
exit(1);
}
if (argc == 3 && argv[2][1] == 'd'){
cout << "= Detailed mode =\n";
detailed_mode = 1;
}
in >> tape_ini; //ввод начальной ленты из файла
in >> m; //ввод к-ва состояний из файла
in >> n; //ввод к-ва символов из файла
Command * c = new Command[m*n]; //создание массива структурного типа Command
char * symbol = new char[n]; //создание массива символов, используемых в данной машине
for (int i = 0; i < m; i++) //
in >> buf;
for (int i = 0; i < n; i++){
in >> symbol[i]; //заполнение массива символов
for (j = 0; j < m; j++){ //заполнение массива команд
int k = i*m+j;
in >> c[k].q;
in >> c[k].a;
in >> c[k].move;
}
}
in.close();
for (int i = 0; i <= 261; i++)
tape[i] = symbol[0]; //заполнение ленты нулевым символом
j = 0;
for (int i = 0; i < strlen(tape_ini); i++){
if (tape_ini[i] == '!')
cursor = i + 119;
else {
tape[120+j] = tape_ini[i];
j++;
}
last = 119 + i; //вспомогательная переменная для обозначения последнего значащего символа
}
if (cursor < 0){ //проверка на наличие курсора
cerr << "Error: no cursor!" << endl;
exit(1);
}
tape_print(tape,cursor,symbol[0]);
for (int i = 0; condition > 0; i++){ //основной цикл
for (j = 0; j < n; j++){ //определение номера символа
if (tape[cursor] == symbol[j]){
symbol_num = j;
break;
}
}
command = m*symbol_num + condition - 1; //определение номера команды
tape[cursor] = c[command].a; //получение нового символа
condition = c[command].q; //получение нового состояния
switch (c[command].move){
case 'L': {cursor--; break;}
case 'R': {cursor++; break;}
case 'N': {break;}
}
if (cursor > last && tape[cursor] != 0) //изменение переменной last
last = cursor;
while (tape[last] == symbol[0] && cursor < last)
last -= 1;
steps = i; //счетчик шагов машины
tape_print(tape,cursor,symbol[0]);
if (i > 500){
cerr << "Error: infinite cycle! Terminating the program...\n";
break;
}
}
cout << "Steps = " << steps + 1 << endl;
return 0;
}