-
Notifications
You must be signed in to change notification settings - Fork 0
/
java-patt.8.1.java
218 lines (210 loc) · 5.26 KB
/
java-patt.8.1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
interface ExprVisitorI {
Object forPlus(ExprD l, ExprD r);
Object forDiff(ExprD l, ExprD r);
Object forProd(ExprD l, ExprD r);
Object forConst(Object c);
}
abstract class ExprD {
abstract Object accept (ExprVisitorI ask);
abstract public String toString();
}
class Plus extends ExprD {
ExprD l;
ExprD r;
Plus(ExprD _l, ExprD _r) {
l = _l;
r = _r;
}
// ----------------------------------------
Object accept (ExprVisitorI ask) {
return ask.forPlus(l, r);
}
public String toString() {
return String.format("(%s) + (%s)", l, r);
}
}
class Diff extends ExprD {
ExprD l;
ExprD r;
Diff(ExprD _l, ExprD _r) {
l = _l;
r = _r;
}
// ----------------------------------------
Object accept (ExprVisitorI ask) {
return ask.forDiff(l, r);
}
public String toString() {
return String.format("(%s) - (%s)", l, r);
}
}
class Prod extends ExprD {
ExprD l;
ExprD r;
Prod(ExprD _l, ExprD _r) {
l = _l;
r = _r;
}
// ----------------------------------------
Object accept (ExprVisitorI ask) {
return ask.forProd(l, r);
}
public String toString() {
return String.format("(%s) * (%s)", l, r);
}
}
class Const extends ExprD {
Object c;
Const(Object _c) {
c = _c;
}
// ----------------------------------------
Object accept (ExprVisitorI ask) {
return ask.forConst(c);
}
public String toString() {
return String.format("%s", c);
}
}
abstract class SetD {
SetD add (Integer i) {
if (mem(i))
return this;
else
return new Add(i, this);
}
abstract boolean mem(Integer i);
abstract SetD plus(SetD s);
abstract SetD diff(SetD s);
abstract SetD prod(SetD s);
abstract public String toString();
}
class Empty extends SetD {
boolean mem(Integer i) {
return false;
}
SetD plus(SetD s) {
return s;
}
SetD diff(SetD s) {
return new Empty();
}
SetD prod(SetD s) {
return new Empty();
}
public String toString() {
return "";
}
}
class Add extends SetD {
Integer i;
SetD rest;
Add(Integer _i, SetD _r) {
i = _i;
rest = _r;
}
// ----------------------------------------
boolean mem(Integer j) {
if (j.equals(i))
return true;
else
return rest.mem(j);
}
SetD plus(SetD x) {
return rest.plus(x.add(i));
}
SetD diff(SetD x) {
if (!x.mem(i))
return (rest.diff(x)).add(i);
else
return rest.diff(x);
}
SetD prod(SetD x) {
if (x.mem(i))
return rest.prod(x).add(i);
else
return rest.prod(x);
}
public String toString() {
return String.format("%d %s", i, rest);
}
}
class IntEvalV implements ExprVisitorI {
public Object forPlus(ExprD l, ExprD r) {
return plus (l.accept(this), r.accept(this));
}
public Object forDiff(ExprD l, ExprD r) {
return diff (l.accept(this), r.accept(this));
}
public Object forProd(ExprD l, ExprD r) {
return prod (l.accept(this), r.accept(this));
}
public Object forConst(Object c) {
return c;
}
Object plus(Object x, Object y) {
return (Object) ((Integer) x + (Integer) y);
}
Object diff(Object x, Object y) {
return (Object) ((Integer) x - (Integer) y);
}
Object prod(Object x, Object y) {
return (Object) ((Integer) x * (Integer) y);
}
}
class SetEvalV extends IntEvalV {
Object plus(Object x, Object y) {
return ((SetD) x).plus ((SetD) y);
}
Object diff(Object x, Object y) {
return ((SetD) x).diff ((SetD) y);
}
Object prod(Object x, Object y) {
return ((SetD) x).prod ((SetD) y);
}
}
class Test {
static void p(Object... objs) {
for(Object o:objs)
System.out.printf("%s ", o);
}
static private void test1() {
ExprD x = new Const(new Integer(10));
ExprD p = new Plus (x, x);
ExprD w = new Prod (p, p);
p (x, ";\n", p, ";\n", w, ";\n");
p (w.accept (new IntEvalV()));
}
static private void test2() {
SetD s = new Empty().add(100).add(200);
SetD s0 = new Empty().add(1).add(2).add(3);
SetD su0 = s.plus(s0);
SetD d = su0.diff(s0);
p (s, ":",
s0, ":",
su0, ":",
d, ":",
su0.prod(s).prod(d), ":",
su0.prod(s).prod(d).prod(s0), ":"
);
}
static private void test3() {
ExprD x = new Prod(new Const (new Empty().add(7)),
new Const (new Empty().add(3)));
ExprD y = new Plus(new Const (new Empty().add(7)),
new Const (new Empty().add(3)));
Object v1 = x.accept(new SetEvalV());
Object v2 = y.accept(new SetEvalV());
p(x, "\n", y, "\n");
p(":", v1, ":", v2, ":");
}
static public void main(String[] args) {
p("\n", "---test1", "\n");
test1();
p("\n", "---test2", "\n");
test2();
p("\n", "---test3", "\n");
test3();
p ("\n", "--", "\n");
}
}