收藏 分享(赏)

Java保存计算过程的计算器.doc

上传人:精品资料 文档编号:10810428 上传时间:2020-01-10 格式:DOC 页数:26 大小:957.50KB
下载 相关 举报
Java保存计算过程的计算器.doc_第1页
第1页 / 共26页
Java保存计算过程的计算器.doc_第2页
第2页 / 共26页
Java保存计算过程的计算器.doc_第3页
第3页 / 共26页
Java保存计算过程的计算器.doc_第4页
第4页 / 共26页
Java保存计算过程的计算器.doc_第5页
第5页 / 共26页
点击查看更多>>
资源描述

1、目 录1. 需求分析 .12. 概要设计 .23. 详细设计 .43.1UML 图 43.2 类设计图74. 主要程序实现 .155. 测试与结果 .226. 心得体会 .25参考文献 2511. 需求分析引子.随着计算机技术的飞速发展,人们已经不能满足于采用人工进行大量的计算,计算器快速的计算速度和稳定的准确率很好的满足了用户的需要,保存计算过程的计算器更是能够让用户随时随地的查找以前的计算过程。保存计算过程的计算器正是在这样的市场需求之下研发的,目的旨在帮助用户快速准确的得到计算结果。目的1.掌握 JAVA 语言编程的基础知识并能熟练运用。2.熟悉类声明与对象的使用。3.理解委托时间处理模

2、型。4.了解 JAVA 语言的图形用户界面 JAVAS WING 的设计知识。5.运用 Eclipse 编写简单计算器程序,并实现其功能 。 系统目标一个简单的计算器程序。用户可以利用鼠标点击数值或操作符按键完成计算的输入,计算的结果将被显示在窗口上部的文本框中。主体功能 1.按下数字键在文本框上会显示数字,这是计算器最基本的功能2.在任何时候按下“+-”键,计算器文本框上的数字置反3.右边一个文本框显示计算过程,上面一个文本框显示输入的数字4.当按下的运算符号时前面已经按下过运算符号时,右边一个文本框显示上一个运算符号以及两个数之间的运算结果。5. 按下清除“C”键,数据被全部清除。开发环境

3、JDK,Eclipse22. 概要设计一、功能图:保存计算过程的计算器单击数字按钮设置计算的运算数单击运算符按钮选择运算符单击函数按钮计算相应函数值单击等号显示计算结果在一个文本区显示以往的计算过程单击保存按钮将计算结果保存到文件单击复制按钮复制文本区的内容到剪贴板单击清除按钮清除文本区中的全部内容3二、系统流程图计算器计算算法 计算器矫正架堆栈 普通型科学型getButton计算数据 getButtonputOperator禁用 一般计算trigonom计量43. 详细设计3.1 UML 图类关系总图5Calculator Window 类+ a c t i o n P e f o r m

4、e d ( ) : v o i d+ C a l c u l a t o r W i n d o w ( )+ m a i n ( ) : v o i d- n u m b e r B u t t o n- o p e r a t i o n B u t t o n- 小数点操作 , 正负号操作 , 退格操作 , 等号操作 , 清零操作 , s i n , s a v e B u t t o n , c o p y B u t t o n , c l e a r B u t t o n- r e s u l t S h o w , s h o w C o m p u t e r P r o c

5、 e s s- s a v e C o m p u t e r P r o c e s s- l i s t- h a n d l e D i g i t- h a n d l e O p e r a t i o n- h a n d l e B a c k- h a n d l e C l e a r- h a n d l e E q u a l i t y- h a n d l e D o t- h a n d l e P O r N- h a n d l e S i nC a l c u l a t o r W i n d o wJ F r a m eA c t o n L i s t

6、em e r图 3.1Calculator Window 的 UML 图6NumberButton 类+ N u m b e r B u t t o n ( )+ g e t N u m b e r ( ) : i n t- n u m b e r : i n tN u m b e r B u t t o nJ B u t o t n- n u m b e r B u t t o nC a l c u l a t o r W i n d o w- 结束 11- 结束 2*图 3.2 NumberButton 类的 UML 图OperationButton 类- o p e r a t i o

7、n B u t t o nC a l c u l a t o r W i n d o w+ O p e r a t i o n B u t o n ( )+ g e t O p e r a t i o n S i g n ( ) : s t r i n g- o p e r a t i o n S i g n : s t r i n gO p e r a t i o n B u t t o nJ B u t o t n- 结束 11- 结束 2*图 3.3 OperationButton 类的 UML 图HandleDigit 类- H a n d l e D i g i tC a l c u

8、 l a t o r W i n d o w+ H a n d l e D i g i t ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u l t S h o w- s h o w C o m p u t e r P r o c e s sH a n d l e D i g i tA c t i o n L i s te r n e r- 结束 11- 结束 2*图 3.4 HandleDigit 类的 UML 图7HandleOperation 类+ H a n d l e O p e r a t i o

9、n ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u l t S h o w- s h o w C o m p u t e r P r o c e s s- s a v e C o m p u t e r P r o c e s sH a n d l e O p e t a t i o nA c t i o n L i s te r n e r- 结束 11- 结束 2*- h a n d l e D i g i t- H a n d l e O p e r a t i o nC a l c u l a t o

10、 r W i n d o w图 3.5 HandleOperation 类的 UML 图HandleDot 类+ H a n d l e D o t ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u t l S h o w- s h o w C o m p u t e r P r o c e s sH a n d l e D o tA c t i o n L i s te r n e r1*- h a n d l e D o tC a l c u l a t o r W i n d o w图 3.6 Handl

11、eDot 类的 UML 图HandlePOrN 类+ H a n d l e P O r N ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u t l S h o w- s h o w C o m p u t e r P r o c e s sH a n d l e P O r NA c t i o n L i s te r n e r1*- h a n d l e P O r NC a l c u l a t o r W i n d o w图 3.7 HandlePOrN 类的 UML 图8HandleEqu

12、ality 类+ H a n d l e E q u a l i t y ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u t l S h o w- s h o w C o m p u t e r P r o c e s s- s a v e C o m p u t e r P r o c e s sH a n d l e E q u a l i t yA c t i o n L i s te r n e r1*- h a n d l e E q u a l i t y- h a n d l e E q u a

13、 l i t yC a l c u l a t o r W i n d o w图 3.8 HandleEquality 类的 UML 图HandleSin 类+ H a n d l e S i n ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u t l S h o w- s h o w C o m p u t e r P r o c e s sH a n d l e S i nA c t i o n L i s te r n e r1*- h a n d l e S i nC a l c u l a t o

14、r W i n d o w图 3.9 HandleSin 类的 UML 图HandleBack 类+ H a n d l e B a c k ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u t l S h o w- s h o w C o m p u t e r P r o c e s sH a n d l e B a c kA c t i o n L i s te r n e r1*- h a n d l e B a c kC a l c u l a t o r W i n d o w图 3.10 Hand

15、leBack 类的 UML 图9HandleClear 类+ H a n d l e C l e a r ( )+ a c t i o n P e r f o r m e d ( ) : v o i d- l i s t- r e s u t l S h o w- s h o w C o m p u t e r P r o c e s sH a n d l e C l e a rA c t i o n L i s te r n e r1*- h a n d l e C l e a rC a l c u l a t o r W i n d o w图 3.11 HandleClear 类的 UML

16、 图3.2 类设计图类 CalculatorWindow模块名称 CalculatorWindow功能描述 负责创建计算器的主窗口接口与属性Void actionPerformed()Function:对 saveButton, copyButton, clearButton 三个按钮的ActionEvent 事件监听InPut: 无OutPut: 无Return:空CalculatorWindow()Function:是构造方法,负责完成窗口的初始化InPut: 无OutPut:无Return:空Static void Main()Functon:是计算器程序运行的入口方法InPut:无Ou

17、tPut:无Return:空数据结构与算法Linkedlist( String) list补充说明 无10类 OperationButton模块名称 OperationButton功能描述 创建主窗口中的一个 “运算符 ”按钮接口与属性String getOperationSign()Function:返回操作符号InPut: 无OutPut: 无Return:运算符号OperationButton()Function:是构造方法,创建 OperationButton 对象InPut: SOutPut:无Return:空数据结构与算法无补充说明 无类 HandleDigit模块名称 Handl

18、eDigit功能描述 用户单击 “数字 ”按钮时,负责处理和运算符有关的计算结果接口与属性Void actionPerformed()Function:处理 list 链表中存储的第一个操作数和第二个操作数InPut: 无OutPut: 无Return:空HandleDigit()Function:是构造方法,创建 HandleDigit 对象InPut: t1, t2OutPut:无Return:空数据结构与算法Linkedlist( String) list补充说明 无11类 HandleOperation模块名称 HandleOperation功能描述 用户单击 “运算符 ”按钮时,负责

19、处理和运算符有关的计算结果接口与属性Void actionPerformed()Function:处理 list 链表中存储的运算符和必要的计算InPut: 无OutPut: 无Return:空HandleOperation()Function:是构造方法,创建 HandleOperationt 对象InPut: t1, t2, t3OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无类 HandleDot模块名称 HandleDot功能描述 负责处理小数点接口与属性Void actionPerformed()Function:处理 li

20、st 链表中存储的运算数InPut: 无OutPut: 无Return:空HandleDot()Function:是构造方法,创建 HandleDot 对象InPut: t1, t2OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无12类 HandlePOrN模块名称 HandlePOrN功能描述 负责处理用户单击 “+/-”按钮时,对数字进行正负转换接口与属性Void actionPerformed()Function:处理 list 链表中存储的运算数InPut: 无OutPut: 无Return:空HandlePOrN()Func

21、tion:是构造方法,创建 HandlePOrN 对象InPut: t1, t2OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无类 HandleEquality模块名称 HandleEquality功能描述 负责处理当用户单击 “=”按钮时,计算有关的数据接口与属性Void actionPerformed()Function:处理 list 链表中存储的运算数、运算符和必要的计算InPut: 无OutPut: 无Return:空HandleEquality()Function:是构造方法,创建 HandleEquality 对象InP

22、ut: t1, t2, t3OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无13类 HandleSin模块名称 HandleSin功能描述 负责处理用户单击 sin 按钮进行正弦三角函数的计算接口与属性Void actionPerformed()Function:负责计算正弦函数的值InPut: 无OutPut: 无Return:空HandleSin()Function:是构造方法,创建 HandleSin 对象InPut: t1, t2, t3OutPut:无Return:空数据结构与算法Linkedlist( String) li

23、st 补充说明 无类 HandleBack模块名称 HandleBack功能描述 负责处理用户单击 “退格 ”按钮时进行退格操作接口与属性Void actionPerformed()Function:负责处理 list 链表中存储的运算数InPut: 无OutPut: 无Return:空HandleBack()Function:是构造方法,创建 HandleBack 对象InPut: t1, t2OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无14类 HandleClear模块名称 HandleClear功能描述 负责处理用户单击 C

24、 按钮(清零操作)时清除有关的计算结果接口与属性Void actionPerformed()Function:清除 list 链表中存储的运算数和操作数,设置resultShow 中显示的数字为 0,清除 showComputerProcess 中显示的计算过程InPut: 无OutPut: 无Return:空HandleClear()Function:是构造方法,创建 HandleClear 对象InPut: t1, t2OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无类 NumberButton模块名称 NumberButton功

25、能描述 负责创建主窗口中的一个 “数字 ”按钮接口与属性String getNumber()Function:返回数字符号InPut: 无OutPut: 无Return:数字符号NumberButton()Function:是构造方法,创建 NumberButton 对象InPut: 无OutPut:无Return:空数据结构与算法Linkedlist( String) list 补充说明 无154.主要程序实现计算结果保存到文件public void actionPerformed(ActionEvent e)if(e.getSource()=copyButton)saveComputerP

26、rocess.copy();if(e.getSource()=clearButton)saveComputerProcess.setText(null);if(e.getSource()=saveButton)JFileChooser chooser = new JFileChooser();int state =chooser.showSaveDialog(null);File file = chooser.getSelectedFile();if(file!=nullStringReader read = new StringReader(content);BufferedReader i

27、n = new BufferedReader(read);FileWriter outOne = new FileWriter(file);BufferedWriter out = new BufferedWriter(outOne);String str = null;while(str= in.readLine()!=null)out.write(str);out.newLine();in.close();out.close();catch(IOException el)数字处理public void actionPerformed(ActionEvent e)NumberButton b

28、 = (NumberButton)e.getSource();if(list.size()=0)int number = b.getNumber();list.add(“+number);resultShow.setText(“+number);showComputerProcess.setText(“+list.get(0);16else if(list.size()=1)int number = b.getNumber();String num = list.getFirst();String s = num.concat(“+number);list.set(0,s);resultSho

29、w.setText(s);showComputerProcess.setText(“+list.get(0);else if(list.size()=2)int number = b.getNumber();list.add(“+number);resultShow.setText(“+number);showComputerProcess.setText(“+list.get(0)+“+list.get(1)+“+list.get(2);else if(list.size()=3)int number = b.getNumber();String num = list.getLast();S

30、tring s = num.concat(“+number);list.set(2,s);resultShow.setText(s);showComputerProcess.setText(“+list.get(0)+“+list.get(1)+“+list.get(2);处理运算符public void actionPerformed(ActionEvent e)OperationButton b = (OperationButton)e.getSource();if(list.size()=1)String fuhao = b.getOperateSign();list.add(fuhao

31、);showComputerProcess.setText(“+list.get(0)+“+list.get(1);else if(list.size()=2)String fuhao =b.getOperateSign();list.set(1,fuhao);showComputerProcess.setText(“+list.get(0)+“+list.get(1);else if(list.size()=3)17String numOne =list.getFirst();String numTwo = list.getLast();String 运算符号=list.get(1);Str

32、ing middleProcess = numOne+“+运算符号 +numTwo;trydouble n1 = Double.parseDouble(numOne);double n2 = Double.parseDouble(numTwo);double result = 0;if(运算符号 .equals(“+“)result= n1+n2;else if(运算符号.equals( “-“)result=n1-n2;else if(运算符号.equals( “*“)result= n1*n2;else if(运算符号.equals( “/“)result = n1/n2;String f

33、uhao = b.getOperateSign();list.clear();list.add(“+result);list.add(fuhao);String pro= middleProcess+“=“+result+“+list.get(1);showComputerProcess.setText(pro);saveComputerProcess.append(“+middleProcess+“=“+result+“n“);resultShow.setText(“+result);catch(Exception ee)处理小数点public void actionPerformed(Ac

34、tionEvent e)String dot = e.getActionCommand();if(list.size()=1)String num = list.getFirst();String s = null;if(num.indexOf(dot)=-1)s= num.concat(dot);18list.set(0,s);elses=num;list.set(0,s);resultShow.setText(s);showComputerProcess.setText(“+list.get(0);else if(list.size()=3)String num = list.getLas

35、t();String s = null;if(num.indexOf(dot)=-1)s=num.concat(dot);list.set(2,s);else s= num;resultShow.setText(s);showComputerProcess.setText(“+list.get(0)+“+list.get(1)+“+list.get(2);处理正负号public void actionPerformed(ActionEvent e)if(list.size()=1)String number1 =list.getFirst();trydouble d =Double.parse

36、Double(number1);d=-1*d;String str = String.valueOf(d);list.set(0,str);resultShow.setText(str);showComputerProcess.setText(“+list.get(0);catch(Exception ee)else if(list.size()=3)String number2 = list.getLast();trydouble d =Double.parseDouble(number2);d= -1*d;String str = String.valueOf(d);19list.set(

37、2,str);resultShow.setText(str);showComputerProcess.setText(“+list.get(0)+“+list.get(2);catch(Exception ee)处理等号public void actionPerformed(ActionEvent e)if(list.size()=1)String num =list.getFirst();resultShow.setText(“+num);showComputerProcess.setText(list.get(0);if(list.size()=2)String num=list.getF

38、irst();String 运算符号 =list.get(1);trydouble n1 = Double.parseDouble(num);double n2 = Double.parseDouble(num);double result = 0;if(运算符号 .equals(“+“)result= n1+n2;else if(运算符号.equals( “-“)result= n1-n2;else if(运算符号.equals( “*“)result= n1*n2;else if(运算符号.equals( “/“)result= n1/n2;resultShow.setText(“+res

39、ult);String process= num+“+运算符号+ “+num+“=“+result;showComputerProcess.setText(process);saveComputerProcess.append(“+process+“n“);list.set(0,“+result);catch(Exception ee)else if(list.size()=3)20String numOne =list.getFirst();String 运算符号= list.get(1);String numTwo = list.getLast();trydouble n1 = Doubl

40、e.parseDouble(numOne);double n2 = Double.parseDouble(numTwo);double result = 0;if(运算符号 .equals(“+“)result= n1+n2;else if(运算符号.equals( “-“)result= n1-n2;else if(运算符号.equals( “*“)result= n1*n2;else if(运算符号.equals( “/“)result= n1/n2;resultShow.setText(“+result);String process= numOne+“+运算符号+“+numTwo+“=

41、“+result;showComputerProcess.setText(process);saveComputerProcess.append(“+process+“n“);list.set(0,“+result);list.removeLast();/移到第二个运算符;list.removeLast();/移调运算符号;catch(Exception ee)处理正弦函数public void actionPerformed(ActionEvent e)if(list.size()=1|list.size()=2)String numOne =list.getFirst();trydoubl

42、e x =Double.parseDouble(numOne);double result =Math.sin(x);String str = String.valueOf(result);list.set(0,str);resultShow.setText(str);String process = “sin(“+numOne+“)“+result;showComputerProcess.setText(process);saveComputerProcess.append(“+process+“n“);if(list.size()=2)list.removeLast();21catch(E

43、xception ee)else if(list.size()=3)String numTwo =list.getLast();try double x =Double.parseDouble(numTwo);double result = Math.sin(x);String str = String.valueOf(result);list.set(0,str);resultShow.setText(str);String process =“sin(“+numTwo+“)=“+result;showComputerProcess.setText(process);saveComputer

44、Process.append(“+process+“n“);list.removeLast();/移掉第二个运算数;list.removeLast();/移掉运算符号catch(Exception ee)处理退格操作public void actionPerformed(ActionEvent e)if(list.size()=1)String num = (String)list.getFirst();if(num.length()=1)num=num.substring(0,num.length()-1);list.set(0,num);showComputerProcess.setTex

45、t(“+num);elselist.removeLast();resultShow.setText(num);showComputerProcess.setText(“+num);else if(list.size()=3)String num=(String)list.getLast();if(num.length()=1)22num=num.substring(0,num.length()-1);list.set(2,num);resultShow.setText(num);showComputerProcess.setText(num);elselist.removeLast();res

46、ultShow.setText(“0“);showComputerProcess.setText(“0“);5.测试与结果计算界面:加减乘除操作和正弦计算操作:23清零操作:保存操作:24清除操作:点击清除按钮之后:256.心得体会在做本项目是时候,会遇到很多问题,最大的问题的如何保存已经输入的数字或者符号进行运算,如何显示运算过程,经过多次研究和查询发现运可以运用链表进行存储和提取数据,用栈也是可以的。此次课程设计让我更了解熟悉了 Java 中的图形用户界面和它的编程方式。在完成课题的过程中也不断充实了自己,学习到了很多以前没有学习到的知识,收获很大。最大的收获是在弯完成过程中培养的解决问题

47、的能力,在做项目时必然会遇到困难,会有不会的东西,重要的不是现在会不会,而是遇到问题知道如何找到解决的途径。还使我对所学知识能够融会贯通,又不断丰富了新知识。Java 计算器设计使得我们对所学的专业课有了更为深刻的认识,使得知识得到了巩固和提高。参考文献1、JAVA 从入门到实践 清华大学出版社 刘升华等编著2、算法设计与分析 清华大学出版社 王晓东编著。3、JAVA2 范例入门与提高 清华大学出版社 东方人华主编 艾力江编著。4、软件设计师教程清华大学出版社 陈平等编著。5、JAVA 语言程序设计 机械工业出版社 (美)Y.Daniel Liang 编著。6、数据结构 云南大学出版社 王震江 何英编著

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报