ImageVerifierCode 换一换
格式:DOC , 页数:19 ,大小:366.50KB ,
资源ID:2493128      下载积分:8 金币
验证码下载
登录下载
邮箱/手机:
图形码:
验证码: 获取验证码
温馨提示:
支付成功后,系统会自动生成账号(用户名为邮箱或者手机号,密码是验证码),方便下次登录下载和查询订单;
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/2493128.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请。


权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:4009-655-100;投诉/维权电话:18658249818。

注意事项

本文(java实现简单的计算器.doc)为本站上传会员【快乐****生活】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

java实现简单的计算器.doc

1、 目 录 目 录 I 1 需求分析 1 1.1 计算器的基本功能: 1 1.1.1 加法运算:用数字按钮和“+”按钮进行运算; 1 1.1.2 减法运算:用数字按钮和“-”按钮进行运算; 1 1.1.3 乘法运算:用数字按钮和“*”按钮进行运算; 1 1.1.4 除法运算:用数字按钮和“/”按钮进行运算; 1 1.2 退格键和清零键: 用”Backspace”、”C”和”CE”按钮实现; 1 1.3 计算器的科学计算方法: 1 1.3.1 开方:用数字按钮和“Sqrt”按钮进行运算; 1 1.3.2 百分比:用数字按钮和“%”按钮进行运算; 1 1.3.3 求倒

2、数:用数字按钮和“1/x”按钮进行运算; 1 1.3.4 求自然对数:用数字按钮和“ln”按钮进行运算; 1 1.3.5 三角函数:用数字按钮和“tan”“cos”“sin”按钮进行运算 1 1.3.6 角度换算成弧度:用数字按钮和“’ ””按钮进行运算 1 1.3.7 求反:用数字按钮和“-/+”按钮进行运算 1 1.3.8 平方和三次方:用数字按钮和“x^2”“x^3”按钮进行运算 1 1.4 常数: 2 1.4.1 π:用Math类中的PI来实现 2 1.4.2 自然对数e:用Math类中的E来实现 2 2 设计 2 2.1 用户界面设计 2 2.1.1 该计算器程

3、序的设计:用户界面包括Swing组件,不过程序中大都使用的是AWT组件. 2 2.1.2 在AWT组件, 2 2.1.3 这个界面设计中包含了五个接口,分别控制运算符,数字,清除,存储功能和小数点的输入 2 2.1.4 程序设计中,使用了布局管理: 3 2.2 概要设计 3 2.2.1 它的功能是使用图形用户来实现计算器的界面设计和运算功能以及一些科学运算方法. 3 3 实现 3 4 测试 14 4.1 实现加法运算:4+12=16 14 4.2 实现乘法运算:3*9=27 15 4.3 用”C’实现清零功能: 16 4.4 用”Backspace”实现退格功能: 17

4、 4.5 求倒数:1/4=0.25 18 I 1 需求分析 该计算器程序除了具备加减乘除基本功能外,还有清零键C、CE和退格键Backspace,和一些部分的科学计算方法,包括开方、求倒、百分比,由于时间问题,之后会完善键盘事件的监听功能。 1.1 计算器的基本功能: 1.1.1 加法运算:用数字按钮和“+”按钮进行运算; 1.1.2 减法运算:用数字按钮和“-”按钮进行运算; 1.1.3 乘法运算:用数字按钮和“*”按钮进行运算; 1.1.4 除法运算:用数字按钮和“/”按钮进行运算; 1.2 退格键和清零键: 用”Backspace”、”C”和”CE”按钮

5、实现; 1.3 计算器的科学计算方法: 1.3.1 开方:用数字按钮和“Sqrt”按钮进行运算; 1.3.2 百分比:用数字按钮和“%”按钮进行运算; 1.3.3 求倒数:用数字按钮和“1/x”按钮进行运算; 1.3.4 求自然对数:用数字按钮和“ln”按钮进行运算; 1.3.5 三角函数:用数字按钮和“tan”“cos”“sin”按钮进行运算 1.3.6 角度换算成弧度:用数字按钮和“’ ””按钮进行运算 1.3.7 求反:用数字按钮和“-/+”按钮进行运算 1.3.8 平方和三次方:用数字按钮和“x^2”“x^3”按钮进行运算 1.4 常数: 1.4.1 π:用M

6、ath类中的PI来实现 1.4.2 自然对数e:用Math类中的E来实现 2 设计 2.1 用户界面设计 2.1.1 该计算器程序的设计:用户界面包括Swing组件,不过程序中大都使用的是AWT组件. 2.1.2 在AWT组件, (1) 使用了面板和按钮组: JPanel panel1, panel2, panel3, panel4; ButtonGroup bgb; (2) 由于该组件按钮较多,设计一个方法简化按钮的设置 void addButton(JPanel panel, String name, ActionListener action, Color co

7、lor){ JButton bt = new JButton(name); panel.add(bt);//在面板上增加按钮 bt.setForeground(color);//设置字体颜色 bt.addActionListener(action);//增加监听事件 2.1.3 这个界面设计中包含了五个接口,分别控制运算符,数字,清除,存储功能和小数点的输入 class Signs implements ActionListener class Num implements ActionListener class Clear implements ActionL

8、istener class Memory implements ActionListener class Dot implements ActionListener 2.1.4 程序设计中,使用了布局管理: (1) 用边布局管理器(BorderLayout)设置计算器容器各方位组件: panel4 = new JPanel(new BorderLayout(5, 5)) panel4.add(panel1, BorderLayout.NORTH); panel4.add(panel2, BorderLayout.CENTER); this.add(tf, Borde

9、rLayout.NORTH); this.add(panel3, BorderLayout.WEST); (2) 用网格布局管理器(GridLayout)设置面板 panel1 = new JPanel(new GridLayout(1, 3, 10, 10)); panel2 = new JPanel(new GridLayout(5, 6, 5, 5)); panel3 = new JPanel(new GridLayout(5, 1, 5, 5)); 2.2 概要设计 计算器的整个程序包括:一个Calculator类 2.2.1 它的功能是使用图形用户来实现计

10、算器的界面设计和运算功能以及一些科学运算方法. 类中包含了Calculator()构造方法,和其他重要的成员方法,最终在main方法中实例化一个ca对象实现计算器的功能。 3 实现 package calculator; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; public class Calculator extends JFrame { JTextField tf; JPane

11、l panel1, panel2, panel3, panel4; JMenuBar myBar; JMenu menu1, menu2, menu3; JMenuItem editItem1, editItem2, help1, help2, help3; JRadioButtonMenuItem seeItem1, seeItem2;//单选框 JCheckBoxMenuItem seeItem3;//复选框 ButtonGroup bgb; boolean IfResult = true, flag = false; String oper = "=";

12、 double result = 0,memory = 0; DecimalFormat df; Calculator(){ super("科学计算器");//设置标题栏 df = new DecimalFormat("#.####");//保留四位小数 this.setLayout(new BorderLayout(10, 5)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); //设置每个JPanel的参数 panel1

13、 new JPanel(new GridLayout(1, 3, 10, 10)); panel2 = new JPanel(new GridLayout(5, 6, 5, 5)); panel3 = new JPanel(new GridLayout(5, 1, 5, 5)); panel4 = new JPanel(new BorderLayout(5, 5)); /** * 菜单栏 */ myBar = new JMenuBar(); menu1 = new JMenu("编辑(E)"); menu2 = new JMenu

14、"查看(V)"); menu3 = new JMenu("帮助(H)"); menu1.setFont(new Font("宋体", Font.PLAIN, 12)); menu2.setFont(new Font("宋体", Font.PLAIN, 12)); menu3.setFont(new Font("宋体", Font.PLAIN, 12)); /** * 编辑栏 */ editItem1 = new JMenuItem("复制(C) Ctrl+C"); editItem2 = new JMenuItem("粘贴(P

15、) Ctrl+V"); editItem1.setFont(new Font("宋体",Font.PLAIN,12)); editItem2.setFont(new Font("宋体",Font.PLAIN,12)); /** * 查看栏 */ seeItem1 = new JRadioButtonMenuItem("科学型(T)"); seeItem2 = new JRadioButtonMenuItem("标准型(S)"); seeItem3 = new JCheckBoxMenuItem("数字分组(I)"); see

16、Item1.setFont(new Font("宋体",Font.PLAIN,12)); seeItem2.setFont(new Font("宋体",Font.PLAIN,12)); seeItem3.setFont(new Font("宋体",Font.PLAIN,12)); /** * 帮助栏 */ help1 = new JMenuItem("帮助主题(H)"); help2 = new JMenuItem("关于计算器(A)"); help1.setFont(new Font("宋体",Font.PLAIN,12));

17、 help2.setFont(new Font("宋体",Font.PLAIN,12)); bgb = new ButtonGroup();//选项组 menu1.add(editItem1); menu1.add(editItem2); menu2.add(seeItem1); menu2.add(seeItem2); menu2.addSeparator();//添加一条分割线 menu2.add(seeItem3); menu3.add(help1); menu3.addSeparator();//添加一条分割线

18、 menu3.add(help2); myBar.add(menu1); myBar.add(menu2); myBar.add(menu3); this.setJMenuBar(myBar); /** * 文本域,即为计算器的屏幕显示区域 */ tf = new JTextField(); tf.setEditable(false);//文本区域不可编辑 tf.setBackground(Color.white);//文本区域的背景色 tf.setHorizontalAlignment(JTextF

19、ield.RIGHT);//文字右对齐 tf.setText("0"); init();//对计算器进行初始化 } /** * 初始化操作 * 添加按钮 */ void init(){ addButton(panel1, "Backspace", new Clear(), Color.red); addButton(panel1, "CE", new Clear(), Color.red); addButton(panel1, "C", new Clear(), Color.red); addButton

20、panel2, "1/x", new Signs(), Color.magenta); addButton(panel2, "ln", new Signs(), Color.magenta); addButton(panel2, "7", new Num(), Color.blue); addButton(panel2, "8", new Num(), Color.blue); addButton(panel2, "9", new Num(), Color.blue); addButton(panel2, "÷", new Signs(), Color.red)

21、 addButton(panel2, "n!", new Signs(), Color.magenta); addButton(panel2, "sqrt", new Signs(), Color.magenta); addButton(panel2, "4", new Num(), Color.blue); addButton(panel2, "5", new Num(), Color.blue); addButton(panel2, "6", new Num(), Color.blue); addButton(panel2, "×", new Sign

22、s(), Color.red); addButton(panel2, "sin", new Signs(), Color.magenta); addButton(panel2, "x^2", new Signs(), Color.magenta); addButton(panel2, "1", new Num(), Color.blue); addButton(panel2, "2", new Num(), Color.blue); addButton(panel2, "3", new Num(), Color.blue); addButton(panel2

23、 "-", new Signs(), Color.red); addButton(panel2, "cos", new Signs(), Color.magenta); addButton(panel2, "x^3", new Signs(), Color.magenta); addButton(panel2, "0", new Num(), Color.blue); addButton(panel2, "-/+", new Clear(), Color.blue); addButton(panel2, ".", new Dot(), Color.blue);

24、 addButton(panel2, "+", new Signs(), Color.red); addButton(panel2, "tan", new Signs(), Color.magenta); addButton(panel2, "%", new Signs(), Color.magenta); addButton(panel2, "π", new Num(), Color.orange); addButton(panel2, "e", new Num(), Color.orange); addButton(panel2, "′″", new Si

25、gns(), Color.orange); addButton(panel2, "=", new Signs(), Color.red); addButton(panel3, "MC", new Memory(), Color.red); addButton(panel3, "MS", new Memory(), Color.red); addButton(panel3, "MR", new Memory(), Color.red); addButton(panel3, "M-", new Memory(), Color.red); addBut

26、ton(panel3, "M+", new Memory(), Color.red); panel4.add(panel1, BorderLayout.NORTH); panel4.add(panel2, BorderLayout.CENTER); this.add(tf, BorderLayout.NORTH); this.add(panel3, BorderLayout.WEST); this.add(panel4); pack(); //调整此窗口的大小,以适合其子组件的首选大小和布局 this.setResizable(false);

27、//窗口不可改变大小 this.setLocation(300, 200); } /** * 统一设置按钮的的使用方式 */ void addButton(JPanel panel, String name, ActionListener action, Color color){ JButton bt = new JButton(name); panel.add(bt);//在面板上增加按钮 bt.setForeground(color);//设置字体颜色 bt.addActionListener(action);//增加监听事件

28、 } /** * 计算器的基础操作(+ - × ÷) */ void getResult (double x){ if(oper.equals("+")){result += x;} else if(oper.equals("-")){result -= x;} else if(oper.equals("×")){result *= x;} else if(oper.equals("÷")){result /= x;} else if(oper.equals("=")){result = x;} tf.setText(df.format(

29、result)); } /** * 运算符号的事件监听 */ class Signs implements ActionListener{ public void actionPerformed(ActionEvent e) { /* * 用ActionEvent对象的getActionCommand()方法取得与引发事件对象的字符串 */ String str = e.getActionCommand(); /* sqrt求平方根 */ if(str.equals("sqrt")){ double

30、 i = Double.parseDouble(tf.getText()); //将tf文本框中已经输入的数字字符串转化为double类型的变量 if(i>=0){ /* * String.valueOf() 转换为字符串 * df.format() 按要求保留四位小数 * Math.sqrt() 求算数平方根 */ tf.setText(String.valueOf(df.format(Math.sqrt(i)))); } else{ tf.setText("负

31、数不能开平方根"); } } /** *用log函数求自然对数 */ else if(str.equals("ln")){ double i = Double.parseDouble(tf.getText()); if(i>0){ tf.setText(String.valueOf(df.format(Math.log(i)))); }else{ tf.setText("负数不能求对数"); } } /* %求百分比 */ else if(st

32、r.equals("%")){ tf.setText(String.valueOf(df.format(Double.parseDouble(tf.getText()) / 100))); } /* 1/x求倒数 */ else if(str.equals("1/x")){ if(Double.parseDouble(tf.getText()) == 0){ tf.setText("除数不能为零"); }else{ tf.setText(String.valueOf(df.format(1 / Double.pa

33、rseDouble(tf.getText())))); } } /* sin求正弦函数 */ else if(str.equals("sin")){ double i = Double.parseDouble(tf.getText()); tf.setText(String.valueOf(df.format(Math.sin(i)))); } /* cos求余弦函数 */ else if(str.equals("cos")){ double i = Double.parseDouble(tf.ge

34、tText()); tf.setText(String.valueOf(df.format(Math.cos(i)))); } /* tan求正切函数 */ else if(str.equals("tan")){ double i = Double.parseDouble(tf.getText()); tf.setText(String.valueOf(df.format(Math.tan(i)))); } /* n!求阶乘 */ else if(str.equals("n!")){ double

35、i = Double.parseDouble(tf.getText()); if((i%2==0)||(i%2==1))//判断为整数放进行阶乘操作 { int j = (int)i;//强制类型转换 int result=1; for(int k=1;k<=j;k++) result *= k; tf.setText(String.valueOf(result)); } else { tf.setText("无法进行阶乘"); } } /*

36、 x^2求平方 */ else if(str.equals("x^2")){ double i = Double.parseDouble(tf.getText()); tf.setText(String.valueOf(df.format(i*i))); } /* x^3求立方 */ else if(str.equals("x^3")){ double i = Double.parseDouble(tf.getText()); tf.setText(String.valueOf(df.format(i*i*i)));

37、 } /* ′″角度转换 */ /** * 将角度值转换成弧度值,方便三角函数的计算 */ else if(str.equals("′″")){ double i = Double.parseDouble(tf.getText()); tf.setText(String.valueOf(i/180*Math.PI)); } else{ if(flag){ IfResult = false; } if(IfResult){ oper = str;

38、 }else{ getResult(Double.parseDouble(tf.getText())); oper = str; IfResult = true; } } } } /** * 清除按钮的事件监听 */ class Clear implements ActionListener{ public void actionPerformed(ActionEvent e) { /* * 用ActionEvent对象的getActionCommand()方法取得与引发事件对象

39、的字符串 */ String str = e.getActionCommand(); if(str == "C"){ tf.setText("0"); IfResult = true; result = 0; }else if(str == "-/+"){ double i = 0 - Double.parseDouble(tf.getText()); tf.setText(String.valueOf(df.format(i))); }else if(str == "Backspace"){

40、 if(Double.parseDouble(tf.getText()) > 0){ if(tf.getText().length() > 1){ tf.setText(tf.getText().substring(0, tf.getText().length() - 1)); //使用退格删除最后一位字符 /* * 函数:substring(int beginIndex, int endIndex) * 它的返回值为一个新的字符串,为原字符串的子串 * beginIndex为子字符串开始的地

41、方,endIndex为子字符串结束的地方 */ }else{ tf.setText("0"); IfResult = true; } }else{ if(tf.getText().length() > 1){ tf.setText(tf.getText().substring(0, tf.getText().length() - 1)); }else{ tf.setText("0"); IfResult = true; } }

42、 }else if(str == "CE"){ tf.setText("0"); IfResult = true; } } } /** * 数字输入的事件监听 */ class Num implements ActionListener{ public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if(IfResult){ tf.setText(""); IfResult = false;

43、 } if(str=="π") { tf.setText(String.valueOf(Math.PI)); } else if(str=="e") { tf.setText(String.valueOf(Math.E)); } else{ tf.setText(tf.getText()+ str); if(tf.getText().equals("0")){ //为了能够输入绝对值小于1的小数并不会重复显示多个0 tf.setText("0"); IfRe

44、sult = true; flag = true; } } } } /** * 小数点的事件监听 */ class Dot implements ActionListener{ public void actionPerformed(ActionEvent e) { IfResult = false; //在已显示的文本中查找是否有"."出现过,没有则加上"." if(tf.getText().indexOf(".") == -1){ tf.setText(tf.getText() + ".")

45、 } } } /** * 储存器的事件监听 */ class Memory implements ActionListener{ public void actionPerformed(ActionEvent e){ String str = e.getActionCommand(); if(str.equals("MC")) memory = 0; else if(str.equals("MS")) memory = Double.parseDouble(tf.getText()); else

46、if(str.equals("MR")) tf.setText(String.valueOf(memory)); else if(str.equals("M-")) memory -= Double.parseDouble(tf.getText()); else if(str.equals("M+")) memory += Double.parseDouble(tf.getText()); IfResult = true; } } /** * main方法 */ public static void main(String[] args) { Calculator ca = new Calculator(); } } 4 测试 4.1 实现加法运算:4+12=16 4.2 实现乘法运算:3*9=27 4.3 用”C’实现清零功能: 4.4 用”Backspace”实现退格功能: 4.5 求倒数:1/4=0.25 第 18 页 共 18 页

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服