Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Turing
  5. {
  6.  
  7.  
  8.  
  9. class MainClass
  10. {
  11. public static string getlenta(LinkedList<char> lenta)// получаем из списка строку которая сейчас на ленте находится чтобы видеть что происходит
  12. {
  13. LinkedListNode<char> node = lenta.First;
  14. string s = "";
  15. while (node != null && node.Value == '*')
  16. {
  17. node = node.Next;
  18. }
  19. while (node != null)
  20. {
  21.  
  22. s += node.Value;
  23. node = node.Next;
  24. }
  25. while (s.Length > 0 && s[s.Length - 1] == ' ')
  26. s = s.Substring(0, s.Length - 1);
  27. return s;
  28. }
  29. public static void Main(string[] args)
  30. {
  31. LinkedList<char> lenta = new LinkedList<char>();
  32. string inputword = Console.ReadLine();// нужно ввести начальное слово
  33. for (int i = 0; i < inputword.Length; i++)
  34. {
  35. lenta.AddLast(inputword[i]); // заносим на ленту слово
  36. }
  37. int n = int.Parse(Console.ReadLine()); // количество состояний
  38.  
  39. Dictionary<KeyValuePair<int, char>, KeyValuePair<KeyValuePair<int, char>, char>> dict = new Dictionary<KeyValuePair<int, char>, KeyValuePair<KeyValuePair<int, char>, char>>();
  40.  
  41. for (int i = 0; i < n; i++)
  42. {
  43. // вводим состояния вида
  44. //"номер состояния" "какой символ" "в какое состояние переходим" "какой символ надо записать" "куда идет головка(влево вправо или закончить работу)"
  45. // например 1 a 2 b L (пустой символ нужно обозначать звездочкой)
  46. string[] s = Console.ReadLine().Split();
  47. int state = int.Parse(s[0]); //
  48. char symbol = s[1][0];
  49. int newstate = int.Parse(s[2]);
  50. char newsymbol = s[3][0];
  51. char turn = s[4][0];
  52. KeyValuePair<int, char> key = new KeyValuePair<int, char>(state,symbol);
  53. KeyValuePair<KeyValuePair<int, char>, char> val = new KeyValuePair<KeyValuePair<int, char>, char>(new KeyValuePair<int, char>(newstate, newsymbol), turn);
  54. dict[key] = val;
  55. // засовываем правила перехода из состояния по символу в другое состояние в словарь
  56. }
  57.  
  58. int curstate = 0; // начальное состояние. Сделал всегда равным 0. можно заменить на ввод с клавиатуры
  59. LinkedListNode<char> curnode = lenta.First;
  60.  
  61. while (true)
  62. {
  63. char symbol = curnode.Value; // символ на который смотрит головка
  64. KeyValuePair<int, char> key = new KeyValuePair<int, char>(curstate, symbol);
  65. if (!dict.ContainsKey(key))
  66. {
  67. Console.WriteLine("Такого перехода не существует");
  68. return;
  69. }
  70. KeyValuePair<KeyValuePair<int, char>, char> val = dict[key];// получаем то что надо сделать при текущем состоянии и символе
  71.  
  72. curnode.Value = val.Key.Value; // меняем значение на новое
  73. curstate = val.Key.Key; // меняем состояние
  74. char turn = val.Value; // тут смотрим куда идти.
  75. Console.WriteLine(getlenta(lenta)); // печатаем как изменилась лента
  76. if (turn == 'L') // если влево то
  77. {
  78. if (curnode.Previous == null) // если влево некуда идти создаем элемент с пустым символом(звездочка типо пустой символ)
  79. {
  80. lenta.AddBefore(curnode, new LinkedListNode<char>('*'));
  81. }
  82. curnode = curnode.Previous; // идем влево
  83. }
  84. else
  85. if (turn == 'R') // тоже самое но вправо
  86. {
  87. if (curnode.Next == null)
  88. {
  89. lenta.AddAfter(curnode, new LinkedListNode<char>('*'));
  90. }
  91. curnode = curnode.Next;
  92. }
  93. else
  94. break;// иначе заканчиваем работу
  95. }
  96.  
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement