From 1ae631fcc80757dc75df4ad03c914627d237e54a Mon Sep 17 00:00:00 2001 From: overflowerror Date: Thu, 3 Jun 2021 21:26:22 +0200 Subject: [PATCH] added input/output display --- turing/Number.java | 86 ++++++++++++++++++++++++++++++++++++++- turing/Tape.java | 28 +++++++++++++ turing/TuringMachine.java | 3 ++ 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/turing/Number.java b/turing/Number.java index cb75783..f75ef54 100644 --- a/turing/Number.java +++ b/turing/Number.java @@ -1,5 +1,5 @@ public class Number { - private class NumberWrapper { + private static class NumberWrapper { Number n; } @@ -26,6 +26,11 @@ public class Number { public void executeIfEquals(Number n, Action a) { n.executeIfZero(a); } + + @Override + public void executeIfNotEquals(Number n, Action a) { + n.executeIfNotZero(a); + } } static class NegativeNumber extends Number { @@ -87,6 +92,17 @@ public class Number { }); } + @Override + + public void executeIfNotEquals(Number n, Action a) { + n.executeIfNegative(() -> { + positive.executeIfNotEquals(((NegativeNumber) n).positive, a); + }); + n.executeIfNotNegative(() -> { + a.execute(); + }); + } + @Override public void forEach(Action a) { positive.forEach(a); @@ -145,6 +161,66 @@ public class Number { return w.n; } + public static Number max(Number n1, Number n2) { + NumberWrapper w = new NumberWrapper(); + + n1.executeIfNegative(() -> { + n2.executeIfNegative(() -> { + w.n = Number.max(((NegativeNumber) n2).positive, ((NegativeNumber) n1).positive); + }); + n2.executeIfNotNegative(() -> { + w.n = n2; + }); + }); + n1.executeIfNotNegative(() -> { + n2.executeIfNegative(() -> { + w.n = n1; + }); + n2.executeIfNotNegative(() -> { + n1.executeIfZero(() -> { + n2.executeIfZero(() -> { + w.n = zero(); + }); + n2.executeIfNotZero(() -> { + w.n = n2; + }); + }); + n1.executeIfNotZero(() -> { + n2.executeIfZero(() -> { + w.n = n1; + }); + n2.executeIfNotZero(() -> { + Number tmp = Number.max(n1.pre, n2.pre); + tmp.executeIfEquals(n1.pre, () -> { + w.n = n1; + }); + tmp.executeIfEquals(n2.pre, () -> { + w.n = n2; + }); + }); + }); + }); + }); + + return w.n; + } + + public static Number min(Number n1, Number n2) { + Number max = max(n1, n2); + + NumberWrapper w = new NumberWrapper(); + + n1.executeIfEquals(max, () -> { + w.n = n2; + }); + + n2.executeIfEquals(max, () -> { + w.n = n1; + }); + + return w.n; + } + public void executeIfZero(Action action) { } @@ -164,6 +240,14 @@ public class Number { }); } + public void executeIfNotEquals(Number n, Action a) { + n.executeIfNotNegative(() -> { + n.executeIfNotZero(() -> { + pre.executeIfNotEquals(n.pre, a); + }); + }); + } + public void executeIfNegative(Action a) { } diff --git a/turing/Tape.java b/turing/Tape.java index 15a3c5a..23f34a3 100644 --- a/turing/Tape.java +++ b/turing/Tape.java @@ -3,6 +3,10 @@ public class Tape { private class CharWrapper { char c; } + + private class StringWrapper { + String s; + } @FunctionalInterface private interface Cell { @@ -10,9 +14,13 @@ public class Tape { } private Cell cells; + private Number min; + private Number max; public Tape() { cells = (i) -> ' '; + min = Number.zero(); + max = Number.zero(); } public char get(Number i) { @@ -22,6 +30,9 @@ public class Tape { public void set(Number i, char c) { final Cell _cells = cells; + + min = Number.min(min, i); + max = Number.max(max, i); cells = (j) -> { CharWrapper w = new CharWrapper(); @@ -35,4 +46,21 @@ public class Tape { return w.c; }; } + + private String cellString(Number min, Number max) { + StringWrapper w = new StringWrapper(); + w.s = ""; + + w.s = String.valueOf(get(min)); + + min.executeIfNotEquals(max, () -> { + w.s += cellString(min.succ(), max); + }); + + return w.s; + } + + public String toString() { + return min + " | " + cellString(min, max) + " | " + max; + } } diff --git a/turing/TuringMachine.java b/turing/TuringMachine.java index ae3e240..c3ab4e2 100644 --- a/turing/TuringMachine.java +++ b/turing/TuringMachine.java @@ -7,6 +7,7 @@ public class TuringMachine { public TuringMachine() { tape = new Tape(); sm = new StateMachine(() -> { + System.out.println(tape); System.out.println("halted"); System.exit(0); }); @@ -29,6 +30,8 @@ public class TuringMachine { tape.set(n, '1'); n = n.succ(); tape.set(n, '0'); + + System.out.println(tape); } public void step() {