Advertisement
xladomaz

Turing Machine NEW

Dec 2nd, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2. //Enter your code here, enjoy!
  3.  
  4. $alphabet = '';
  5. $carets = '';
  6.  
  7. $EMPTY_SYMBOL = '';
  8. $STOP_CARET_SYMBOL = '';
  9.  
  10. $str_cassette = '';
  11.  
  12. $position = 0;
  13.  
  14. $table = [];
  15.  
  16. $temp_to_write = '';
  17.  
  18. //Ввод данных
  19.  
  20. getConsoleData('Write a alphabet of Turing Machine', $alphabet);
  21. $alphabet = explode(' ', $alphabet);
  22.  
  23. getConsoleData('Write a list of carets', $carets);
  24. $carets = explode(' ', $carets);
  25.  
  26. getConsoleData('Write a empty symbol', $EMPTY_SYMBOL);
  27. $alphabet = array_merge([$EMPTY_SYMBOL], $alphabet);
  28.  
  29. getConsoleData('Write a stop carets symbol', $STOP_CARET_SYMBOL);
  30.  
  31.  
  32. getConsoleData('Write a default cassette', $str_cassette);
  33.  
  34. getConsoleData('Write a position on cassette', $position);
  35.  
  36. foreach ($carets as $key => $caret) {
  37.     foreach ($alphabet as $value) {
  38.         getConsoleData('Write ' . $caret . '->' . $value, $temp_to_write);
  39.         $table[$caret][$value] = explode(' ', $temp_to_write);
  40.     }
  41. }
  42.  
  43. //print_r($table);
  44.  
  45. //Конец ввода данных
  46.  
  47. //Выполнение алгоритма
  48. $explode_cassette = explode(' ', $str_cassette);
  49. $cassette = array_merge([$EMPTY_SYMBOL], $explode_cassette , [$EMPTY_SYMBOL]);
  50.  
  51. /*print_r($cassette);*/
  52. $position = count($explode_cassette);
  53. /*$table = [
  54.     'q1' => ['', ['q2', $EMPTY_SYMBOL, 'l'], ['q0', $EMPTY_SYMBOL, 's']],//['q3', '1', 'r'], ['q1', $EMPTY_SYMBOL, 'l']
  55.     'q2' => [['q3', '1', 'r'], ['q2', '1', 'l'], ['q2', '*', 'l']],
  56.     'q3' => [['q1', $EMPTY_SYMBOL, 'l'], ['q3', '1', 'r'], ['q3', '*', 'r']]
  57. ];*/
  58.  
  59. $q = 'q1';
  60.  
  61. $count_iter = 0;
  62. //echo $cassette[$position] . ' ' . $q . PHP_EOL;
  63.  
  64. //print_r($table[$q][$cassette[$position]]);
  65.  
  66. while($q != $STOP_CARET_SYMBOL) {
  67.     if($count_iter > 500)
  68.         break;
  69.     $count_iter++;
  70. /*    echo $q . PHP_EOL;
  71.     echo toRow($cassette[$position]) . PHP_EOL;
  72.     exit();*/
  73.     $current = $table[$q][$cassette[$position]];
  74.     $temp_q = $q;
  75.     $temp_cassette = $cassette[$position];
  76.     $cassette[$position] = $current[1];
  77.     $q = $current[0];
  78.     if($current[2] == 'l') {
  79.         $position--;
  80.     }elseif($current[2] == 'r') {
  81.         $position++;
  82.     }
  83.     if($position < 0) {
  84.         $cassette = array_merge([$EMPTY_SYMBOL], $cassette);
  85.         $position = 0;
  86.     }
  87.     if($position > count($cassette))
  88.         $cassette = array_merge( $cassette, [$EMPTY_SYMBOL]);
  89.     echo $count_iter . ') ' . $temp_q . $temp_cassette . '->' . implode('', $current) . PHP_EOL;
  90.     $line = str_create('-', count($cassette)*2+1) . PHP_EOL;
  91.     echo $line;
  92.     echo '|' . implode('|', $cassette) . '|' . PHP_EOL;
  93.     echo $line;
  94. }
  95.  
  96. /*function toRow($name) {
  97.     $rows = [$EMPTY_SYMBOL => 0, '1' => 1, '*' => 2];
  98.     return $rows[$name];
  99. }*/
  100.  
  101. function str_create($symbol, $count) {
  102.     if($count < 2) return $symbol;
  103.     return $symbol . str_create($symbol, $count - 1);
  104. }
  105.  
  106. function getConsoleData($message, &$var) {
  107.     echo $message . ': ';
  108.     $var = trim(fgets(STDIN));
  109.     echo PHP_EOL;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement