Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.System;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Project {
- public static void main(String[] args) throws CloneNotSupportedException {
- Scanner scanner = new Scanner(System.in);
- String encodingString=scanner.nextLine();
- TuringMachine TM = new TuringMachine(encodingString);
- String result=TM.run(TM);
- System.out.println(encodingString.substring(0,encodingString.length()-2-TM.input.length()));
- System.out.println(TM.input);
- System.out.println(TM.transitionList.size());
- System.out.println(TM.stateList.size());
- if(result=="accept") {
- System.out.println("M stops and accepts w");
- }
- else if(result=="reject") {
- System.out.println("M stops and rejects w");
- }
- else {
- System.out.println("M is still running");
- }
- scanner.close();
- }
- }
- class TuringMachine implements Cloneable{
- static State startState;
- static State acceptState;
- static State rejectState;
- ArrayList<State> stateList = new ArrayList<>();
- ArrayList<Transition> transitionList = new ArrayList<>();
- String input;//original string in tape
- StringBuilder tape;
- int tapeHead;// simulates tape head position using the index of the input string
- State currentState;
- Boolean loopMark;
- TuringMachine(String encodingString) {
- int i;
- this.loopMark = false;
- tapeHead=0;
- startState = new State("q0");
- rejectState = new State("qr");
- acceptState = new State("qa");
- this.currentState=new State("q0");
- char[] s = encodingString.toCharArray();// char array of encodingString
- for (i = 0;; i += 13) {
- if (s[i + 1] == '#') {
- this.input = new String(s, i+2, s.length-i-3);
- if(!input.equals("e"))
- this.tape = new StringBuilder(new String(s, i+2, s.length-i-3));
- else
- this.tape = new StringBuilder("_");
- break;
- } else {
- //System.out.println(s+"\n"+i);
- this.transitionList.add(new Transition(new String(s, i + 1, 12)));
- }
- }
- }
- public Object clone()throws CloneNotSupportedException{
- return super.clone();
- }
- Boolean isAcceptState() {
- //System.out.println(this.currentState.name);
- //System.out.println(acceptState.name);
- return (this.currentState.equals(acceptState));
- }
- Boolean isRejectState() {
- return (this.currentState.equals(rejectState));
- }
- String run(TuringMachine tm) throws CloneNotSupportedException {
- return process(tm, null,0);
- }
- String process(TuringMachine tm, Transition t, int depth) throws CloneNotSupportedException {
- if(t != null) {
- tm.currentState = (State)(t.rearState.clone());
- tm.tape.setCharAt(this.tapeHead, t.writeSymbol);
- if (t.tapeHeadDirection == 'R') {
- tm.tapeHead++;
- if(tm.tapeHead==tm.tape.length()) {
- tm.tape.append('_');
- }
- } else {
- if(tapeHead>0)
- tm.tapeHead--;
- }
- }
- else {
- tm.currentState = (State)(TuringMachine.startState.clone());
- tm.tapeHead=0;
- }
- if (depth == 21) {
- return "loop";
- } else if (tm.isAcceptState() == true) {
- return "accept";
- } else if (tm.isRejectState() == true) {
- return "reject";
- }
- for (int i = 0; i < transitionList.size(); i++) {
- if (tm.transitionList.get(i).priorState.equals(tm.currentState)
- && tm.transitionList.get(i).readSymbol == tape.charAt(tm.tapeHead)) {
- String r = process((TuringMachine)(tm.clone()), tm.transitionList.get(i),depth + 1);
- if(r=="accept") {
- return r;
- }
- else if(r=="loop") {
- tm.loopMark=true;
- }
- }
- }
- if(tm.loopMark) {
- return "loop";
- }
- else {
- return "reject";//
- }
- }
- class State implements Cloneable{
- String name;
- State(String a) {
- this.name = a;
- }
- public Object clone()throws CloneNotSupportedException{
- return super.clone();
- }
- @Override
- public boolean equals(Object v) { //ArrayList's custom Contains method
- return this.name.equals(((State)v).name); //The contains method calls the equals method with signature equals(Object)
- }
- }
- class Transition {
- State priorState;
- State rearState;
- char readSymbol;
- char writeSymbol;
- char tapeHeadDirection;
- Transition(String t) {
- //System.out.println(t);
- char[] a = t.toCharArray();
- this.priorState = new State(new String(a, 0, 2));// string between but not include #
- this.rearState = new State(new String(a, 6, 2));
- if(!stateList.contains(priorState)) {
- stateList.add(priorState);
- }
- if(!stateList.contains(rearState)) {
- stateList.add(rearState);
- }
- this.readSymbol = a[3];
- this.writeSymbol = a[9];
- this.tapeHeadDirection = a[11];
- this.tapeHeadDirection=t.charAt(11);
- }
- }
- }
- import java.lang.System;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Project {
- public static void main(String[] args) throws CloneNotSupportedException {
- Scanner scanner = new Scanner(System.in);
- String encodingString=scanner.nextLine();
- TuringMachine TM = new TuringMachine(encodingString);
- String result=TM.run(TM);
- System.out.println(encodingString.substring(0,encodingString.length()-2-TM.input.length()));
- System.out.println(TM.input);
- System.out.println(TM.transitionList.size());
- System.out.println(TM.stateList.size());
- if(result=="accept") {
- System.out.println("M stops and accepts w");
- }
- else if(result=="reject") {
- System.out.println("M stops and rejects w");
- }
- else {
- System.out.println("M is still running");
- }
- scanner.close();
- }
- }
- class TuringMachine implements Cloneable{
- static State startState;
- static State acceptState;
- static State rejectState;
- ArrayList<State> stateList = new ArrayList<>();
- ArrayList<Transition> transitionList = new ArrayList<>();
- String input;//original string in tape
- StringBuilder tape;
- int tapeHead;// simulates tape head position using the index of the input string
- State currentState;
- Boolean loopMark;
- TuringMachine(String encodingString) {
- int i;
- this.loopMark = false;
- tapeHead=0;
- startState = new State("q0");
- rejectState = new State("qr");
- acceptState = new State("qa");
- this.currentState=new State("q0");
- char[] s = encodingString.toCharArray();// char array of encodingString
- for (i = 0;; i += 13) {
- if (s[i + 1] == '#') {
- this.input = new String(s, i+2, s.length-i-3);
- if(!input.equals("e"))
- this.tape = new StringBuilder(new String(s, i+2, s.length-i-3));
- else
- this.tape = new StringBuilder("_");
- break;
- } else {
- //System.out.println(s+"\n"+i);
- this.transitionList.add(new Transition(new String(s, i + 1, 12)));
- }
- }
- }
- public Object clone()throws CloneNotSupportedException{
- return super.clone();
- }
- Boolean isAcceptState() {
- //System.out.println(this.currentState.name);
- //System.out.println(acceptState.name);
- return (this.currentState.equals(acceptState));
- }
- Boolean isRejectState() {
- return (this.currentState.equals(rejectState));
- }
- String run(TuringMachine tm) throws CloneNotSupportedException {
- return process(tm, null,0);
- }
- String process(TuringMachine tm, Transition t, int depth) throws CloneNotSupportedException {
- if(t != null) {
- tm.currentState = (State)(t.rearState.clone());
- tm.tape.setCharAt(this.tapeHead, t.writeSymbol);
- if (t.tapeHeadDirection == 'R') {
- tm.tapeHead++;
- if(tm.tapeHead==tm.tape.length()) {
- tm.tape.append('_');
- }
- } else {
- if(tapeHead>0)
- tm.tapeHead--;
- }
- }
- else {
- tm.currentState = (State)(TuringMachine.startState.clone());
- tm.tapeHead=0;
- }
- if (depth == 21) {
- return "loop";
- } else if (tm.isAcceptState() == true) {
- return "accept";
- } else if (tm.isRejectState() == true) {
- return "reject";
- }
- for (int i = 0; i < transitionList.size(); i++) {
- if (tm.transitionList.get(i).priorState.equals(tm.currentState)
- && tm.transitionList.get(i).readSymbol == tape.charAt(tm.tapeHead)) {
- String r = process((TuringMachine)(tm.clone()), tm.transitionList.get(i),depth + 1);
- if(r=="accept") {
- return r;
- }
- else if(r=="loop") {
- tm.loopMark=true;
- }
- }
- }
- if(tm.loopMark) {
- return "loop";
- }
- else {
- return "reject";//
- }
- }
- class State implements Cloneable{
- String name;
- State(String a) {
- this.name = a;
- }
- public Object clone()throws CloneNotSupportedException{
- return super.clone();
- }
- @Override
- public boolean equals(Object v) { //ArrayList's custom Contains method
- return this.name.equals(((State)v).name); //The contains method calls the equals method with signature equals(Object)
- }
- }
- class Transition {
- State priorState;
- State rearState;
- char readSymbol;
- char writeSymbol;
- char tapeHeadDirection;
- Transition(String t) {
- //System.out.println(t);
- char[] a = t.toCharArray();
- this.priorState = new State(new String(a, 0, 2));// string between but not include #
- this.rearState = new State(new String(a, 6, 2));
- if(!stateList.contains(priorState)) {
- stateList.add(priorState);
- }
- if(!stateList.contains(rearState)) {
- stateList.add(rearState);
- }
- this.readSymbol = a[3];
- this.writeSymbol = a[9];
- this.tapeHeadDirection = a[11];
- this.tapeHeadDirection=t.charAt(11);
- }
- }
- }
Add Comment
Please, Sign In to add comment