再也不愿意多讲,作为一个信誓旦旦不学java的孩子能写成这样你们也不要再怪我了,这是我们的课设题目,为自己写的
要是你们也有相同的题目就果敢的cc吧。
break;
break;
break;
break;
break;
break;
} while (x!=5);
要求选用线性表的一种合适的存储结构表示集合,并实现集合的并、交等相关运算,并设计一个验证程序对给出的集合运算加以验证。
集合的运算至少应包括:
(1)集合创建
(2)集合的输出
(3)并集运算
(4)交集运算
(5)差集运算:属于A而不属于B的元素为元素的集合称为A与B的差
(6)包含运算:可给分为子集、真子集
(7)属于运算:判断一个元素是否在集合中
package 期末算法设计;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;
public class SetOperation {
HashMap allSet = new HashMap<>();
Scanner read =
new Scanner(System.in);
class Set{
String name;
TreeSet data = new TreeSet<>();
}
public void creatSet(){
System.out.println("输入集合名和元素的个数");
Set temp = new Set();
temp.name = read.next();
int size = read.nextInt();
System.out.println("输入元素");
for(int i=0;i
int dataTemp = read.nextInt();
temp.data.add(dataTemp);
}
allSet.put(temp.name, temp);
System.out.println(temp.name +temp.data.toString());
System.out.println("集合创建成功");
}
public Set getSet(){
System.out.println("输入集合名");
String name = read.next();
return allSet.get(name);
}
public void showSet(){
Set temp = getSet();
System.out.println(temp.name +temp.data.toString());
System.out.println("集合查询成功");
}
public void showAllSet(){
System.out.println("所有的集合名为。。。");
String name = allSet.keySet().toString();
System.out.println(name);
}
public void operation(){
TreeSet result = new
TreeSet<>();
int operation;
System.out.println("输入你的操作
(1.交
2.并
3.差
4.包含
5 属于 )" );
operation = read.nextInt();
switch (operation) {
case 1: result =jiao();System.out.println(result.toString());
break;
case 2: result
=bing();System.out.println(result.toString());
case 3: result
=cha();System.out.println(result.toString());
case 4: baohan();
case 5:shuyu();
default:System.out.println("wrong input");
break;
}
}
public TreeSet jiao(){
Set a = getSet();
Set b = getSet();
TreeSet data = new TreeSet<>();
TreeSet temp = new TreeSet<>();
temp.addAll(a.data);
while(!temp.isEmpty()){
int x=temp.pollFirst();
if(b.data.contains(x)){
data.add(x);
}
}
return data;
}
public TreeSet bing(){
Set a = getSet();
Set b = getSet();
TreeSet data = new TreeSet<>();
data.addAll(a.data);
data.addAll(b.data);
return data;
}
public TreeSet cha(){
Set a = getSet();
Set b = getSet();
TreeSet data = new TreeSet<>();
TreeSet temp = new TreeSet<>();
temp.addAll(a.data);
while(!temp.isEmpty()){
int x=temp.pollFirst();
if(b.data.contains(x)!=true){
data.add(x);
}
}
return data;
}
public void baohan(){
Set a = getSet();
Set b = getSet();
TreeSet temp = new TreeSet<>();
temp.addAll(a.data);
while(!temp.isEmpty()){
int x=temp.pollFirst();
if (!b.data.contains(x)) {
System.out.println(a.name+"不包含于"+b.name);
return ;
}
}
System.out.println(a.name+"包含于"+b.name);
}
public void shuyu(){
Set a = getSet();
System.out.println("输入元素");
int x=read.nextInt();
if(a.data.contains(x)){
System.out.println(x+"属于"+a.name);
}
else{
System.out.println(x+"不属于"+a.name);
}
}
public static void main(String[] args){
SetOperation operation = new SetOperation();
Scanner read = new Scanner(System.in);
System.out.println("输入你的操作: 1.创建集合
2.查询单个集合 3.查询所有集合 4.集合运算
5.退出系统");
int x;
do {
x=read.nextInt();
switch (x) {
case 1:operation.creatSet();
break;
case 2:operation.showSet();
case 3:operation.showAllSet();
case 4:operation.operation();
default:
break;
}
}
}