四则运算计算器设计说明书.doc

上传人:hs****ma 文档编号:543962864 上传时间:2023-11-08 格式:DOC 页数:10 大小:85.01KB
返回 下载 相关 举报
四则运算计算器设计说明书.doc_第1页
第1页 / 共10页
四则运算计算器设计说明书.doc_第2页
第2页 / 共10页
四则运算计算器设计说明书.doc_第3页
第3页 / 共10页
四则运算计算器设计说明书.doc_第4页
第4页 / 共10页
四则运算计算器设计说明书.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《四则运算计算器设计说明书.doc》由会员分享,可在线阅读,更多相关《四则运算计算器设计说明书.doc(10页珍藏版)》请在金锄头文库上搜索。

1、四则运算计算器设计说明书一、设计目标本次计算器的程序设计,通过使用JAVA中的AWT包和Swing包的类库设计图形界面的计算器。此计算器能够完成加减乘除的四则混合运算。利用面向对象程序设计的思想,将各个组件的事件响应分别用不同的方式表达出来,并且使用了图形界面中的事件委托机制来处理事件响应。二、设计流程1.分析该计算器需要完成的功能。 用户能够完成添加负号的四则混合运算,开方,取倒数,并且计算器能够自动识别运算符的优先级,根据用户输入的运算表达式,自动计算出相应的结果。同时还完成了计算器中C按钮清屏功能和Backspace退格键。2. 考虑异常处理。(1)当输入的表达式中出现除零的操作,显示框

2、将显示“Infinity(无穷大)”。(2)当输入的表达式错误时,将弹出提示框显示“表达式错误请重新输入” (3)当计算器的显示文本框里为没有输入内容时直接点击等号按钮 3. 编码实现计算器的功能。(1)新建相关的文件。(2)引入JAVA中相关的包。(3)定义相关的变量,创建相关组件,并对组件的属性进行设置。(4)对所创建的组件进行布局,完成界面的实现。(5)为各个组件添加事件监听器。(6)重写事件接口ActionListener的方法public void actionPerformed(ActionEvent e)。(7)为各个组件编写事件代码,完成每个按钮的不同功能。三、程序截图四、程序

3、代码 import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.s

4、wing.JTextField;public class Calc extends JFrame implements ActionListener JPanel topPanel = null;JPanel midPanel = null;JPanel bottomPanel = null;JTextField tResult = null;JButton backspaceButton = null;JButton ceButton = null;JButton cButton = null;JButton button1 = null;JButton button2 = null;JBu

5、tton button3 = null;JButton button4 = null;JButton button5 = null;JButton button6 = null;JButton button7 = null;JButton button8 = null;JButton button9 = null;JButton button0 = null;JButton buttonDiv = null;JButton buttonPlus = null;JButton buttonMinus = null;JButton buttonMul = null;JButton buttonSq

6、rt = null;JButton buttonMod = null;JButton buttonPM = null;JButton buttonX = null;JButton buttonPoint = null;JButton buttonEquals = null;StringBuffer str = new StringBuffer();boolean isDouble = false;/ 是否为实数int opFlag = -1;static double t1 = 0, t2 = 0, t3 = 0, result = 0;static int opflag1 = -1, opf

7、lag2 = -1, flag = 0, resflag = 1;int preOp, currentOp = 0;/ 标准位double op1 = 0, op2 = 0;/ 操作数double n3;/ 取得屏幕对象Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();/ 取得屏幕的宽度int width = screenSize.width;/ 取得屏幕的高度int heigth = screenSize.height;public Calc() tResult = new JTextField(0.);tR

8、esult.setColumns(26);tResult.setHorizontalAlignment(JTextField.RIGHT);topPanel = new JPanel();topPanel.add(tResult);backspaceButton = new JButton(Backspace);backspaceButton.addActionListener(this);ceButton = new JButton(CE);ceButton.addActionListener(this);cButton = new JButton(C);cButton.addActionL

9、istener(this);midPanel = new JPanel();midPanel.add(backspaceButton);midPanel.add(ceButton);midPanel.add(cButton);bottomPanel = new JPanel(new GridLayout(4, 5, 3, 3);button7 = new JButton(7);button7.addActionListener(this);bottomPanel.add(button7);button8 = new JButton(8);button8.addActionListener(th

10、is);bottomPanel.add(button8);button9 = new JButton(9);button9.addActionListener(this);bottomPanel.add(button9);buttonDiv = new JButton(/);buttonDiv.addActionListener(this);bottomPanel.add(buttonDiv);buttonSqrt = new JButton(sqrt);buttonSqrt.addActionListener(this);bottomPanel.add(buttonSqrt);button4

11、 = new JButton(4);button4.addActionListener(this);bottomPanel.add(button4);button5 = new JButton(5);button5.addActionListener(this);bottomPanel.add(button5);button6 = new JButton(6);button6.addActionListener(this);bottomPanel.add(button6);buttonMul = new JButton(*);buttonMul.addActionListener(this);

12、bottomPanel.add(buttonMul);buttonMod = new JButton(%);buttonMod.addActionListener(this);bottomPanel.add(buttonMod);button1 = new JButton(1);button1.addActionListener(this);bottomPanel.add(button1);button2 = new JButton(2);button2.addActionListener(this);bottomPanel.add(button2);button3 = new JButton

13、(3);button3.addActionListener(this);bottomPanel.add(button3);buttonMinus = new JButton(-);buttonMinus.addActionListener(this);bottomPanel.add(buttonMinus);buttonX = new JButton(1/x);buttonX.addActionListener(this);bottomPanel.add(buttonX);button0 = new JButton(0);button0.addActionListener(this);bott

14、omPanel.add(button0);buttonPM = new JButton(+/-);buttonPM.addActionListener(this);bottomPanel.add(buttonPM);buttonPoint = new JButton(.);buttonPoint.addActionListener(this);bottomPanel.add(buttonPoint);buttonPlus = new JButton(+);buttonPlus.addActionListener(this);bottomPanel.add(buttonPlus);buttonEquals = new JButton(=);buttonEquals.addActionListener(this);bottomPanel.add(buttonEquals);this.setLayout(new BorderLayout();this.add(topPanel, North);this

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号