Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Turing
- {
- class MainClass
- {
- public static string getlenta(LinkedList<char> lenta)// получаем из списка строку которая сейчас на ленте находится чтобы видеть что происходит
- {
- LinkedListNode<char> node = lenta.First;
- string s = "";
- while (node != null && node.Value == '*')
- {
- node = node.Next;
- }
- while (node != null)
- {
- s += node.Value;
- node = node.Next;
- }
- while (s.Length > 0 && s[s.Length - 1] == ' ')
- s = s.Substring(0, s.Length - 1);
- return s;
- }
- public static void Main(string[] args)
- {
- LinkedList<char> lenta = new LinkedList<char>();
- string inputword = Console.ReadLine();// нужно ввести начальное слово
- for (int i = 0; i < inputword.Length; i++)
- {
- lenta.AddLast(inputword[i]); // заносим на ленту слово
- }
- int n = int.Parse(Console.ReadLine()); // количество состояний
- Dictionary<KeyValuePair<int, char>, KeyValuePair<KeyValuePair<int, char>, char>> dict = new Dictionary<KeyValuePair<int, char>, KeyValuePair<KeyValuePair<int, char>, char>>();
- for (int i = 0; i < n; i++)
- {
- // вводим состояния вида
- //"номер состояния" "какой символ" "в какое состояние переходим" "какой символ надо записать" "куда идет головка(влево вправо или закончить работу)"
- // например 1 a 2 b L (пустой символ нужно обозначать звездочкой)
- string[] s = Console.ReadLine().Split();
- int state = int.Parse(s[0]); //
- char symbol = s[1][0];
- int newstate = int.Parse(s[2]);
- char newsymbol = s[3][0];
- char turn = s[4][0];
- KeyValuePair<int, char> key = new KeyValuePair<int, char>(state,symbol);
- KeyValuePair<KeyValuePair<int, char>, char> val = new KeyValuePair<KeyValuePair<int, char>, char>(new KeyValuePair<int, char>(newstate, newsymbol), turn);
- dict[key] = val;
- // засовываем правила перехода из состояния по символу в другое состояние в словарь
- }
- int curstate = 0; // начальное состояние. Сделал всегда равным 0. можно заменить на ввод с клавиатуры
- LinkedListNode<char> curnode = lenta.First;
- while (true)
- {
- char symbol = curnode.Value; // символ на который смотрит головка
- KeyValuePair<int, char> key = new KeyValuePair<int, char>(curstate, symbol);
- if (!dict.ContainsKey(key))
- {
- Console.WriteLine("Такого перехода не существует");
- return;
- }
- KeyValuePair<KeyValuePair<int, char>, char> val = dict[key];// получаем то что надо сделать при текущем состоянии и символе
- curnode.Value = val.Key.Value; // меняем значение на новое
- curstate = val.Key.Key; // меняем состояние
- char turn = val.Value; // тут смотрим куда идти.
- Console.WriteLine(getlenta(lenta)); // печатаем как изменилась лента
- if (turn == 'L') // если влево то
- {
- if (curnode.Previous == null) // если влево некуда идти создаем элемент с пустым символом(звездочка типо пустой символ)
- {
- lenta.AddBefore(curnode, new LinkedListNode<char>('*'));
- }
- curnode = curnode.Previous; // идем влево
- }
- else
- if (turn == 'R') // тоже самое но вправо
- {
- if (curnode.Next == null)
- {
- lenta.AddAfter(curnode, new LinkedListNode<char>('*'));
- }
- curnode = curnode.Next;
- }
- else
- break;// иначе заканчиваем работу
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement