




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机学院信管08级java程序设计报告java语言课程设计报告题 目:带函数功能和数制转换功能的计算器制作设 计 者: 专业班级: 学 号: 指导教师: 2011 年 12 月 12 日 计算机学院1、系统需求分析1.1系统名称:带函数功能和数制转换功能的计算器1.2系统介绍制作一个计算器,主要有以下功能:1.可以连续输入运算,对于输入的表达式能够按算符优先级进行处理。如输入2+3*2时输出为8,而不是10。2.可以处理含括号的表达式,sin、cos、tan、平方、倒数、开方等操作可以与表达式结合进行运算,不用单独运算。3.科学计数法表示,当输入值或者计算结果超过一定范围时会自动以科学计数法表示。4.增加了科学计算器的一些设施,如清空,求平方,求根号,求倒数,求三角函数,正负值,显示括号嵌套层数等。5.错误提示,除数为0,应算超过范围等在下方将有文字提示,并将当前结果置零。6.支持小数点,支持正负运算。1.3开发背景计算器作为计算工具被应用于方方面面,为人类的计算提供了便利。原来的计算器有很多不完善的地方,比如说三角函数和错误提示,本计算器制作期间考虑到这些点,将很多东西融入了这个小软件,给运算和使用提供了更多的功能。1.4系统面向的用户人群本计算器为计算量中等,对函数要求不太高的人群所开发。适合于简单计算数据,不适合科研工作。1.5开发环境eclipse2.系统设计2.1总体设计本程序在设计时,参照了windows自带的科学计算器的界面设计和功能模式。在界面上与其非常相似。利用eclipse编程环境,通过对按钮和菜单添加监听,并重载actionperformed函数实现相应事件的响当当应。程序的内核采用栈来处理,从而不仅可以处理表达式,而且可以按优先级来计算。同时还可以将sin、cos、tan、平方、倒数、开方等操作与表达式结合进行运算,在算符优先级的比较方面,本程序抛弃了传统的建立一个二维数组的方式来查询比较算符优先级,而是设一个enum类型的变量,将优先级别小的操作符放在前面,优先级别大的操作符放在后面,如:privateenumoperatoropnone,opleft,opadd,opsub,opmul,opdiv,opright,分别代表等号、左括号、加、减、乘、除、右括号。比较优先级时只要通过比较operator成员的ordinal方法反回值就可以了,如:this.m_operator.ordinal()=this.o_stack.peek().ordinal()。2.2源文件说明calcdemo.java:主类calcframe.java:程序界面类calcfunc.java:程序运算类,实现数值的输入显示和计算calcmenu.java:程序菜单类2.3相关函数和变量说明calcfunc.javapublicintm_left=0;左括号与右括号相差数publicintm_bits=0;小数点后位数publicdoublem_operand=0;当前操作数publicdoublem_accum=0;计算结果publicbooleanm_operable=false;决定当前是显示当前操作数还是运算结果publicbooleanm_dot=false;是否已经输入小数点publicstringstr;输出框显示字符串stackm_stack=null;操作数栈stacko_stack=null;操作符栈privateenumoperatoropnone,opleft,opadd,opsub,opmul,opdiv,opright;自定义操作符类型publicoperatorm_operator=operator.opnone;操作符的实例2.4详细设计双击计算器根目录calc.exe执行应用程序,运行界面如下所示:1) 数值输入和显示程序中数据的输入都是由clacfunc.java中的input函数完成的。函数的代码如下:publicvoidinput(stringstr)if(str.equals(.)this.m_dot=true;this.m_operable=true;return;if(str.equals(+)this.m_operable=false;this.m_operator=operator.opadd;run();return;if(str.equals(-)this.m_operable=false;this.m_operator=operator.opsub;run();return;if(str.equals(*)this.m_operable=false;this.m_operator=operator.opmul;run();return;if(str.equals(/)this.m_operable=false;this.m_operator=operator.opdiv;run();return;if(!this.m_operable)this.m_operand=0;this.m_dot=false;this.m_bits=0;if(this.m_dot)this.m_bits+;this.m_operand=this.m_operand+integer.parseint(str)/math.pow(10,this.m_bits);elsethis.m_operand=this.m_operand*10+integer.parseint(str);this.m_operable=true;disp();主要思想就是如果输入的是操作符,则当操作符变量为输入的操作符,然后压入栈运算。如果输入的是数字,就根据m_operable变量判断当前数是否输完,如未输完就按十进制计数法则累加,否则就作为新的数。累加后的数据先放到临时变量里,当输入操作符的时候才压入栈里。这个操作在run函数里完成。2) 程序核心本程序的核心为两三个函数,run处理操作数和操作符的栈操作,compute函数从操作数栈取出两个操作数和从操作符栈取出一个操作符并进行运算,程序代码如下:publicvoidrun()operatorgo;if(this.o_stack.empty()|(this.m_operator=operator.opleft)|this.m_operator.ordinal()this.o_stack.peek().ordinal()if(this.m_operator=operator.opleft)this.o_stack.push(this.m_operator);elsethis.o_stack.push(this.m_operator);this.m_stack.push(this.m_operand);elsethis.m_stack.push(this.m_operand);while(!this.o_stack.empty()&this.m_operator.ordinal()=this.o_stack.peek().ordinal()go=this.o_stack.pop();compute(go);this.o_stack.push(this.m_operator);if(!this.m_stack.empty()this.m_operand=this.m_accum=this.m_stack.peek();disp();if(this.m_operator=operator.opnone)this.o_stack.clear();this.m_stack.clear();publicvoidcompute(operatoropp)intresult;result=gettwo();if(result=1)switch(opp)caseopadd:this.m_stack.push(op1+op2);break;caseopmul:this.m_stack.push(op1*op2);break;caseopsub:this.m_stack.push(op2-op1);break;caseopdiv:if(op1=0)this.jlabel.settext(除数不能为零!);elsethis.m_stack.push(op2/op1);break;elsethis.m_stack.clear();this.o_stack.clear();this.m_operable=false;privateintgettwo()if(this.m_stack.empty()return0;op1=this.m_stack.pop();if(this.m_stack.empty()return0;op2=this.m_stack.pop();return1;3实验结果与数据处理1)计算表达式10+20*(30+40)+sin30。/22:计算器输出:1410.125000000064实际计算结果:1410.125,与计算结果符合。2)计算:123456789*987654321:计算器输出:1.21932631112635264e17实际计算结果:1.21932631112635264e17,与计算结果基本符合。3)计算:-3.86的平方计算器输出:14.8996实际计算结果:14.8996,与计算结果基本符合。4)计算:1/0计算器输出:0,下方文本显示“除数不能为零!”5)计算:100的n次方计算器输出:连续按n次平方后,计算器输出为0,下方文本显示“超出运算范围”;程序因为采用double数据类型存储输入数值,所以计算结果很精确,但有时会因为太精确而产生麻烦,如输入:0.1112时,实际显示会变成:0.11120000000000001。但这不影响运算结果。当再输入一个数值时,如8则显示正常:0.11128。这是由程序对输入数值的算法决定的。算法代码如下:if(this.m_dot)this.m_bits+;this.m_operand=this.m_operand+integer.parseint(str)/math.pow(10,this.m_bits);elsethis.m_operand=this.m_operand*10+integer.parseint(str);4软件测试软件测试成功,实现设计的所有要求,能进行基本数据运算、三角函数运算、进制之间的转换以及括号的优先级运算等等,运算无误。5系统总结本次项目在规定期间完成,可以进行设计题目中要求的各种运算。6系统设计心得体会在本次项目中设计中,java在原来的基础上有了更深的了解和掌握,能够熟练的对程序进行添加和改写。此外还在老师讲解的基础上学会了举一反三,能够根据自己的需求适当的添加一些相关的代码。其次对开发环境eclipse在原有的基础上有了很深的了解。此外,在开发过程中,也得到了一些教训,必须对每句代码知道是什么意思,做到认真仔细。还需要不断地尝试新的内容。在不懂得地方要多查多看多问。参考文献朱福喜.java语言基础教程m.北京:清华大学出版社,2008张化祥.java语言基础教程m.北京:清华大学出版社,2007。部份原代码calcdemo.javaimportcalcpackage.*;publicclasscalcdemo/*paramargs*/publicstaticvoidmain(stringargs)calcframemainframe=newcalcframe();mainframe.getjframe();/只是为了去除未使用变量错误警告,此语句无意义calcframe.javapublicvoidactionperformed(actionevente)stringcommand=e.getactioncommand();this.jlabel.settext(输入.);if(command.equals(sin)func.sin();elseif(command.equals(cos)func.cos();elseif(command.equals(tan)func.tan();elseif(command.equals(x2)func.x2();elseif(command.equals(1/x)func.x1();elseif(command.equals()func.left();elseif(command.equals()func.right();elseif(command.equals(sqrt)func.sqrt();elseif(command.equals(清空)func.clear();elseif(command.equals(退出)system.exit(0);elseif(command.equals(=)func.be();elseif(command.equals(+-)func.as();elsefunc.input(command);calcfunc.javapackagecalcpackage;importjavax.swing.*;importjava.util.stack;publicclasscalcfuncprivatejlabeljlabel=null;privatejtextfieldjtextfield=null;privatejtextfieldjleft=null;privatedoubleop1=0,op2=0;publicdoublepi=3.141592655358989323846264;publicintm_left=0;publicintm_bits=0;publicdoublem_operand=0;publicdoublem_accum=0;publicbooleanm_operable=false;publicbooleanm_dot=false;publicstringstr;stackm_stack=null;stacko_stack=null;privateenumoperatoropnone,opleft,opadd,opsub,opmul,opdiv,opright;publicoperatorm_operator=operator.opnone;publiccalcfunc(jlabeljl,jtextfieldjf,jtextfieldjtl)this.jlabel=jl;this.jtextfield=jf;this.jleft=jtl;m_stack=newstack();o_stack=newstack();clear();publicvoidinput(stringstr)if(str.equals(.)this.m_dot=true;this.m_operable=true;return;if(str.equals(+)this.m_operable=false;this.m_operator=operator.opadd;run();return;if(str.equals(-)this.m_operable=false;this.m_operator=operator.opsub;run();return;if(str.equals(*)this.m_operable=false;this.m_operator=operator.opmul;run();return;if(str.equals(/)this.m_operable=false;this.m_operator=operator.opdiv;run();return;if(!this.m_operable)this.m_operand=0;this.m_dot=false;this.m_bits=0;if(this.m_dot)this.m_bits+;this.m_operand=this.m_operand+integer.parseint(str)/math.pow(10,this.m_bits);elsethis.m_operand=this.m_operand*10+integer.parseint(str);this.m_operable=true;disp();publicvoiddisp()doublelval=(m_operable)?m_operand:m_accum;str=string.valueof(lval);if(str.equals(infinity)this.jlabel.settext(超过运算范围!);str=0.0;jtextfield.settext(str);publicvoidsqrt()this.m_operable=false;this.m_operand=math.sqrt(this.m_operand);this.m_accum=this.m_operand;disp();publicvoidsin()this.m_operand=this.m_operand/180*pi;this.m_operand=math.sin(this.m_operand);this.m_accum=this.m_operand;this.m_operable=false;disp();publicvoidcos()this.m_operand=this.m_operand/180*pi;this.m_operand=math.cos(this.m_operand);this.m_accum=this.m_operand;this.m_operable=false;disp();publicvoidtan()this.m_operand=this.m_operand/180*pi;this.m_operand=math.tan(this.m_operand);this.m_accum=this.m_operand;this.m_operable=false;disp();publicvoidleft()this.m_left+;this.jleft.settext(+string.valueof(this.m_left);this.m_operator=operator.opleft;this.m_operable=false;run();publicvoidright()if(this.m_left!=0)this.m_operator=operator.opright;this.m_operable=false;this.m_left-;if(this.m_left!=0)this.jleft.settext(+string.valueof(this.m_left);elsethis.jleft.settext();this.m_stack.push(this.m_operand);operatorop;while(this.o_stack.peek()!=operator.opleft)op=this.o_stack.pop();compute(op);this.m_operand=this.m_accum=this.m_stack.peek();disp();this.o_stack.pop();this.m_stack.pop();publicvoidx2()this.m_operable=false;this.m_operand=this.m_operand*this.m_operand;this.m_accum=this.m_operand;disp();publicvoidx1()this.m_operable=false;if(this.m_operand=0)this.jlabel.settext(除数不能为零!);elsethis.m_operand=1/this.m_operand;this.m_accum=this.m_operand;disp();publicvoidclear()this.m_accum=0;this.m_left=0;this.m_operable=false;this.m_operand=0;this.str=;this.jtextfield.settext(0.0);this.m_dot=false;this.m_stack.clear();this.o_stack.clear();this.jleft.settext();this.op1=0;this.op2=0;this.m_operator=operator.opnone;this.jlabel.settext(科学计算器);publicvoidas()this.m_operable=false;this.m_operand=this.m_accum=(-1)*this.m_operand;disp();publicvoidrun()operatorgo;if(this.o_stack.empty()|(this.m_operator=operator.opleft)|this.m_operator.ordinal()this.o_stack.peek().ordinal()if(this.m_operator=operator.opleft)this.o_stack.push(this.m_operator);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖北省2025届数学七下期末学业质量监测试题含解析
- 企业战略影响下的可持续发展路径试题及答案
- 续方管理中的难点与对策计划
- 重庆十一中2025届数学八下期末达标检测模拟试题含解析
- 学期工作总结与展望计划
- 江苏省苏州市立达中学2025届数学七下期末学业质量监测试题含解析
- 急诊医学志愿者的参与计划
- 新年实现财务管理的工作安排计划
- 紧贴时事的计算机二级VB试题及答案
- 水务管理数字化转型分析计划
- X射线(RAY)上岗证考试试题及答案
- 人教版物理八年级下册第三次月考试卷及答案
- 游戏研发团队管理及创新激励机制设计
- 【MOOC】家具史-南京林业大学 中国大学慕课MOOC答案
- 门市房转租合同
- 2024年度高速公路监控系统维护承包合同
- 北京市矢量地图-可改颜色
- 英语俚语课件教学课件
- 悬崖村课件教学课件
- 船舶结构节点图
- 污水处理ao工艺
评论
0/150
提交评论