java课程设计科学计算器.docx

上传人:b****6 文档编号:8435563 上传时间:2023-01-31 格式:DOCX 页数:29 大小:76.44KB
下载 相关 举报
java课程设计科学计算器.docx_第1页
第1页 / 共29页
java课程设计科学计算器.docx_第2页
第2页 / 共29页
java课程设计科学计算器.docx_第3页
第3页 / 共29页
java课程设计科学计算器.docx_第4页
第4页 / 共29页
java课程设计科学计算器.docx_第5页
第5页 / 共29页
点击查看更多>>
下载资源
资源描述

java课程设计科学计算器.docx

《java课程设计科学计算器.docx》由会员分享,可在线阅读,更多相关《java课程设计科学计算器.docx(29页珍藏版)》请在冰豆网上搜索。

java课程设计科学计算器.docx

java课程设计科学计算器

1课设任务及要求

1.1课设任务:

  

⑴、设计的计算器应用程序可以完成加法、减法、乘法、除法以及取余运算(可以进行浮点数和负数的运算);

⑵、有求倒数、退格和清零功能。

1.2创新要求:

能进行正切、余弦,以及求平方根、指数(包括对e)、自然对数运算。

1.3设计要求

①设计的计算器应用程序可以完成加法、减法、乘法、除法和取余运算。

且有小数点、正负号、求倒数、退格和清零功能。

②课程设计可选用Eclipse、JBuilder、NetBeans等作为开发平台以提高开发效率,通过资料查阅和学习尽可能熟练掌握其中一种集成开发环境。

③认真按时完成课程设计报告,课程设计报告内容包括:

设计任务与要求、需求分析、设计思路、详细设计、运行调试与分析讨论和设计体会与小结六个部分。

2需求分析

2.1设计背景

设计这个计算器主要是参考Windows操作系统中自带的计算器,由于编者水平和时间的限制,不能将计算器设计到科学型及其他更复杂的类型,在设计过程中还参考了一些其他的优秀设计。

但本计算器除了常用的加减乘除(可以进行浮点和负数运算)这些基本运算外,还有求余、求倒、退格、清零,甚至还能进行一些复杂科学的运算,比如余弦(cos)、正切(tan)、指数运算(pow)、自然对数运算(log)、求平方根(sqrt)以及对e的指数运算(exp),并且还能进行连续运算。

总体上说来,本计算器设计简单,代码很少,程序很小,但功能却很强大,这是同类计算器所不具备的。

 

2.2开发的技术及功能

本课程设计是要做一个图形界面的计算器,其界面主要是由swing组件中的控件构成。

程序实现了计算器的基本功能有:

加、减、乘、除基本算术运算(可以进行浮点和负数运算)和sin、cos、tan等三角函数求值运算,同时能进行指数运算和自然对数运算,还有求倒数、退格和清零功能。

 

3设计思路

⑴、本应用程序继承自框架类(JFrame),容器Containerc采用BorderLayout边缘布局,将单行文本框加入到“North”区域,包含各种按钮的面板JPanelp加入到”Center”区域。

包含各种按钮的面板JPanelp采用3行6列的网格布局,然后将数字按钮和运算符按钮以及控制按钮用一个for循环添加到面板中同时注册按钮事件监听器。

如:

Buttonb=newButton();

b.addActionListener(事件监听器);

⑵、事件监听器中的事件处理方法voidactionPerformed(ActionEventevt)完成主要的按钮事件的处理。

事件处理分以下几种情况:

数字按钮事件(”0”,”1”,”2”…”8”,”9”)、运算符按钮事件(”+”,”-“,”*”,”/”,”%”)、正负号按钮事件(”+/-“)、小数点按钮事件(”.”)、等号按钮事件(”=”)、求倒按钮事件(”求倒”)、退格按钮事件(”退格”)、清除按钮事件(“C”)、正切(tan)、余弦(cos),以及求平方根(sqrt)、指数(pow)、对e的指数(exp)、对数运算(log)。

⑶、在事件处理,触发按钮事件时,先判断是或是数字是或是“-/+”是或是“.”,是的话就将负号“-”、数字、小数点“.”分别写入文本框并存放在sum中,然后判断是或是“退格”、“求倒”等,是的话进行相应的处理,都不是的话则跳到doOperation()执行运算同时将运算符存放在preOperater中。

触发按钮事件时,要进一步分析,是重新开始计算时触发的按钮事件还是计算中间触发的按钮事件。

⑷、计算器完成的是一个数学表达式,如:

3+2,所以可以采用一个数组来存储数字或字符,如3,+,2分别存储在数组中,最后运算时,可以一一取出来进行运算。

⑸、利用按钮设计计算器的各个运算符和操作符,通过按钮的事件处理实现按钮计算功能。

⑹、利用文本框显示操作数和运算结果。

4详细设计

4.1功能实现:

①加减乘除求余以及指数运算

 

②其他运算

Ⅰ、求平方根

Ⅱ、对数运算

Ⅲ、余弦运算

Ⅳ、正切运算

Ⅴ、e的指数运算

Ⅵ、倒数运算

 

③主要方法说明

publiccos(doublex)//求x的余弦函数

publictan(doublex)//求x的正切函数

publicsqrt(doublex)//求x的平方根

publiclog(doublex)//求x的自然对数运算

publicexp(doublex)//求e的x次幂

publicpow(doublea,doubleb)//求a的b次幂

publicdaoshu(doublex)//求x的倒数

 

④程序流程图

4.4程序设计过程

设计过程:

(1)确定计算器函数功能和界面布局;

(2)设置框架,注册监听事件,编写方法;

(3)调试运行;

(4)改写应用程序,生成APPLET

5系统调试

5.1运行调试

计算器界面如下:

经过调试后运行,计算器界面与windows自带的计算器风格基本一致实现了预期的效果;

输入数据进行验证,各项函数功能实现均正常。

 

6参考文献

[1]丁振凡《Java语言使用教程》.北京邮件大学出版社,2006.9

[2]丁振凡《Java语言使用教程实验指导》.北京邮件大学出版社,2006.9

[3]BruceEckel《Java编程思想》.机械工业出版社2007.6

[4]耿祥义《JAVA2实用教程》.清华大学出版社2009.5

 

附录

程序代码如下:

Appalication如下:

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.event.*;

importjava.lang.*;

importjava.text.*;

importjava.math.BigInteger;

publicclassCalculatorimplementsActionListener

{

//动作监听接口

JFrameframe;//框架

JTextFieldtextShow;//文本行

JPanelpanel,panel1,panel2,panel3;//面板

JButtonbuttonBk,buttonCe,buttonC;

JButtonbutton[];

JButtonDot,AddAndSub,Add,Sub,Mul,Div,Mod,Sqrt,Dao,Equal,zhengxian,yuxian,jiecheng,duishu;

JMenuBarmainMenu;//主菜单,子菜单,菜单项

JMenueditMenu,viewMenu;

JMenuItemcopyItem,pasteItem,tItem,sItem;

DecimalFormatprec;//用于设置数据输出精度49,67

booleanclickable;//用于控制当前能否按键

doublevard,result;//用来保存double型数据的中间值(vard)和最后结果(result)

intkey=-1,prekey=-1;//key用来保存当前进行何种运算,prekey用来保存前次进行何种运算

Stringcopy;//做复制,粘贴用

//构造函数

publicCalculator()

{

clickable=true;

result=0;

frame=newJFrame("计算器—应用非师范");

prec=newDecimalFormat("0.######");//设置数据输出精度(对于double型值)

textShow=newJTextField(15);

textShow.setText("");

textShow.setHorizontalAlignment(textShow.RIGHT);

textShow.setEditable(false);

textShow.setBackground(newColor(255,255,255));

panel=newJPanel();

frame.getContentPane().add(panel);

panel1=newJPanel();

panel2=newJPanel();

panel.setLayout(newBorderLayout());

//菜单设置

mainMenu=newJMenuBar();

editMenu=newJMenu("编辑(E)");

viewMenu=newJMenu("查看(V)");

copyItem=newJMenuItem("复制(C)Ctrl+C");

copyItem.addActionListener(this);

pasteItem=newJMenuItem("粘贴(P)Ctrl+V");

pasteItem.addActionListener(this);

editMenu.add(copyItem);

editMenu.add(pasteItem);

tItem=newJMenuItem("★精简型科学计算器");

tItem.addActionListener(this);

sItem=newJMenuItem("☆开发团队");

sItem.addActionListener(this);

viewMenu.add(tItem);

viewMenu.add(sItem);

mainMenu.add(editMenu);

mainMenu.add(viewMenu);

panel.add(mainMenu,BorderLayout.NORTH);

panel.add(textShow,BorderLayout.CENTER);

panel.add(panel1,BorderLayout.SOUTH);

panel1.setLayout(newBorderLayout());

buttonBk=newJButton("Backspace");

buttonBk.setForeground(newColor(255,0,0));

buttonCe=newJButton("CE");

buttonCe.setForeground(newColor(255,0,0));

buttonC=newJButton("C");

buttonC.setForeground(newColor(255,0,0));

buttonBk.addActionListener(this);

buttonCe.addActionListener(this);

buttonC.addActionListener(this);

panel1.add(panel2,BorderLayout.NORTH);

panel2.setLayout(newFlowLayout(FlowLayout.RIGHT));

panel2.add(buttonBk);

panel2.add(buttonCe);

panel2.add(buttonC);

panel3=newJPanel();

panel1.add(panel3,BorderLayout.CENTER);

button=newJButton[14];

for(inti=0;i

{

button[i]=newJButton(Integer.toString(i));

button[i].setForeground(newColor(22,22,255));

}

Dot=newJButton(".");

Dot.setForeground(newColor(0,0,255));

AddAndSub=newJButton("+/-");

AddAndSub.setForeground(newColor(0,0,255));

Add=newJButton("+");

Add.setForeground(newColor(0,0,255));

Sub=newJButton("-");

Sub.setForeground(newColor(0,0,255));

Mul=newJButton("*");

Mul.setForeground(newColor(0,0,255));

Div=newJButton("/");

Div.setForeground(newColor(0,0,255));

Mod=newJButton("%");

Mod.setForeground(newColor(0,0,255));

Sqrt=newJButton("sqrt");

Sqrt.setForeground(newColor(0,0,255));

Dao=newJButton("1/x");

Dao.setForeground(newColor(0,0,255));

Equal=newJButton("=");

Equal.setForeground(newColor(0,0,255));

jiecheng=newJButton("n!

");

jiecheng.setForeground(newColor(0,0,255));

zhengxian=newJButton("sin");

zhengxian.setForeground(newColor(0,0,255));

yuxian=newJButton("cos");

yuxian.setForeground(newColor(0,0,255));

duishu=newJButton("log");

duishu.setForeground(newColor(0,0,255));

//将所有行为与监听绑定

panel3.setLayout(newGridLayout(4,6,6,6));//

panel3.add(button[7]);

button[7].addActionListener(this);

panel3.add(button[8]);

button[8].addActionListener(this);

panel3.add(button[9]);

button[9].addActionListener(this);

panel3.add(Div);

Div.addActionListener(this);

panel3.add(Sqrt);

Sqrt.addActionListener(this);

panel3.add(zhengxian);

zhengxian.addActionListener(this);

panel3.add(button[4]);

button[4].addActionListener(this);

panel3.add(button[5]);

button[5].addActionListener(this);

panel3.add(button[6]);

button[6].addActionListener(this);

panel3.add(Mul);

Mul.addActionListener(this);

panel3.add(Mod);

Mod.addActionListener(this);

panel3.add(yuxian);

yuxian.addActionListener(this);

panel3.add(button[1]);

button[1].addActionListener(this);

panel3.add(button[2]);

button[2].addActionListener(this);

panel3.add(button[3]);

button[3].addActionListener(this);

panel3.add(Sub);

Sub.addActionListener(this);

panel3.add(Dao);

Dao.addActionListener(this);

panel3.add(jiecheng);

jiecheng.addActionListener(this);

panel3.add(button[0]);

button[0].addActionListener(this);

panel3.add(AddAndSub);

AddAndSub.addActionListener(this);

panel3.add(Dot);

Dot.addActionListener(this);

panel3.add(Add);

Add.addActionListener(this);

panel3.add(Equal);

Equal.addActionListener(this);

panel3.add(duishu);

duishu.addActionListener(this);

frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

frame.pack();

frame.show();

}

//设置各个按钮的动作

publicstaticintcalc(intn)

{

intret=1;//BigInteger.ONE;

for(Integeri=2;i<=n;i++)

{

ret=ret*i;//.multiply(newBigInteger(i.toString()));

}

returnret;

}

publicvoidactionPerformed(ActionEventevent)

{

booleansign=false;//判断是否是double型数参与运算,是为true,不是为false

Objecttemp=event.getSource();

try{

//如果按下数字按钮,将按下的按钮代表的数据插入的当前文本框字符串之后

for(inti=0;i<=9;i++)

if(temp==button[i]&&clickable==true)

textShow.setText(textShow.getText()+Integer.toString(i));

//按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插入'.'

if(temp==Dot&&clickable==true){

booleanisDot=false;

if(textShow.getText().length()==0)

isDot=true;

for(inti=0;i

if('.'==textShow.getText().charAt(i)){

isDot=true;

break;

}

if(isDot==false)

textShow.setText(textShow.getText()+".");

}

if((temp==Add||temp==Sub||temp==Mul||

temp==Div)&&clickable==true){

//'+'操作

if(temp==Add){

switch(prekey){

case0:

result+=Double.parseDouble(textShow.getText());

break;

case1:

result-=Double.parseDouble(textShow.getText());

break;

case2:

result*=Double.parseDouble(textShow.getText());

break;

case3:

if(Double.parseDouble(textShow.getText())==0){

textShow.setText("除数不能为零");

clickable=false;

}

else

result/=Double.parseDouble(textShow.getText());

break;

default:

result=Double.parseDouble(textShow.getText());

}

textShow.setText("");

prekey=key=0;

}

//'-'操作

if(temp==Sub){

switch(prekey){

case0:

result+=Double.parseDouble(textShow.getText());

break;

case1:

result-=Double.pa

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

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1