




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、课 程 设 计课程名称Java语言课程设计题目名称集合的并、交和差运算学生学院应用数学学院专业班级_学 号_学生姓名_指导教师2013年 10月 28日目录一、程序介绍1、程序界面2、主要功能二、实验方案及主要代码1、结点类2、链表类2.1 构造方法2.2 链表元素异常检测2.3 增加元素2.4 显示链表2.5 链表排序2.6 String链表排序2.7 集合交运算2.8 集合并运算2.9 集合差运算3、窗口类3.1 集合元素类型(单选框按钮组)3.2 集合交、并、差运算按钮3.3 Reset按钮3.4 Search按钮3.5 排序按钮三、实验总结1. 我负责的工作2. 问题及讨论2.1 结点
2、元素的数据类型 object与泛型<T>2.2 集合元素的分类及其优点2.3 集合并交差算法的时间复杂度3. 实验总结一、 程序介绍1、程序界面:2、主要功能有:1)集合的元素类型有三个选择,分别是Integer、Double、String。2)通过编辑框输入链表,鼠标移到编辑框会提示“输入集合元素,以空白符隔 开”,从而实现整条链表一起输入。3)如果编辑框输入元素与已选择的操作不同或元素输入重复,便会分别弹出对话框提示“集合中元素类型不合法”、“元素不可重复”。4)按钮Union、Intersec、Difference分别实现集合的交、并、差运算。5)按钮Reset重置文本编辑框
3、。6)点击按钮Search会弹出搜索替换对话框,选择需替换的链表(collection1或 collection2),输入搜索元素和替换元素,点击Ensure替换。7)对话框底层的按钮可实现对result编辑框进行升序(降序)排序。3、数据处理集合并运算交运算:差运算:降序排序:查找替换:二、 实验方案及主要代码程序有三个类,分别是节点类(泛型)、链表类、和窗体类。1. 节点类class Node<T> /结点类 T data; Node next;节点类运用了泛型,里面有两个成员变量,分别是结点存放的数据data,结点所指向的下一个结点 next。2. 链表类链表类类名:List
4、Link有两个成员变量: Node head; /指向链表的头引用 int length; /链表的长度主要的方法有:2.1 构造方法以整形为例,三个类型的元素都有自己的构造方法无形参的构造方法,头结点为空,长度为0. public ListLink(int t) /int t仅为实现方法的重构,无实际作用 head = new Node<Integer>(); head.next = null; length = 0; 用编辑框的字符串初始化链表public ListLink(String strT, int t) throws Exception /参数 int t仅为实现方法
5、的重构,无实际作用 /从文本框中获取字符串strT,并用其来生成一个集合 Node<Integer> p; head = new Node<Integer>(); String str = strT.split("s"); if (!checkRepeat(str) throw new Exception("整数集合不可有重复的元素!"); if (!checkIntAbnormal(str) throw new Exception("整数集合中元素类型不合法"); length = str.length; h
6、ead.next = null; for (int i = length - 1; i >= 0; i-) p = new Node();p.data = Integer.parseInt(stri); p.next = head.next; head.next = p; Double型和String型的构造方法同理.Double型:Node<Integer >改成Node<Double> Integer.parseInt(stri)改成Double.parseDouble (stri);String型:Node<Integer >改成Node<
7、String>p.data = Integer.parseInt(stri);改成p.data=stri;2.2 链表元素异常检测由集合的知识可知,集合的元素不可重复,故在初始化链表时,需检测集合里的元素是否重复。检测重复代码如下: public boolean checkRepeat(String str) for (int i = 0; i < str.length; i+) for (int j = (i + 1); j < str.length; j+) if (stri.equals(strj) return false; return true;程序里设置了三种数
8、据类型,故初始化还需检测输入元素是否为异常类型,如果异常,则抛出异常,代码如下: public boolean checkIntAbnormal(String str) for (int i = 0; i < str.length; i+) try int Int = Integer.parseInt(stri); /double Db=Double.parseDouble(stri); catch (NumberFormatException e) return false; return true; 2.3 增加元素链表中,每次插入元素都是在头结点后插入。(以int型为例) /pub
9、lic void Add(String var)public void Add(int var) Node<Integer> p = new Node(); /Node<String> p=new Node(); p.data = var; p.next = head.next; head.next = p; length+; 2.4 显示链表 从头结点的next开始,遍历所有结点,逐个输出 public String showIntList() Node<Integer> n = head.next; StringBuffer str = new Stri
10、ngBuffer(); while (n != null) str.append(n.data + " "); n = n.next; return new String(str); 2.5 链表排序链表升序,从头结点后第一个结点开始,每一个结点和后面所有的结点比较,如果结点的数据大于(降序则是小于)后一个结点的数据,交换彼此的数据,遍历所有结点,便完成排序。整型链表升序排序:(Double型同理)public void sortInt_ASC() Node<Integer> cur = head.next; Node<Integer> nn = n
11、ew Node(); while (cur != null) Node<Integer> after = cur.next; while (after != null) /降序则为if (pareTo(after.data) < 0) if (pareTo(after.data) > 0) nn.data = after.data; after.data = cur.data; cur.data = nn.data; after = after.next; cur = cur.next; 2.6 String链表排序字符串元素的比较算法:把String元素里的每一个ch
12、ar字符转换成ASCII编码值,然后累加得到累加值。累加值高的元素大于累加值低的元素。例如 b>a;cd>ab;算法代码如下: public static boolean StringCompare(String str1,String str2) char ch1=str1.toLowerCase().toCharArray(); char ch2=str2.toLowerCase().toCharArray(); int l1=0; int l2=0; for(int i=0;i<ch1.length;i+) l1+=(int)ch1i; for(int j=0;j<
13、;ch2.length;j+) l2+=(int)ch2j; if(l1>l2) return true; else return false; 升序排序代码: public void sortString_ASC() Node<String> cur=head.next; Node<String> nn=new Node(); while(cur!=null) Node<String> after=cur.next; while(after!=null) /降序排序 if(!StringCompare(cur.data,after.data) if(
14、StringCompare(cur.data,after.data) nn.data=after.data; after.data=cur.data; cur.data=nn.data; after=after.next; cur=cur.next; 2.7 集合交运算Integer型交运算:(Double型同理)a) 对链1和链2进行升序排序,排序后,定义两个结点cur1和cur2指向链1和链2都从头结点后第一个元素。b) 如果链1的元素小于链2的元素,则cur1指向链1的后一个结点。c) 如果链1的元素大于链2的元素,则cur2指向链2的后一个结点。d) 如果链1的元素等于链2的元素,则把
15、这个结点加入链3.然后cur1和cur2都指向链的下一个结点。e) 以此方式遍历链1与链2,直到其中一条链到达末尾。实现代码如下: public static void InterSectionInt(ListLink L1, ListLink L2, ListLink L3) L1.sortInt_ASC(); L2.sortInt_ASC(); Node<Integer> cur1 = L1.head.next; Node<Integer> cur2 = L2.head.next; while (cur1 != null && cur2 != nul
16、l) if (pareTo(cur2.data) = 0) L3.Add(cur1.data); cur1 = cur1.next; cur2 = cur2.next; else if (pareTo(cur2.data) < 0) cur1 = cur1.next; else cur2 = cur2.next; L3.sortInt_ASC();String型交运算:a) 定义两个结点cur1和cur2指向链1和链2都从头结点后第一个元素。b) 遍历链2中的结点,如果cur1的数据等于链2其中的一个结点的数据,则把cur1的数据加入链3.然后cur1指向链1的下一个结点。c) 以此方式
17、遍历链1,直到链到1达末尾。public static void InterSectionString(ListLink L1,ListLink L2,ListLink L3) Node<String> cur1=L1.head.next; Node<String> cur2=L2.head.next; while(cur1!=null) while(cur2!=null) if(cur1.data.equals(cur2.data) L3.Add(cur1.data); cur2=cur2.next; cur1=cur1.next; cur2=L2.head.next
18、; 2.8 集合并运算Integer型并运算:(Double型String型同理)a) 对链1和链2进行升序排序,排序后,定义两个结点cur1和cur2指向链1和链2都从头结点后第一个元素。b) 如果链1的元素小于链2的元素,则把cur1的数据加入链3,然后指向链1的后一个结点。c) 如果链1的元素大于链2的元素,则把cur2的数据加入链3,然后指向链2的后一个结点。d) 如果链1的元素等于链2的元素,则把这个结点加入链3.然后cur1和cur2都指向链的下一个结点。e) 以此方式遍历链1与链2,直到其中一条链到达末尾。f) 判断哪一条链还没到达末尾,把这条链的剩余结点数据都添加到链3.实现代
19、码如下:public static void UnionInt(ListLink L1, ListLink L2, ListLink L3) L1.sortInt_ASC(); L2.sortInt_ASC(); Node<Integer> cur1 = L1.head.next; Node<Integer> cur2 = L2.head.next; while (cur1 != null && cur2 != null) if (pareTo(cur2.data) = 0) L3.Add(cur1.data); cur1 = cur1.next; cu
20、r2 = cur2.next; else if (pareTo(cur2.data) < 0) L3.Add(cur1.data); cur1 = cur1.next; else L3.Add(cur2.data); cur2 = cur2.next; while (cur1 != null) L3.Add(cur1.data); cur1 = cur1.next; while (cur2 != null) L3.Add(cur2.data); cur2 = cur2.next; L3.sortInt_ASC();String型并运算:a) 定义两个结点cur1和cur2指向链1和链2都
21、从头结点后第一个元素。b) 遍历链2中的所有结点,如果cur1的数据不等于链2所有结点的数据,则把cur1的数据加入链3.然后cur1指向链1的下一个结点。c) 以此方式遍历链1,直到链到1达末尾。d) 把链2所有结点的数据加入链3.public static void UnionString(ListLink L1,ListLink L2,ListLink L3) Node<String> cur1=L1.head.next; Node<String> cur2=L2.head.next; while(cur1!=null) while(cur2!=null) if(
22、cur1.data.equals(cur2.data) break; cur2=cur2.next; if(cur2=null) L3.Add(cur1.data); cur1=cur1.next; cur2=L2.head.next; while(cur2!=null) L3.Add(cur2.data); cur2=cur2.next; 2.9 集合差运算以Integer型为例子:(Double和String同理)a) 对链1和链2进行升序排序,排序后,定义两个结点cur1和cur2指向链1和链2都从头结点后第一个元素。b) 遍历链2所有的结点,如果cur1的数据不等于链2所有结点的数据,
23、则把cur1的数据加入链3.然后cur1指向链1的下一个结点。c) 以此方式遍历链1,直到链到1达末尾。 public static void DifferenceInt(ListLink L1, ListLink L2, ListLink L3) L1.sortInt_ASC(); L2.sortInt_ASC(); Node<Integer> cur1 = L1.head.next; Node<Integer> cur2 = L2.head.next; while (cur1 != null && cur2 != null) while (cur2
24、!= null) if (pareTo(cur2.data) = 0) break; cur2 = cur2.next; if (cur2 = null) L3.Add(cur1.data); cur1 = cur1.next; cur2 = L2.head.next; L3.sortInt_ASC(); 3. 窗体类3.6 集合元素类型(单选框按钮组)创建按钮组,把集合元素类型这三个单项按钮加入按钮组,实现单选功能。3.7 集合交、并、差运算按钮以集合并运算按钮为例:(交、差运算功能按钮同理)1) 获取编辑框1的字符串1,编辑框2的字符串2.2) 判断选择了哪个单选按钮,进行对应的元素处理a
25、) 创建三个新的链表对象,用字符串1和字符串2对链1和链2初始化。b) 集合并运算。c) 如果出现字符串重复或异常错误则弹出警告窗d) 更新编辑框的标签,显示长度。代码如下:String str1 = collection1.getText(); String str2 = collection2.getText(); ListLink List1 = null, List2 = null, List3 = null; if (integerstyle.isSelected() /集合为整形 try List1 = new ListLink(str1, 1); List2 = new List
26、Link(str2, 1); List3 = new ListLink(1); ListLink.UnionInt(List1, List2, List3); result.setText(List3.showIntList(); catch (Exception e) javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), "错误提示", javax.swing.JOptionPane.ERROR_MESSAGE); else if (doublestyle.isSelected() /集合为浮点型
27、try List1 = new ListLink(str1, 1.0); List2 = new ListLink(str2, 1.0); List3 = new ListLink(1.0); ListLink.UnionDouble(List1, List2, List3); result.setText(List3.showDoubleList(); catch (Exception e) javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), "错误提示", javax.swing.JOption
28、Pane.ERROR_MESSAGE); else if (letterstyle.isSelected() /集合为字符串型 try List1=new ListLink(str1,"1"); List2 = new ListLink(str2,"1"); List3 = new ListLink("1"); ListLink.UnionString(List1, List2, List3); result.setText(List3.showStringlist(); catch(Exception e) javax.swing.
29、JOptionPane.showMessageDialog(this, e.getMessage(), "错误提示", javax.swing.JOptionPane.ERROR_MESSAGE); collection1.setToolTipText("length:" + List1.length); collection2.setToolTipText("length:" + List2.length);result.setToolTipText("length:" + List3.length);3.8 R
30、eset按钮 collection1.setText(""); collection2.setText(""); result.setText(""); collection1.setToolTipText("输入集合元素,以空白符隔开"); collection2.setToolTipText("输入集合元素,以空白符隔开"); result.setToolTipText("结果");3.9 Search按钮弹出查询对话框:(jDialog1为查询对话框的名字) jDial
31、og1.setSize(400, 300); jDialog1.setVisible(true);Collection1和collection2对应查询对话框的Ensure按钮代码:判断选择了哪个编辑框,对选择了的编辑框做如下操作:a) 以空白符为分隔符,分离编辑框的字符串,存入String数组里。b) 获取search和change编辑框的字符c) 遍历string数组,如果stri 和search 相同,则把stri改成changed) 更新Collection1或collection2编辑框e) 输出查找和替换信息 private void jButton1ActionPerformed
32、(java.awt.event.ActionEvent evt) / 查找和替换: if (Check_Collection1.isSelected() String str1 = collection1.getText(); String str = str1.split(" "); String search_str = searchtext.getText(); String change_str = changetext.getText(); String new_Textfield = "" int count = 0; /记录有几个相同的字符
33、串 for (int i = 0; i < str.length; i+) if (stri.equals(search_str) stri = change_str; count+; new_Textfield += (stri + " "); collection1.setText(new_Textfield); javax.swing.JOptionPane.showMessageDialog(this, "找到 " + """ + search_str + "" " + count
34、 + "个,以全部替换成 "" + change_str + ""","查找替换信息Check_Collection1", javax.swing.JOptionPane.PLAIN_MESSAGE); if (Check_Collection2.isSelected() String str1 = collection2.getText(); String str = str1.split(" "); String search_str = searchtext.getText(); Stri
35、ng change_str = changetext.getText(); String new_Textfield = "" int count = 0; /记录有几个相同的字符串 for (int i = 0; i < str.length; i+) if (stri.equals(search_str) stri = change_str; count+; new_Textfield += (stri + " "); collection2.setText(new_Textfield); javax.swing.JOptionPane.sho
36、wMessageDialog(this, "找到 " + """ + search_str + "" " + count + "个,以全部替换成 "" + change_str + """, "查找替换信息Check_Collection2", javax.swing.JOptionPane.PLAIN_MESSAGE);3.10 排序按钮对result编辑框进行升序排序:(降序同理) private void ascendAction
37、Performed(java.awt.event.ActionEvent evt) / 升序排序: String strT = result.getText(); ListLink List; if (integerstyle.isSelected() try List = new ListLink(strT, 1); List.sortInt_ASC(); result.setText(List.showIntList(); catch (Exception e) javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), "错误提示", javax.swing.JOptionPane.ERROR_MESSAGE); if (doublestyle.isSelected() try List = new ListLink(strT, 1.0); List.sortDouble_ASC(); result.setText(List.showDoubleList(); catch (Exception e) javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), "错误提示", javax
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论