Computer Science
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 


Rechercher Advanced Search

Keywords

Latest topics
» (2011Q2) ??????? ?????? ?41? ???????????!/????????????!?(TX 1280x720 X264 AAC).mp4
Turing Machine EmptySat Feb 15, 2014 10:12 am by doriperni

» Sync2 V2.20.1312 X86x64.rar.rar
Turing Machine EmptyFri Feb 14, 2014 7:37 pm by carmibire

» 9.1 Inheritance Hierarchies
Turing Machine EmptyTue Feb 26, 2013 12:50 pm by ThienDinh

» Useful website
Turing Machine EmptySat Dec 08, 2012 6:52 pm by tpham1991

» The Machine that Changed the World: Giant Brains. 1992 Documentary
Turing Machine EmptySat Dec 08, 2012 2:16 am by ThienDinh

» Problems in practice test.
Turing Machine EmptyThu Dec 06, 2012 2:32 pm by ThienDinh

» Newton's Method
Turing Machine EmptyMon Dec 03, 2012 8:32 pm by ThienDinh

» Console Calc
Turing Machine EmptySun Dec 02, 2012 5:57 pm by Petahwil

» Law Of Cosines
Turing Machine EmptySat Dec 01, 2012 4:54 pm by Petahwil

May 2024
SunMonTueWedThuFriSat
   1234
567891011
12131415161718
19202122232425
262728293031 

Calendar Calendar


Turing Machine

Go down

Turing Machine Empty Turing Machine

Post by ThienDinh Fri Nov 30, 2012 7:40 pm

I just tried to write Java Code for Turing machine. See if anyone interested
alien alien alien alien alien alien

TuringMachine.java
Code:

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author ThienDinh
 */
public class TuringMachine {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
        ArrayList<State> states = new ArrayList<>();
        ArrayList<String> inputs = new ArrayList<>();
        boolean done = false;
        System.out.println("Give the determining input: ");
        do
        {
            String inpu = in.next();
            if (inpu.equals("end"))
            {
                break;
            } else
            {
                inputs.add(inpu);
            }
        } while (true);
       
        System.out.print("How many possible state: ");
        int totalState = in.nextInt();
        for (int j = 0; j < totalState; j++)
        {
            states.add(new State(inputs));
            System.out.println("State s" + j + ":");
            for (int i = 0; i < inputs.size(); i++)
            {
                System.out.print("Input next state: ");
                states.get(j).nextStates.add(in.nextInt());
            }
            for (int i = 0; i < inputs.size(); i++)
            {
                System.out.print("Input output: ");
                states.get(j).outPuts.add(in.next());
            }
                for (int i = 0; i < inputs.size(); i++)
            {
                System.out.print("Input movement: ");
                states.get(j).move.add(in.next());
            }
        }
        System.out.print("Print out\n");
        printOut(states);
        System.out.println("Input single one:");
        ArrayList<String> inp = new ArrayList<>();
        do
        {
            String inpu = in.next();
            if (inpu.equals("end"))
            {
                break;
            } else
            {
                inp.add(inpu);
            }
        } while (true);       
        modifyTape(states, inp);
    }
   
    public static void printOut(ArrayList<State> states)
    {
        for (int i = 0; i < states.size(); i++)
        {
            for (int j = 0; j < states.get(i).nextStates.size(); j++)
            {
                System.out.print(" " + states.get(i).nextStates.get(j));
            }
            for (int j = 0; j < states.get(i).nextStates.size(); j++)
            {
                System.out.print(" " + states.get(i).outPuts.get(j));
            }
            System.out.println();
        }
    }
   
    public static String getOutput(ArrayList<State> states, ArrayList<String> input)
    {
        int index = 0;
        String output = "";
        for (int i = 0; i < input.size(); i++)
        {
            output += states.get(index).outPut(input.get(i));
            index = states.get(index).nextState(input.get(i));
        }
        return output;
    }
   
    public static void modifyTape(ArrayList<State> states, ArrayList<String> tape)
    {
        int index = 0;
        int tapIndex = 0;
        // Search for the first available symbol
        for (int i = 0; i < tape.size(); i++)
        {
            if (!tape.get(i).equals("B"))
            {
                tapIndex = i;
                break;
            }
        }
       
        //Do the process until machine halt
        while (true)
        {
            try
            {
                String cell = tape.get(tapIndex);
                System.out.println("TAPE: " + tape + " TAP INDEX: " + tapIndex);
                // Replace the cell at position tapIndex to the output of the state which gets input from that cell
                tape.set(tapIndex, states.get(index).outPut(cell));
                if(states.get(index).move(cell).equals("R"))
                {
                    tapIndex++;
                }else if(states.get(index).move(cell).equals("L"))
                {
                    tapIndex--;
                }
                index = states.get(index).nextState(cell);
                if(index == -1)
                {
                    throw new Exception();
                }
            } catch (Exception ex)
            {
                System.out.println("Machine Halt at " + tapIndex);
                break;
            }
        }
    }
}

State.java
Code:


import java.util.ArrayList;

/**
 *
 * @author ThienDinh
 */
/**
 * This class is just for a single state. It can access its next states and
 * output determined by set up input
 *
 * @author ThienDinh
 */
public class State {

  public ArrayList<String> inputTypes;
  public ArrayList<Integer> nextStates;
  public ArrayList<String> outPuts;
  public ArrayList<String> move;

    public State(ArrayList<String>  inputTypes)
    {
        this.inputTypes = inputTypes;
        nextStates = new ArrayList<>();
        outPuts = new ArrayList<>();
        move = new ArrayList<>();
    }

    /**
    * Return the index of the next state
    *
    * @param state
    * @param input
    * @return the index of the next state
    */
    public int nextState(String input)
    {
        for (int i = 0; i < inputTypes.size(); i++)
        {
            if (inputTypes.get(i).equals(input))
            {
                return nextStates.get(i);
            }
        }
        return  -1;
    }

    /**
    * Return the output of the state
    *
    * @param state
    * @param input
    * @return the output of the state
    */
    public String outPut(String input)
    {
        for (int i = 0; i < inputTypes.size(); i++)
        {
            if (inputTypes.get(i).equals(input))
            {
                return outPuts.get(i);
            }
        }
        return "";
    }
   
    public String move(String input)
    {
        for (int i = 0; i < inputTypes.size(); i++)
        {
            if (inputTypes.get(i).equals(input))
            {
                return move.get(i);
            }
        }
        return "";
    }
}

ThienDinh
Admin

Posts : 27
Join date : 2012-11-30

https://computerscienceqc.board-directory.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum