Esempio n. 1
0
        private void btExecute_Click(object sender, EventArgs e)
        {
            string selectedMachine = comboBox1.GetItemText(comboBox1.SelectedItem);

            // fill the data grid view
            char[] input = txtInput.Text.ToCharArray();
            int    position;
            int    count;

            for (int i = 0; i < input.Length; i++)
            {
                DGVTuring[i, 0].Value = input[i];
            }
            switch (selectedMachine)
            {
            case "Palíndromo":
                TuringMachinePalin palindrome = new TuringMachinePalin();
                position           = 0;
                count              = 0;
                lblStepsCount.Text = count.ToString();
                DGVTuring.Rows[0].Cells[0].Style.BackColor = Color.Red;
                if (palindrome.evaluateInput(input))
                {
                    for (int i = 0; i < palindrome.headMovements.Count; i++)
                    {
                        if (palindrome.headMovements[i] == 'R')
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position++;
                            DGVTuring[i, 0].Value = palindrome.tape[i];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        else
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position--;
                            if (i < palindrome.tape.Count)
                            {
                                DGVTuring[i, 0].Value = palindrome.tape[i];
                            }
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        lblState.Text = palindrome.history[i];
                        count++;
                        lblStepsCount.Text = count.ToString();
                        System.Threading.Thread.Sleep(1000);
                        this.Refresh();
                    }
                    lblState.Text = "Aceptado";
                }
                break;

            case "Multiplicación Unario":
                TuringMachineMult mult = new TuringMachineMult();
                position           = 0;
                count              = 0;
                lblStepsCount.Text = count.ToString();
                DGVTuring.Rows[0].Cells[0].Style.BackColor = Color.Red;
                if (mult.evaluateInput(input))
                {
                    for (int i = 0; i < mult.headMovements.Count; i++)
                    {
                        if (mult.headMovements[i] == 'R')
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position++;
                            DGVTuring[i, 0].Value = mult.tape[position];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        else
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position--;
                            //if (i < mult.tape.Count)
                            DGVTuring[i, 0].Value = mult.tape[position];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        lblState.Text = mult.history[i];
                        count++;
                        lblStepsCount.Text = count.ToString();
                        System.Threading.Thread.Sleep(1000);
                        this.Refresh();
                    }
                    lblState.Text = "Aceptado";
                }
                break;

            case "Suma Unario":
                TuringMachineSum sum = new TuringMachineSum();
                position           = 0;
                count              = 0;
                lblStepsCount.Text = count.ToString();
                DGVTuring.Rows[0].Cells[0].Style.BackColor = Color.Red;
                if (sum.evaluateInput(input))
                {
                    for (int i = 0; i < sum.headMovements.Count; i++)
                    {
                        if (sum.headMovements[i] == 'R')
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position++;
                            DGVTuring[i, 0].Value = sum.tape[position];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        else
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position--;
                            DGVTuring[i, 0].Value = sum.tape[position];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        lblState.Text = sum.history[i];
                        count++;
                        lblStepsCount.Text = count.ToString();
                        System.Threading.Thread.Sleep(1000);
                        this.Refresh();
                    }
                }
                lblState.Text = "Aceptado";
                break;

            case "Resta Unario":
                TuringMachineSub sub = new TuringMachineSub();
                position           = 0;
                count              = 0;
                lblStepsCount.Text = count.ToString();
                DGVTuring.Rows[0].Cells[0].Style.BackColor = Color.Red;
                if (sub.evaluateInput(input))
                {
                    for (int i = 0; i < sub.headMovements.Count; i++)
                    {
                        if (sub.headMovements[i] == 'R')
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position++;
                            DGVTuring[i, 0].Value = sub.tape[position];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        else
                        {
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.White;
                            position--;
                            //if (i < mult.tape.Count)
                            DGVTuring[i, 0].Value = sub.tape[position];
                            DGVTuring.Rows[0].Cells[position].Style.BackColor = Color.Red;
                        }
                        lblState.Text = sub.history[i];
                        count++;
                        lblStepsCount.Text = count.ToString();
                        System.Threading.Thread.Sleep(1000);
                        this.Refresh();
                    }
                    lblState.Text = "Aceptado";
                }
                break;
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Selecciones la maquina a utilizar");
            Console.WriteLine("1. Suma Unaria");
            Console.WriteLine("2. Resta Unaria");
            Console.WriteLine("3. Multiplicacion Unaria");
            Console.WriteLine("4. Palindromo");
            Console.WriteLine("5. Copia de patron");
            int choice = int.Parse(Console.ReadLine());

            Console.WriteLine("Ingrese la cadena");
            if (choice == 1)
            {
                string           input = Console.ReadLine();
                TuringMachineSum sum   = new TuringMachineSum();
                if (sum.evaluateInput(input.ToCharArray()))
                {
                    // write the tape
                    for (int i = 0; i < sum.tape.Count; i++)
                    {
                        Console.Write(sum.tape[i] + " ");
                    }
                    Console.WriteLine();
                    // write the movements
                    for (int i = 0; i < sum.headMovements.Count; i++)
                    {
                        Console.Write(sum.headMovements[i] + " ");
                    }
                    Console.WriteLine();
                    for (int i = 0; i < sum.history.Count; i++)
                    {
                        Console.Write(sum.history[i] + " ");
                    }
                }
            }
            if (choice == 2)
            {
                string           input = Console.ReadLine();
                TuringMachineSub sub   = new TuringMachineSub();
                if (sub.evaluateInput(input.ToCharArray()))
                {
                    // write the tape
                    for (int i = 0; i < sub.tape.Count; i++)
                    {
                        Console.Write(sub.tape[i] + " ");
                    }
                    Console.WriteLine();
                    // write the movements
                    for (int i = 0; i < sub.headMovements.Count; i++)
                    {
                        Console.Write(sub.headMovements[i] + " ");
                    }
                    Console.WriteLine();
                    for (int i = 0; i < sub.history.Count; i++)
                    {
                        Console.Write(sub.history[i] + " ");
                    }
                }
            }
            if (choice == 3)
            {
                string            input = Console.ReadLine();
                TuringMachineMult mult  = new TuringMachineMult();
                if (mult.evaluateInput(input.ToCharArray()))
                {
                    // write the tape
                    for (int i = 0; i < mult.tape.Count; i++)
                    {
                        Console.Write(mult.tape[i] + " ");
                    }
                    Console.WriteLine();
                    // write the movements
                    for (int i = 0; i < mult.headMovements.Count; i++)
                    {
                        Console.Write(mult.headMovements[i] + " ");
                    }
                    Console.WriteLine();
                    for (int i = 0; i < mult.history.Count; i++)
                    {
                        Console.Write(mult.history[i] + " ");
                    }
                }
            }
            if (choice == 4)
            {
                string             input = Console.ReadLine();
                TuringMachinePalin mult  = new TuringMachinePalin();
                if (mult.evaluateInput(input.ToCharArray()))
                {
                    // write the tape
                    for (int i = 0; i < mult.tape.Count; i++)
                    {
                        Console.Write(mult.tape[i] + " ");
                    }
                    Console.WriteLine();
                    // write the movements
                    for (int i = 0; i < mult.headMovements.Count; i++)
                    {
                        Console.Write(mult.headMovements[i] + " ");
                    }
                    Console.WriteLine();
                    for (int i = 0; i < mult.history.Count; i++)
                    {
                        Console.Write(mult.history[i] + " ");
                    }
                    if (mult.returnState())
                    {
                        Console.WriteLine("Aceptado");
                    }
                }
            }
            if (choice == 5)
            {
                string            input = Console.ReadLine();
                TuringMachineCopy mult  = new TuringMachineCopy();
                if (mult.evaluateInput(input.ToCharArray()))
                {
                    // write the tape
                    for (int i = 0; i < mult.tape.Count; i++)
                    {
                        Console.Write(mult.tape[i] + " ");
                    }
                    Console.WriteLine();
                    // write the movements
                    for (int i = 0; i < mult.headMovements.Count; i++)
                    {
                        Console.Write(mult.headMovements[i] + " ");
                    }
                    Console.WriteLine();
                    for (int i = 0; i < mult.history.Count; i++)
                    {
                        Console.Write(mult.history[i] + " ");
                    }
                    if (mult.returnState())
                    {
                        Console.WriteLine("Aceptado");
                    }
                }
            }

            Console.ReadKey();
        }