Guest User

Untitled

a guest
Dec 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. import java.lang.System;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class Project {
  6. public static void main(String[] args) throws CloneNotSupportedException {
  7. Scanner scanner = new Scanner(System.in);
  8. String encodingString=scanner.nextLine();
  9. TuringMachine TM = new TuringMachine(encodingString);
  10. String result=TM.run(TM);
  11.  
  12. System.out.println(encodingString.substring(0,encodingString.length()-2-TM.input.length()));
  13. System.out.println(TM.input);
  14. System.out.println(TM.transitionList.size());
  15. System.out.println(TM.stateList.size());
  16. if(result=="accept") {
  17. System.out.println("M stops and accepts w");
  18. }
  19. else if(result=="reject") {
  20. System.out.println("M stops and rejects w");
  21. }
  22. else {
  23. System.out.println("M is still running");
  24. }
  25. scanner.close();
  26. }
  27.  
  28. }
  29.  
  30. class TuringMachine implements Cloneable{
  31. static State startState;
  32. static State acceptState;
  33. static State rejectState;
  34. ArrayList<State> stateList = new ArrayList<>();
  35. ArrayList<Transition> transitionList = new ArrayList<>();
  36. String input;//original string in tape
  37. StringBuilder tape;
  38. int tapeHead;// simulates tape head position using the index of the input string
  39. State currentState;
  40. Boolean loopMark;
  41.  
  42. TuringMachine(String encodingString) {
  43. int i;
  44. this.loopMark = false;
  45. tapeHead=0;
  46. startState = new State("q0");
  47. rejectState = new State("qr");
  48. acceptState = new State("qa");
  49. this.currentState=new State("q0");
  50. char[] s = encodingString.toCharArray();// char array of encodingString
  51.  
  52. for (i = 0;; i += 13) {
  53. if (s[i + 1] == '#') {
  54. this.input = new String(s, i+2, s.length-i-3);
  55. if(!input.equals("e"))
  56. this.tape = new StringBuilder(new String(s, i+2, s.length-i-3));
  57. else
  58. this.tape = new StringBuilder("_");
  59. break;
  60. } else {
  61. //System.out.println(s+"\n"+i);
  62. this.transitionList.add(new Transition(new String(s, i + 1, 12)));
  63. }
  64. }
  65. }
  66.  
  67. public Object clone()throws CloneNotSupportedException{
  68. return super.clone();
  69. }
  70.  
  71. Boolean isAcceptState() {
  72. //System.out.println(this.currentState.name);
  73. //System.out.println(acceptState.name);
  74. return (this.currentState.equals(acceptState));
  75. }
  76.  
  77. Boolean isRejectState() {
  78. return (this.currentState.equals(rejectState));
  79. }
  80.  
  81. String run(TuringMachine tm) throws CloneNotSupportedException {
  82. return process(tm, null,0);
  83. }
  84.  
  85. String process(TuringMachine tm, Transition t, int depth) throws CloneNotSupportedException {
  86. if(t != null) {
  87. tm.currentState = (State)(t.rearState.clone());
  88. tm.tape.setCharAt(this.tapeHead, t.writeSymbol);
  89. if (t.tapeHeadDirection == 'R') {
  90. tm.tapeHead++;
  91. if(tm.tapeHead==tm.tape.length()) {
  92. tm.tape.append('_');
  93. }
  94. } else {
  95. if(tapeHead>0)
  96. tm.tapeHead--;
  97. }
  98. }
  99. else {
  100. tm.currentState = (State)(TuringMachine.startState.clone());
  101. tm.tapeHead=0;
  102. }
  103.  
  104. if (depth == 21) {
  105. return "loop";
  106. } else if (tm.isAcceptState() == true) {
  107. return "accept";
  108. } else if (tm.isRejectState() == true) {
  109. return "reject";
  110. }
  111.  
  112. for (int i = 0; i < transitionList.size(); i++) {
  113. if (tm.transitionList.get(i).priorState.equals(tm.currentState)
  114. && tm.transitionList.get(i).readSymbol == tape.charAt(tm.tapeHead)) {
  115. String r = process((TuringMachine)(tm.clone()), tm.transitionList.get(i),depth + 1);
  116.  
  117. if(r=="accept") {
  118. return r;
  119. }
  120. else if(r=="loop") {
  121. tm.loopMark=true;
  122. }
  123. }
  124. }
  125. if(tm.loopMark) {
  126. return "loop";
  127. }
  128. else {
  129. return "reject";//
  130. }
  131. }
  132.  
  133. class State implements Cloneable{
  134. String name;
  135.  
  136. State(String a) {
  137. this.name = a;
  138. }
  139.  
  140. public Object clone()throws CloneNotSupportedException{
  141. return super.clone();
  142. }
  143.  
  144. @Override
  145. public boolean equals(Object v) { //ArrayList's custom Contains method
  146. return this.name.equals(((State)v).name); //The contains method calls the equals method with signature equals(Object)
  147. }
  148. }
  149.  
  150. class Transition {
  151. State priorState;
  152. State rearState;
  153. char readSymbol;
  154. char writeSymbol;
  155. char tapeHeadDirection;
  156.  
  157. Transition(String t) {
  158. //System.out.println(t);
  159. char[] a = t.toCharArray();
  160. this.priorState = new State(new String(a, 0, 2));// string between but not include #
  161. this.rearState = new State(new String(a, 6, 2));
  162.  
  163. if(!stateList.contains(priorState)) {
  164. stateList.add(priorState);
  165. }
  166. if(!stateList.contains(rearState)) {
  167. stateList.add(rearState);
  168. }
  169. this.readSymbol = a[3];
  170. this.writeSymbol = a[9];
  171. this.tapeHeadDirection = a[11];
  172. this.tapeHeadDirection=t.charAt(11);
  173. }
  174.  
  175. }
  176.  
  177. }
  178. import java.lang.System;
  179. import java.util.ArrayList;
  180. import java.util.Scanner;
  181.  
  182. public class Project {
  183. public static void main(String[] args) throws CloneNotSupportedException {
  184. Scanner scanner = new Scanner(System.in);
  185. String encodingString=scanner.nextLine();
  186. TuringMachine TM = new TuringMachine(encodingString);
  187. String result=TM.run(TM);
  188.  
  189. System.out.println(encodingString.substring(0,encodingString.length()-2-TM.input.length()));
  190. System.out.println(TM.input);
  191. System.out.println(TM.transitionList.size());
  192. System.out.println(TM.stateList.size());
  193. if(result=="accept") {
  194. System.out.println("M stops and accepts w");
  195. }
  196. else if(result=="reject") {
  197. System.out.println("M stops and rejects w");
  198. }
  199. else {
  200. System.out.println("M is still running");
  201. }
  202. scanner.close();
  203. }
  204.  
  205. }
  206.  
  207. class TuringMachine implements Cloneable{
  208. static State startState;
  209. static State acceptState;
  210. static State rejectState;
  211. ArrayList<State> stateList = new ArrayList<>();
  212. ArrayList<Transition> transitionList = new ArrayList<>();
  213. String input;//original string in tape
  214. StringBuilder tape;
  215. int tapeHead;// simulates tape head position using the index of the input string
  216. State currentState;
  217. Boolean loopMark;
  218.  
  219. TuringMachine(String encodingString) {
  220. int i;
  221. this.loopMark = false;
  222. tapeHead=0;
  223. startState = new State("q0");
  224. rejectState = new State("qr");
  225. acceptState = new State("qa");
  226. this.currentState=new State("q0");
  227. char[] s = encodingString.toCharArray();// char array of encodingString
  228.  
  229. for (i = 0;; i += 13) {
  230. if (s[i + 1] == '#') {
  231. this.input = new String(s, i+2, s.length-i-3);
  232. if(!input.equals("e"))
  233. this.tape = new StringBuilder(new String(s, i+2, s.length-i-3));
  234. else
  235. this.tape = new StringBuilder("_");
  236. break;
  237. } else {
  238. //System.out.println(s+"\n"+i);
  239. this.transitionList.add(new Transition(new String(s, i + 1, 12)));
  240. }
  241. }
  242. }
  243.  
  244. public Object clone()throws CloneNotSupportedException{
  245. return super.clone();
  246. }
  247.  
  248. Boolean isAcceptState() {
  249. //System.out.println(this.currentState.name);
  250. //System.out.println(acceptState.name);
  251. return (this.currentState.equals(acceptState));
  252. }
  253.  
  254. Boolean isRejectState() {
  255. return (this.currentState.equals(rejectState));
  256. }
  257.  
  258. String run(TuringMachine tm) throws CloneNotSupportedException {
  259. return process(tm, null,0);
  260. }
  261.  
  262. String process(TuringMachine tm, Transition t, int depth) throws CloneNotSupportedException {
  263. if(t != null) {
  264. tm.currentState = (State)(t.rearState.clone());
  265. tm.tape.setCharAt(this.tapeHead, t.writeSymbol);
  266. if (t.tapeHeadDirection == 'R') {
  267. tm.tapeHead++;
  268. if(tm.tapeHead==tm.tape.length()) {
  269. tm.tape.append('_');
  270. }
  271. } else {
  272. if(tapeHead>0)
  273. tm.tapeHead--;
  274. }
  275. }
  276. else {
  277. tm.currentState = (State)(TuringMachine.startState.clone());
  278. tm.tapeHead=0;
  279. }
  280.  
  281. if (depth == 21) {
  282. return "loop";
  283. } else if (tm.isAcceptState() == true) {
  284. return "accept";
  285. } else if (tm.isRejectState() == true) {
  286. return "reject";
  287. }
  288.  
  289. for (int i = 0; i < transitionList.size(); i++) {
  290. if (tm.transitionList.get(i).priorState.equals(tm.currentState)
  291. && tm.transitionList.get(i).readSymbol == tape.charAt(tm.tapeHead)) {
  292. String r = process((TuringMachine)(tm.clone()), tm.transitionList.get(i),depth + 1);
  293.  
  294. if(r=="accept") {
  295. return r;
  296. }
  297. else if(r=="loop") {
  298. tm.loopMark=true;
  299. }
  300. }
  301. }
  302. if(tm.loopMark) {
  303. return "loop";
  304. }
  305. else {
  306. return "reject";//
  307. }
  308. }
  309.  
  310. class State implements Cloneable{
  311. String name;
  312.  
  313. State(String a) {
  314. this.name = a;
  315. }
  316.  
  317. public Object clone()throws CloneNotSupportedException{
  318. return super.clone();
  319. }
  320.  
  321. @Override
  322. public boolean equals(Object v) { //ArrayList's custom Contains method
  323. return this.name.equals(((State)v).name); //The contains method calls the equals method with signature equals(Object)
  324. }
  325. }
  326.  
  327. class Transition {
  328. State priorState;
  329. State rearState;
  330. char readSymbol;
  331. char writeSymbol;
  332. char tapeHeadDirection;
  333.  
  334. Transition(String t) {
  335. //System.out.println(t);
  336. char[] a = t.toCharArray();
  337. this.priorState = new State(new String(a, 0, 2));// string between but not include #
  338. this.rearState = new State(new String(a, 6, 2));
  339.  
  340. if(!stateList.contains(priorState)) {
  341. stateList.add(priorState);
  342. }
  343. if(!stateList.contains(rearState)) {
  344. stateList.add(rearState);
  345. }
  346. this.readSymbol = a[3];
  347. this.writeSymbol = a[9];
  348. this.tapeHeadDirection = a[11];
  349. this.tapeHeadDirection=t.charAt(11);
  350. }
  351.  
  352. }
  353.  
  354. }
Add Comment
Please, Sign In to add comment