public class Z4_5 {
private boolean isEmpty;
private final int size = 10;
private int current;
private int set[] = new int[size];
public Z4_5() {
this.isEmpty = true;
this.current = 0;
for (int i = 0; i < size; i++)
this.set[i] = 0;
}
public void Clear() {
this.isEmpty = true;
this.current = 0;
for (int i = 0; i < this.size; i++)
this.set[i] = 0;
}
public boolean MergeIntegerSets(Z4_5 arg1, Z4_5 arg2) {
if (arg1.isEmpty && arg2.isEmpty) {
this.Clear();
return true;
}
if (arg1.isEmpty == true) {
this.isEmpty = false;
this.current = arg2.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg2.set[i];
return true;
}
if (arg2.isEmpty == true) {
this.isEmpty = false;
this.current = arg1.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg1.set[i];
return true;
}
if (arg1.equal(arg2) == true) {
this.isEmpty = false;
this.current = arg1.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg1.set[i];
return true;
}
Z4_5 tmp = new Z4_5();
tmp.isEmpty = false;
tmp.current = this.current;
for (int i = 0; i < this.size; i++)
tmp.set[i] = this.set[i];
this.isEmpty = false;
this.current = arg1.current;
for (int i = 0; i < this.size; i++)
this.set[i] = arg1.set[i];
for (int i = 0; i < arg2.size; i++) {
if (this.isIn(arg2.set[i]) == false) {
if (this.current == 10) {
System.out.println("IntegerSet Over flow!");
this.isEmpty = false;
this.current = tmp.current;
for (int j = 0; j < this.size; j++)
this.set[j] = tmp.set[j];
return false;
}
this.InsertElement(arg2.set[i]);
}
}
return true;
}
public boolean IntersectIntegerSet(Z4_5 arg1, Z4_5 arg2) {
if (arg1.isEmpty == true || arg2.isEmpty == true)
return true;
for (int i = 0; i < this.size; i++)
if (arg2.set[i] != 0 && arg1.isIn(arg2.set[i]) == true)
this.InsertElement(arg2.set[i]);
return true;
}
public boolean InsertElement(int arg) {
if (arg < 20 || arg > 80) {
System.out.println("The argument must >20 and <80");
return false;
}
if (this.isEmpty == true) {
this.isEmpty = false;
this.current++;
this.set[0] = arg;
return true;
}
if (this.current == 10)
return false;
if (this.isIn(arg) == true) {
System.out.println(arg + " is already in the IntegerSet!");
return false;
}
for (int i = 0; i < this.size; i++) {
if (this.set[i] == 0) {
this.set[i] = arg;
this.current++;
return true;
}
}
return false;
}
public boolean DeleteElement(int arg) {
if (arg < 20 || arg > 80) {
System.out.println("The argument must >20 and <80");
return false;
}
if (this.isEmpty == true)
return false;
else {
if (this.isIn(arg) == true) {
int pos = size - 1;
for (int i = 0; i < size; i++)
if (this.set[i] == arg)
pos = i;
this.current--;
if (this.current == 0)
this.isEmpty = true;
this.set[pos] = 0;
return true;
} else {
System.out.println(arg + " is not in the IntegerSet!");
return false;
}
}
}
public boolean valid() {
for (int i = 0; i < this.size; i++) {
if (this.set[i] == 0)
continue;
if (this.set[i] < 20 || this.set[i] > 80)
return false;
}
return true;
}
public boolean equal(Z4_5 arg) {
for (int i = 0; i < size; i++)
if (this.isIn(arg.set[i]) == false)
return false;
return true;
}
public boolean isIn(int arg) {
if (this.isEmpty == true)
return false;
for (int i = 0; i < size; i++)
if (arg == set[i])
return true;
return false;
}
public void print() {
System.out.print("( ");
for (int i = 0; i < this.size; i++)
System.out.print(this.set[i] + " ");
System.out.print(")\n");
}
public static void main(String[] args) {
Z4_5 t1 = new Z4_5();
Z4_5 t2 = new Z4_5();
Z4_5 t3 = new Z4_5();
Z4_5 t4 = new Z4_5();
t1.InsertElement(30);
t1.InsertElement(33);
t1.InsertElement(38);
System.out.println("t1");
t1.print();
t2.InsertElement(30);
t2.InsertElement(32);
t2.InsertElement(38);
t2.InsertElement(43);
t2.InsertElement(54);
System.out.println("t2");
t2.print();
t3.MergeIntegerSets(t1, t2);
t3.print();
t4.IntersectIntegerSet(t1, t2);
t4.print();
}
}
评论0