-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
202 lines (174 loc) · 7.01 KB
/
index.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!doctype html>
<html>
<head lang="ru">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Преобразование алгоритма в терминах нормальной схемы алгоритма Маркова в машину Тьюринга</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2>Алгоритм Маркова</h2>
<!-- <textarea class="form-control input-lg" id="markovTextarea" rows="10">
// Прибавление единицы к двоичному числу
1*->*0
0*->.1
*->.1
$0->0$
$1->1$
$->*
->$
</textarea> -->
<div class="form-group">
<input type="text" class="form-control" id="markovTextarea" value="abc->baa">
</div>
<div class="form-group">
<a href="#" class="btn btn-dark" onclick="markov2turingClick(event);return false;" style="" role="button">Преобразовать в машину Тьюрингу</a>
</div>
</div>
<div class="col-md-12">
<h2>Машина Тьюринга</h2>
<pre id="turingOut"></pre>
<!-- <textarea class="form-control" id="turingTextarea" rows="10"></textarea> -->
</div>
</div>
</div>
<script type="text/javascript">
class markovProduction {
constructor(str) {
var a = str.split('->');
this.from = a[0];
if (a[1].substr(0,1)=='.') {
this.to = a[1].substr(1)
this.end = 1;
} else {
this.to = a[1];
this.end = 0;
}
this.alphabet = (this.to+this.from).split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; });
}
}
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
function markov2turingClick (event) {
event.preventDefault();
$("#turingOut").html("");
markov2turing();
}
function markov2turing() {
var markovStringArray = document.querySelector('#markovTextarea').value.split('\n');
var turingStringArray = [];
for (var i = markovStringArray.length - 1; i >= 0; i--) {
if ((markovStringArray[i].substr(0,2) == "//") || markovStringArray[i] == "")
markovStringArray.remove(i);
}
for (var i = 0 ; i < markovStringArray.length; i++) {
// turingStringArray = turingStringArray.concat(m2t(markovStringArray[i]));
m2t(markovStringArray, i);
}
}
function out(str) {
// console.log (str);
// $('#turingTextarea').val(function(i, text) {
// return text + str+ "\n";
// });
$('#turingOut').append(str+ "\n");
}
class searchStates {
constructor(a, b) {
this.found = a;
this.notfound = b;
}
}
function search(prod, prodnum, nextprodnum) {
var s;
var str = prod.from;
for (var i = 0; i < prod.from.length; i++) {
s = prod.from.charAt(i);
if (i == (prod.from.length - 1)) {
out("// Последний символ найден, подстрока найдена");
out("prod"+prodnum+"_search"+i+" "+s+" -> prod"+prodnum+"_found "+s+" E");
}
else {
// out("//подстрока найдена");
out("prod"+prodnum+"_search"+i+" "+s+" -> prod"+prodnum+"_search"+(i+1)+" "+s+" R");
}
if ((i != 0) && (i != (prod.from.length - 1))){
out("// Набор команд для возврата к начальному состоянию (найден лишний символ)");
for (var j = 0; j < prod.from.length; j++){
if (j!=i)
out("prod"+prodnum+"_search"+i+" "+prod.from.charAt(j)+" -> prod"+prodnum+"_search0 "+prod.from.charAt(j)+" E");
}
out("// Возврат к состоянию первого символа");
out("prod"+prodnum+"_search"+i+" "+prod.from.charAt(0)+" -> prod"+prodnum+"_search1 "+prod.from.charAt(0)+" R");
}
}
out("// Конец строки, подстрока не найдена");
for (var i = 0; i < prod.from.length; i++) {
out("prod"+prodnum+"_search"+i+" B -> prod"+prodnum+"_notfound B L");
}
out("// Движемся в начало строки");
for (var i = 0; i < prod.alphabet.length; i++) {
out("prod"+prodnum+"_notfound "+prod.alphabet[i]+" -> prod"+prodnum+"_notfound "+prod.alphabet[i]+" L");
}
if (nextprodnum >= 0) {
out("prod"+prodnum+"_notfound B -> prod"+nextprodnum+"_search0 B R");
} else
out("prod"+prodnum+"_notfound B -> error B R");
return new searchStates("prod"+prodnum+"_found", "prod"+prodnum+"_notfound");
}
function shift(n, direction='R') {
}
function replace(prod, prodnum, search) {
// if (prod.from.length < prod.to.length)
// shift();
// if (prod.from.length > prod.to.length)
// shift();
out("// Приступаем к записи");
if (prod.from.length == prod.to.length) {
// не нужно двигать ничего
out("prod"+prodnum+"_found "+prod.from.charAt(prod.from.length-1)+" -> prod"+prodnum+"_write"+(prod.from.length-1)+" "+prod.from.charAt(prod.from.length-1)+" E");
}
for (var i = prod.to.length - 1; i >= 0; i--) {
var charFrom;
if (i<prod.from.length) charFrom = prod.from.charAt(i);
else charFrom = 'B';
out("prod"+prodnum+"_write"+i+" "+charFrom+" -> prod"+prodnum+"_write"+i+" "+prod.to.charAt(i)+" E");
// prod.to[i]
}
//к началу строки
out("// Возвращаемся к началу строки");
for (var i = 0; i < prod.alphabet.length; i++) {
out("prod"+prodnum+"_write0 "+prod.alphabet[i]+" -> prod"+prodnum+"_write0 "+prod.alphabet[i]+" L");
}
if (prod.end) {
out("prod"+prodnum+"_write0 B -> end B R");
} else {
out("prod"+prodnum+"_write0 B -> prod"+prodnum+"_search0 B R");
}
}
function m2t(markovProductionStr, prodnum) {
// console.log(markovProductionStr[prodnum]);
var m = new markovProduction(markovProductionStr[prodnum]);
out("// Алфавит: " + m.alphabet.join(" "));
console.log(m);
if (prodnum < markovProductionStr.length - 1) nextprodnum = prodnum + 1;
else nextprodnum = -1;
var s = search(m, prodnum, nextprodnum);
// console.log(s);
var r = replace(m, prodnum, s);
// out("prod"+prodnum+"_search"+i+" "+str.charAt(0)+" -> prod"+prodnum+"_search1 "+str.charAt(0)+" R");
}
</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
</html>