예제 #1
0
        //Funcion to validate the string
        private static void introduceString(Turing machine)
        {
            Console.WriteLine("Introduce the string to evaluate: ");
            string tape = Console.ReadLine();

            //In some cases, the Turing Machine needs to check one position after the last character in the string,
            //it is necessary to add a space, otherwise it would throw an IndexOutOfBoundException if there is null
            tape += ' ';
            string pointer = "^";//Head pointer in the machine. Helps to visualize where we are at any moment on the tape

            //Create a new file where it will print each step and movement in the machine
            Console.WriteLine("Name of the file where the movement will be printed: ");
            string file = Console.ReadLine();

            //**********IMPORTANT: VERIFY THE LOCATION OF THE FILE AND REPLACE WITH THE FOLDER WHERE IT WILL BE LOCATED************
            string fileName = file + ".txt";

            using (FileStream fs = File.Create(fileName)) {                        //Creating a new file using FileStream
                Byte[] title = new UTF8Encoding(true).GetBytes("Printing tape\n"); //Printing the head of the new file
                fs.Write(title, 0, title.Length);
            }

            //Recursive function to validate the string
            //Parameters: the first state in the Turing Machine, the string to evaluate, the position in the string and the file name to write on it each state.
            validateTuringMachine(machine.fetchNode(1), tape, 0, pointer, fileName);
        }
예제 #2
0
        //Main function of the Project
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Clear();
                try {//Try-catch block to validate the correct format of entries
                    Console.WriteLine("What would you like to do in the Turing Machine?\n1.Addition\n2.Subtraction");
                    short addSub = short.Parse(Console.ReadLine());

                    if (addSub == 1 || addSub == 2)
                    {
                        Turing machine = new Turing(addSub);//Initialization of Turing Machine

                        while (true)
                        {
                            Console.Clear();
                            try {//Try-catch to validate if the entries are in the valid options
                                Console.WriteLine("Turing Machine created\n1.Print Turing Machine.\n2.Evaluate a string.");
                                short op = short.Parse(Console.ReadLine());

                                if (op == 1)
                                {
                                    machine.printTuringMachine(); //Printing the Turing Machine
                                    introduceString(machine);     //Function call to introduce a string to validate
                                }
                                else if (op == 2)
                                {
                                    introduceString(machine);
                                }
                                else
                                {
                                    throw new Exception(); //Throw exception if the input aren't neither 1 or 2
                                }
                            } catch (Exception e) {        //Catch if the entry isn't a valid option
                                Console.WriteLine("Introduce a valid option.\nPress any key to continue.");
                                Console.ReadKey();
                            }
                        }
                    }
                    else
                    {
                        throw new Exception(); //Throw exception if the input aren't neither 1 or 2
                    }
                } catch (FormatException e) {  //Catch if the entry isn't a number
                    Console.WriteLine("Introduce a valid format.\nPress any key to continue.");
                    Console.ReadKey();
                } catch (Exception e) {//Catch if the entry isn't a valid option
                    Console.WriteLine("Introduce a valid option.\nPress any key to continue.");
                    Console.ReadKey();
                }
            }
        }