已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
四则运算计算器设计说明书一、设计目标本次计算器的程序设计,通过使用JAVA中的AWT包和Swing包的类库设计图形界面的计算器。此计算器能够完成加减乘除的四则混合运算。利用面向对象程序设计的思想,将各个组件的事件响应分别用不同的方式表达出来,并且使用了图形界面中的事件委托机制来处理事件响应。二、设计流程1.分析该计算器需要完成的功能。 用户能够完成添加负号的四则混合运算,开方,取倒数,并且计算器能够自动识别运算符的优先级,根据用户输入的运算表达式,自动计算出相应的结果。同时还完成了计算器中C按钮清屏功能和Backspace退格键。2. 考虑异常处理。(1)当输入的表达式中出现除零的操作,显示框将显示“Infinity(无穷大)”。(2)当输入的表达式错误时,将弹出提示框显示“表达式错误请重新输入” (3)当计算器的显示文本框里为没有输入内容时直接点击等号按钮 3. 编码实现计算器的功能。(1)新建相关的文件。(2)引入JAVA中相关的包。(3)定义相关的变量,创建相关组件,并对组件的属性进行设置。(4)对所创建的组件进行布局,完成界面的实现。(5)为各个组件添加事件监听器。(6)重写事件接口ActionListener的方法public void actionPerformed(ActionEvent e)。(7)为各个组件编写事件代码,完成每个按钮的不同功能。三、程序截图四、程序代码 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.swing.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;JButton 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 buttonSqrt = 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, opflag2 = -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.);tResult.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.addActionListener(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(this);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 = 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);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(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);bottomPanel.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.add(midPanel, Center);this.add(bottomPanel, South);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(310, 225);this.setResizable(false);/ 设置窗体出现在屏幕的中央this.setLocation(width - this.getWidth() / 2,(heigth - this.getHeight() / 2);this.setVisible(true);/* * param args */public static void main(String args) new Calc();Overridepublic void actionPerformed(ActionEvent e) String s = e.getActionCommand().trim();if (s.equals(CE) / 如果是CE则清除文本框tResult.setText(0.); else if (s.equals(Backspace) if (!tResult.getText().trim().equals(0.) / 如果文本框中有内容if (str.length() != 1 & str.length() != 0) tResult.setText(str.delete(str.length() - 1, str.length().toString(); else tResult.setText(0.);str.setLength(0);op2 = Double.parseDouble(tResult.getText().trim(); else if (s.equals(C) / 如果是C删除当前计算tResult.setText(0.);op1 = op2 = 0;str.replace(0, str.length(), );preOp = currentOp = 0; else if (s.equals(1/x) / 如果按键为1/x则将文本框中的数据为它的倒数String temp = tResult.getText().trim();double dtemp = Double.parseDouble(temp);tResult.setText( + 1 / dtemp); else if (s.equals(sqrt) / 如果按键为sqrt则将文本框中的内容求平方根String temp = tResult.getText().trim();double dtemp = Double.parseDouble(temp);tResult.setText( + Math.sqrt(dtemp); else if (s.equals(+) str.setLength(0);if (currentOp = 0) preOp = currentOp = 1;op2 = 0;tResult.setText( + op1); else currentOp = preOp;preOp = 1;switch (currentOp) case 1:op1 += op2;tResult.setText( + op1);break;case 2:op1 -= op2;tResult.setText( + op1);break;case 3:op1 *= op2;tResult.setText( + op1);break;case 4:op1 /= op2;tResult.setText( + op1);break; else if (s.equals(-) str.setLength(0);if (currentOp = 0) preOp = currentOp = 2;/ op1=op2;op2=0;tResult.setText( + op1); else currentOp = preOp;preOp = 2;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText( + op1);break;case 2:op1 = op1 - op2;tResult.setText( + op1);break;case 3:op1 = op1 * op2;tResult.setText( + op1);break;case 4:op1 = op1 / op2;tResult.setText( + op1);break; else if (s.equals(*)/ *str.setLength(0);if (currentOp = 0) preOp = currentOp = 3;/ op1=op2;op2=1;tResult.setText( + op1);/ op1=op2; else currentOp = preOp;preOp = 3;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText( + op1);break;case 2:op1 = op1 - op2;tResult.setText( + op1);break;case 3:op1 = op1 * op2;tResult.setText( + op1);break;case 4:op1 = op1 / op2;tResult.setText( + op1);break; else if (s.equals(/)/ /str.setLength(0);if (currentOp = 0) preOp = currentOp = 4;/ op2=1;tResult.setText( + op1);/ op1=op2; else currentOp = preOp;preOp = 4;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText( + op1);break;case 2:op1 = op1 - op2;tResult.setText( + op1);break;case 3:op1 = op1 * op2;tResult.setText( + op1);break;case 4:op1 = op1 / op2;tResult.setText( + op1);break; else if (s.equals(=)/ =if (currentOp = 0) str.setLength(0);tResult.setText( + op2); else str.setLength(0);currentOp = preOp;switch (currentOp) case 1:op1 = op1 + op2;tResult.setText( + op1);break;c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2023年朝阳市特岗教师招聘考试真题汇编附答案解析
- 2025下半年四川艺术职业学院考核招聘工作人员15人备考题库带答案解析
- 2025广东广州市南沙区社区专职工作人员招聘10人备考题库(第二场)含答案详解(基础题)
- 2025福建厦门市集美区诚毅中学产假顶岗英语教师招聘1人(公共基础知识)综合能力测试题附答案解析
- 2024年舟山市特岗教师笔试真题题库附答案解析
- 2025山东威海市总工会招聘3人备考题库附答案详解(精练)
- 2025福建厦门市第十中学招聘非在编及顶岗教师8人备考题库含答案解析(夺冠)
- 2025江苏连云港东海县招聘第三批社区工作者58人备考题库含答案详解(精练)
- 2024年海南州特岗教师招聘真题汇编附答案解析
- 2025贵州机电职业技术学院引进高技能人才2人备考题库带答案解析(夺冠)
- 反渗透膜处理培训课件
- 人武部2025年终总结样本(3篇)
- 山西省旅游资源
- 抖音母婴行业当我遇见小小的你项目营销方案
- 【《自动杀鱼机的方案计算设计》14000字】
- 包裹性脓胸的护理
- 2025-2026学年统编版八年级语文上册 第四单元整本书阅读《红岩》知识点梳理
- 2025初中英语词汇表1600词分类记忆
- 虚实交互技术-第1篇-洞察与解读
- 个人诊所劳务合同范本
- 《海堤生态化 设计技术指南》
评论
0/150
提交评论