Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Enter your code here, enjoy!
- $alphabet = '';
- $carets = '';
- $EMPTY_SYMBOL = '';
- $STOP_CARET_SYMBOL = '';
- $str_cassette = '';
- $position = 0;
- $table = [];
- $temp_to_write = '';
- //Ввод данных
- getConsoleData('Write a alphabet of Turing Machine', $alphabet);
- $alphabet = explode(' ', $alphabet);
- getConsoleData('Write a list of carets', $carets);
- $carets = explode(' ', $carets);
- getConsoleData('Write a empty symbol', $EMPTY_SYMBOL);
- $alphabet = array_merge([$EMPTY_SYMBOL], $alphabet);
- getConsoleData('Write a stop carets symbol', $STOP_CARET_SYMBOL);
- getConsoleData('Write a default cassette', $str_cassette);
- getConsoleData('Write a position on cassette', $position);
- foreach ($carets as $key => $caret) {
- foreach ($alphabet as $value) {
- getConsoleData('Write ' . $caret . '->' . $value, $temp_to_write);
- $table[$caret][$value] = explode(' ', $temp_to_write);
- }
- }
- //print_r($table);
- //Конец ввода данных
- //Выполнение алгоритма
- $explode_cassette = explode(' ', $str_cassette);
- $cassette = array_merge([$EMPTY_SYMBOL], $explode_cassette , [$EMPTY_SYMBOL]);
- /*print_r($cassette);*/
- $position = count($explode_cassette);
- /*$table = [
- 'q1' => ['', ['q2', $EMPTY_SYMBOL, 'l'], ['q0', $EMPTY_SYMBOL, 's']],//['q3', '1', 'r'], ['q1', $EMPTY_SYMBOL, 'l']
- 'q2' => [['q3', '1', 'r'], ['q2', '1', 'l'], ['q2', '*', 'l']],
- 'q3' => [['q1', $EMPTY_SYMBOL, 'l'], ['q3', '1', 'r'], ['q3', '*', 'r']]
- ];*/
- $q = 'q1';
- $count_iter = 0;
- //echo $cassette[$position] . ' ' . $q . PHP_EOL;
- //print_r($table[$q][$cassette[$position]]);
- while($q != $STOP_CARET_SYMBOL) {
- if($count_iter > 500)
- break;
- $count_iter++;
- /* echo $q . PHP_EOL;
- echo toRow($cassette[$position]) . PHP_EOL;
- exit();*/
- $current = $table[$q][$cassette[$position]];
- $temp_q = $q;
- $temp_cassette = $cassette[$position];
- $cassette[$position] = $current[1];
- $q = $current[0];
- if($current[2] == 'l') {
- $position--;
- }elseif($current[2] == 'r') {
- $position++;
- }
- if($position < 0) {
- $cassette = array_merge([$EMPTY_SYMBOL], $cassette);
- $position = 0;
- }
- if($position > count($cassette))
- $cassette = array_merge( $cassette, [$EMPTY_SYMBOL]);
- echo $count_iter . ') ' . $temp_q . $temp_cassette . '->' . implode('', $current) . PHP_EOL;
- $line = str_create('-', count($cassette)*2+1) . PHP_EOL;
- echo $line;
- echo '|' . implode('|', $cassette) . '|' . PHP_EOL;
- echo $line;
- }
- /*function toRow($name) {
- $rows = [$EMPTY_SYMBOL => 0, '1' => 1, '*' => 2];
- return $rows[$name];
- }*/
- function str_create($symbol, $count) {
- if($count < 2) return $symbol;
- return $symbol . str_create($symbol, $count - 1);
- }
- function getConsoleData($message, &$var) {
- echo $message . ': ';
- $var = trim(fgets(STDIN));
- echo PHP_EOL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement