用JAVA编写的计算器程序设计报告_第1页
用JAVA编写的计算器程序设计报告_第2页
用JAVA编写的计算器程序设计报告_第3页
用JAVA编写的计算器程序设计报告_第4页
用JAVA编写的计算器程序设计报告_第5页
已阅读5页,还剩81页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用 java 编写的计算器程序设计 报告目录目录 . 21 需求分析 . 01.1计算器的基本功能 :01.1.1加法运算:用数字按钮和“ +”按钮进行运算; . 01.1.2减法运算:用数字按钮和“ - ”按钮进行运算; . 01.1.3乘法运算:用数字按钮和“ *”按钮进行运算; . 01.1.4除法运算:用数字按钮和“ / ”按钮进行运算; . 01.2退 格 键 和 清 零 键 :用 ”backspace”和”c”按钮实现; . 01.3计 算 器 的 科 学 计 算 方 法 : 01.3.1开方:用数字按钮和“ sqrt ”按钮进行运算; . 01.3.2百分比:用数字按钮和“ %

2、”按钮进行运算; . 01.3.3求倒数:用数字按钮和“ 1/x ”按钮进行运算; . 02 设计. 12.1用户界面设计12.1.1该计算器程序的设计:用户界面 包括 swing 组件,不过程序中大都使用 的是 awt 组件. import java.awt.*; .12.1.2在awt组件,12.1.3这个界面设计中包含了两个接口 , 单击事件监听器接 actionlistener 口和 键盘事件监听器接口 (keylistener). .5 2.1.4程 序 设 计 中 , 使 用 了 布 局 管 理 : 52.2概要设计62.2.1calculator类 中 的 类 名 是calcul

3、ator.它的功能是使用图形用户 来实现计算器的界面设计和运算功能以及一些科学运算方法 . .62.2.2main 主类.中调用了 cal.display 来实现计算器的功能 . .63 实现. 64 测试. 364.1实 现 加 法 运 算 : 4+12=16 374.2实 现 减 法 运 算 : 22-11=11 374.3实现乘法运算 :3*9=27384.4实现除法运算 :64/32=2384.5用”c实现清零功能:394.6用 ”backspace” 实 现 退 格 功 能 : 404.7求倒数:1/4=0.25405 总结和体会 . 错误!未定义书签。1 需求分析(该部分主要阐述所

4、要实现的程序具体具 有什么样的功能,要细化,可以用图表作为辅助 描述手段)该计算器程序除了具备加减乘除基本功能外,还有清零键 c 和退格键 backspace,和一些部分的科学计算方法,包括开方、求倒、百分比,程序里面 也写了键盘事件监听器接口,不过由于时间仓促,还没能设计出来,所以该计 算器不能实现此功能。backspacececmcmrmsm+7410852+/-963./*-+sqrt%1/x=1.1计算器的基本功能:1.1.1 加法运算:用数字按钮和“ +”按钮进行运算;1.1.2 减法运算:用数字按钮和“ -”按钮进行运算;1.1.3 乘法运算:用数字按钮和“ *”按钮进行运算;1.

5、1.4 除法运算:用数字按钮和“ /”按钮进行运算;1.2退格键和清零键 :用”backspace”和”c”按钮实现;1.3计算器的科学计算方法:1.3.1 开方: 用数字按钮和“ sqrt”按钮进行运算;1.3.2 百分比:用数字按钮和“ %”按钮进行运算;1.3.3 求倒数:用数字按钮和“ 1/x”按钮进行运算;第 0 页共 41 页2 设计(该部分主要要说明,在使用 java 实现该 程序前考虑的内容,主要包括下面两部分:用户 界面设计和概要设计(这部分可简单看作是类设 计)。2.1 用户界面设计(用图或文字阐述你的界面如何设计,如: 包括哪些部分,使用什么样的布局管理器等)2.1.1

6、该计算器程序的设计:用户界面包括 swing 组件,不过程序中大都使用的 是 awt 组件. import java.awt.*;importjava.awt.event.*;import javax.swing.*;2.1.2 在 awt 组件,(1) 使用了面板和按钮 :panel p1,p2,p3,p4,p5,p6;button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;buttonbdiv,bsqrt,bmulti,bminus,bpercent,bplus,brec iprocal,bequal,bdot,bnegative;buttonbbackspace,bce

7、,bc,bmr,bms,bmc,bm;第 1 页共 41 页(2) 界面设计也包括了 awt 的委托事件模型 , 该程序设计在 java.awt.event 包中定义了窗口 事件类publicwindowclosing(windowevent e)system.exit(0);void单击事件类.public void actionperformed(actionevente)/key 0 to 9if(this.keyavailablee.getactioncommand().length()=1&e.getactioncommand().compareto(0)=0&e.getaction

8、command().compareto(9)=0)if(this.istempnowinput)this.dnowinput=0;this.istempnowinput=false;第 2 页共 41 页this.nbitsnum+;if(this.alreadyhavedot=false)this.dnowinput=this.dnowinput*10+double.parsedouble(e.getactioncommand();elsedoubletemp=double.parsedouble(e.getactioncommand();for(int i=this.n;i=0&e.get

9、actioncommand().compareto(9)=0)if(this.istempnowinput)this.dnowinput=0;this.istempnowinput=false;第 19 页共 41 页this.nbitsnum+;if(this.alreadyhavedot=false)this.dnowinput=this.dnowinput*10+double.parsedouble(e.getactioncommand();elsedoubletemp=double.parsedouble(e.getactioncommand();for(int i=this.n;i=

10、0)this.dresult=math.sqrt(this.dresult);tf1.settext(double.tostring(this.dresult);else第 26 页共 41 页tf1.settext( 函 数 输入无效);this.keyavailable=false;elseif(this.dnowinput=0)this.dnowinput=math.sqrt(this.dnowinput);tf1.settext(double.tostring(this.dnowinput);elsetf1.settext( 函 数 输入无效);第 27 页共 41 页this.key

11、available=false;/ key 1/xif(this.keyavailablee.getactioncommand()=1/x)if(this.dnowinput=0)&tf1.settext(除数不能为零);this.keyavailable=false;elsethis.dnowinput=1/this.dnowinput;tf1.settext(double.tostring(this.dnowinput);第 28 页共 41 页/ key =if(this.keyavailablee.getactioncommand()=)&this.alreadyclickedeque

12、al=true;switch(this.noperation)casethis.dresult+=this.dnowinput; break;casethis.dresult-=this.dnowinput; break;casethis.dresult*=this.dnowinput; break;case 4:1 :2 :3 :if(this.dnowinput=0)tf1.settext( 除数不能为零);第 29 页共 41 页this.keyavailable=false;elsethis.dresult=this.dresult/this.dnowinput;if(this.key

13、available)tf1.settext(double.tostring(this.dresult);/ key msif(this.keyavailablee.getactioncommand()=ms)&this.dmemory=this.dnowinput;if(this.dmemory!=0)tf2.settext(m);/ key mcif(this.keyavailablee.getactioncommand()=mc)&第 30 页共 41 页this.dmemory=0;tf2.settext();/ key mrif(this.keyavailablee.getaction

14、command()=mr)&this.dnowinput=this.dmemory;tf1.settext(double.tostring(this.dnowinput);/ key m+if(this.keyavailablee.getactioncommand()=m+)&this.dmemory+=this.dnowinput;if(this.dmemory!=0)tf2.settext(m);第 31 页共 41 页else tf2.settext();/ key %if(this.keyavailablee.getactioncommand()=%)&this.dnowinput=(

15、this.dresult*this.dnowinput)/100;tf1.settext(double.tostring(this.dnowinput);/ key backspaceif(this.keyavailablee.getactioncommand()=backspace)&if(!this.alreadyclickedequeal)if(this.dnowinput!=0)第 32 页共 41 页if(this.alreadyhavedot)if(this.n=-1)this.alreadyhavedot=false;this.n=0;elsestringstr,str1;str

16、=tf1.gettext();str1=str.substring(0,this.nbitsnum-1);this.nbitsnum-;this.n+;this.dnowinput=double.parsedouble(str1);第 33 页共 41 页tf1.settext(double.tostring(this.dnowinput);elseint temp;temp=(int)(this.dnowinput/10);this.dnowinput=(double)temp;tf1.settext(double.tostring(this.dnowinput);public void keypressed(keyevent e)第 34 页共 41 页public void keyreleased(keyevent e)public void keytyped(keyevent e)char ch=e.getkeychar();system.out.println(ch+ );public void processevent(char ch)public static void main(string args) calculator cal=new calculator();cal.display();publicwindowclosing(windoweve

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论