mirror of
https://github.com/sigmasternchen/javagony2
synced 2025-03-15 03:38:54 +00:00
added input/output display
This commit is contained in:
parent
22396b5d69
commit
1ae631fcc8
3 changed files with 116 additions and 1 deletions
|
@ -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) {
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue