




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java平台第一版版最后结果如图:可实现连等、连加、开平方、正负运算。代码:import java.awt.BorderLayout; /还可以增加的功能,整数运算超过Long上限的提示。 -ANS的快速显示 import java.awt.GridLayout; import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class MyCalculator extends KeyAdapter implements ActionListener Override public void keyPressed(KeyEvent e) throws java.lang.NumberFormatException / TODO Auto-generated method stub if (e.getKeyCode()=KeyEvent.VK_0 & e.getKeyChar()1) str=str.substring(0,str.length()-1); totle=totle.substring(0,str.length()-1); else System.out.println(e.getKeyCode(); throw new java.lang.NumberFormatException(); Override public void actionPerformed(ActionEvent e) String s = e.getActionCommand();/返回键的值 if (index=1) MyCalculator.str+=s; totle+=s; text.setText(px+totle); else MyCalculator.ano+=s; totle+=s; text.setText(px+totle); private JFrame board=new JFrame(Calculator by XTANMY);private JPanel p=new JPanel();static JTextField text= new JTextField();private JButton b0=new JButton(0);private JButton b1=new JButton(1);private JButton b2=new JButton(2);private JButton b3=new JButton(3);private JButton b4=new JButton(4);private JButton b5=new JButton(5);private JButton b6=new JButton(6);private JButton b7=new JButton(7);private JButton b8=new JButton(8);private JButton b9=new JButton(9);private JButton sum=new JButton(+);private JButton equ=new JButton(=);private JButton dot=new JButton(.);private JButton min=new JButton(-);private JButton mul=new JButton(*);private JButton div=new JButton(/);private JButton fu=new JButton(+/-);private JButton sq=new JButton(sqrt);private JButton clr=new JButton(C);static String px= ; /可以不用px,使用replace()static String totle= ; /用于显示static String str=0; /为空是不能parseInt的,加0不影响数值static String ano=0; static int index=1;/标示第一个数还是第二个数 static int in=0;/标示是整数还是实数static char op;/操作符static boolean now=false;/标示是否连等static boolean jump=false;/标示是否从整除跳到实数除static Long a=0l;static Long b=0l;static Double c=0.0;static Double d=0.0;static int flag1=1; /表示负号static int flag2=1;static boolean dotonly=true;/只能加一个小数点void first() text.setHorizontalAlignment(JTextField.LEFT);text.addKeyListener(this);/ board.addKeyListener(this);board.setResizable(false);/不能最大化board.getContentPane().add(text,BorderLayout.NORTH);board.getContentPane().add(p,BorderLayout.SOUTH); board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);void second()p.setLayout(new GridLayout(4,5);p.add(b7);p.add(b8);p.add(b9);p.add(div);p.add(mul);p.add(b4);p.add(b5);p.add(b6);p.add(min);p.add(sum);p.add(b1);p.add(b2);p.add(b3);p.add(dot);p.add(sq);p.add(b0);p.add(fu);p.add(clr);p.add(equ);b0.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this);b7.addActionListener(this);b8.addActionListener(this);b9.addActionListener(this);sum.addActionListener(new sum();equ.addActionListener(new equ();dot.addActionListener(new dot();min.addActionListener(new min();mul.addActionListener(new mul();div.addActionListener(new div();fu.addActionListener(new fu();sq.addActionListener(new sq();clr.addActionListener(new clr();board.pack();board.setVisible(true);board.setLocationRelativeTo(null); /设置窗口居中显示 static void clr()now=false;jump=false;index=1;op= ;a=0l;b=0l;c=0.0;d=0.0;str=0;ano=0;in=0;flag1=1;flag2=1;dotonly=true;totle= ;px= ;text.setText(0); return;static void play()/整数相加,相减,相乘,不提供相除if (str.length()=1 & ano.length()!=1 & !now)/解决在结果上继续算的问题 now=true; /此时不读c,只读d b=Long.parseLong(ano)*flag2; if (ano.length()=1 & !now & op!= ) /第二个数没值 /用来解决结果为25时,计算25*=625. if (str.length()1) a=Long.parseLong(str)*flag1;/读第一个值 / System.out.println(a);b=a; now=true;/不按普通方法读俩数 index =-1; /不让a乘两遍flag1 if (!now & op!= ) a=Long.parseLong(str)*flag1; b=Long.parseLong(ano)*flag2;Long c; /包装器才有toString方法if (op=/ & b=0) /除数不能为0 text.setText(除数不能为0,自动复位); now=false;jump=false;index=1;op= ;a=0l;b=0l;str=0;ano=0;in=0;flag1=1;flag2=1;dotonly=true;totle= ;px= ;return;if (op=/& a%b!=0) now =true; jump=true;/ System.out.println(jump); play2(); return; /必须return啊。不然执行下个switch。if (now & index!=-1)a=a*flag1;/1、6*6 2、+/- = 参考windows计算器结果为-36*6switch (op)case +:c=a+b;text.setText(c.toString();a=c;now=true;index=1;str=0;ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case -:c=a-b;text.setText(c.toString();a=c;now=true;index=1;str=0;ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case *:c=a*b;text.setText(c.toString();a=c;now=true;index=1;str=0;ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case /:c=a/b;text.setText(c.toString();a=c; now=true;index=1;str=0;ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;static void play2() /实数相加、相减、相乘、相除if (str.length()=1 & ano.length()!=1 & !now)/解决result*5=5result问题 now=true; /此时不读c,只读d d=Double.parseDouble(ano)*flag2; if (in=1) c=(double) a; /解决Ans*1.0 ans为整数的问题。 if (ano.length()=1 & !now & op!=s & op!= ) /第二个数没值 if (c=0 | str.length()1) c=Double.parseDouble(str)*flag1; d=c; now=true; index=-1; if (!now & op!= ) c=Double.parseDouble(str)*flag1; d=Double.parseDouble(ano)*flag2;Double x;if (jump) /jump过来的d先赋值c=(double) a;d=(double) b;jump=false;if (op=/ & d=0) /除数不能为0 text.setText(除数不能为0,自动复位); now=false;jump=false;index=1;op= ;a=0l;b=0l;c=0.0;d=0.0;str=0;ano=0;in=0;flag1=1;flag2=1;dotonly=true;totle= ;px= ; return;/else if (op=/ & c%d=0)/play();if (op=s & in=1) c=(double) a*flag1; now=false; /防止乘两遍flag1 if (now & index!=-1) /解决9+6=15,- = -15+6=-9的问题c=c*flag1;if (op=s & c0)text.setText(被开方数必须为正,自动复位);now=false;jump=false;index=1;op= ;a=0l;b=0l;c=0.0;d=0.0;str=0;ano=0;in=0;flag1=1;flag2=1;dotonly=true;totle= ;px= ;return;int m=0,n=0; /按小数精度运算if (c.toString().contains(.) m=c.toString().length()-c.toString().indexOf(.)-1;if (d.toString().contains(.) n=d.toString().length()-d.toString().indexOf(.)-1;int sum=m+n;if (mLong.MAX_VALUE)/得有绝对值,因为可能为负的sum-;/System.out.println(flag1+,+flag2);switch (op)case +:x=Math.round(c+d)*Math.pow(10,m)/Math.pow(10,m);text.setText(x.toString();c=x;now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case -:x=Math.round(c-d)*Math.pow(10,m)/Math.pow(10,m);text.setText(x.toString();c=x;now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case *:x=Math.round(c*d)*Math.pow(10,sum)/Math.pow(10,sum);text.setText(x.toString();c=x;now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case /:x=c/d;text.setText(x.toString();c=x;now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;case s:x=Math.sqrt(c);text.setText(x.toString();c=x;now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break;static void change() /防止显示-形式 if (MyCalculator.totle.charAt(0)=-)if (totle.charAt(0)=- & px= )px=-;totle=totle.replace(-, );if (totle.charAt(0)=- & px=-)px= ;totle=totle.replace(-, );public static void main(String args) throws java.lang.NumberFormatException MyCalculator my=new MyCalculator(); my.first(); my.second();class sum implements ActionListener Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stub MyCalculator.index=2; MyCalculator.op=+; MyCalculator.now=false; MyCalculator.dotonly=true; if (MyCalculator.str.length()=1 & MyCalculator.in=2) /该if解决ANS/5的显示问题 MyCalculator.totle=MyCalculator.c.toString(); else if (MyCalculator.str.length()=1 & (MyCalculator.in=1| MyCalculator.in=0) MyCalculator.totle=MyCalculator.a.toString(); MyCalculator.change(); MyCalculator.totle+=+ ; /e.getCommand()+ ;也可以 MyCalculator.text.setText(MyCalculator.px+MyCalculator.totle);class min implements ActionListener Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stub MyCalculator.index=2; MyCalculator.op=-; MyCalculator.now=false; MyCalculator.dotonly=true; if (MyCalculator.str.length()=1 & MyCalculator.in=2) MyCalculator.totle=MyCalculator.c.toString(); else if (MyCalculator.str.length()=1 & (MyCalculator.in=1| MyCalculator.in=0) MyCalculator.totle=MyCalculator.a.toString(); MyCalculator.change(); MyCalculator.totle+=- ; MyCalculator.text.setText(MyCalculator.px+MyCalculator.totle);class mul implements ActionListener Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stub MyCalculator.index=2; MyCalculator.op=*; MyCalculator.now=false; MyCalculator.dotonly=true; if (MyCalculator.str.length()=1 & MyCalculator.in=2) MyCalculator.totle=MyCalculator.c.toString(); else if (MyCalculator.str.length()=1 & (MyCalculator.in=1| MyCalculator.in=0) MyCalculator.totle=MyCalculator.a.toString(); MyCalculator.change(); MyCalculator.totle+=* ; MyCalculator.text.setText(MyCalculator.px+MyCalculator.totle);class div implements ActionListener Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stub MyCalculator.index=2; MyCalculator.op=/; MyCalculator.now=false; MyCalculator.dotonly=true; /MyCalculator.in=2; if (MyCalculator.str.length()=1 & MyCalculator.in=2 ) MyCalculator.totle=MyCalculator.c.toString(); else if (MyCalculator.str.length()=1 & (MyCalculator.in=1| MyCalculator.in=0) MyCalculator.totle=MyCalculator.a.toString(); MyCalculator.change(); MyCalculator.totle+=/ ; MyCalculator.text.setText(MyCalculator.px+MyCalculator.totle); class equ implements ActionListener Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stubif (MyCalculator.str.contains(.) | MyCalculator.ano.contains(.)| (MyCalculator.str.length()=1 & MyCalculator.in=2 ) )/length为 /1和equals0等价。实数(op)一个数则结果应也为实数。MyCalculator.play2();else MyCalculator.play();class dot implements ActionListener Overridepublic void actionPerformed(ActionEvent e) if (MyCalculator.index=1 & MyCalculator.dotonly) MyCalculator.str+=.; MyCalculator.dotonly=false; MyCalculator.totle+=.; MyCalcul
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 英语基础词法试题及答案
- 330kV升压储能站建设项目经济效益和社会效益分析报告
- 物流基础试题及答案
- 不锈钢生产线项目风险评估报告
- 工业园区储能项目建设工程方案
- 城市地下燃气管网及供气设施建设改造项目技术方案
- 离婚不离家协议书范本:财产分割与共同抚养子女
- 离异父母子女抚养责任分配及监护权协商合同范本
- 离婚抚养权协议书:子女教育、医疗及生活费用范本
- 生命科学领域基因测序数据保密合作协议
- 血尿临床评估与健康管理
- 毕业设计(论文)-芦苇草方格铺设装置设计
- 手术后疼痛评估与护理团体标准
- 五金公司质量管理制度
- 中试平台运营管理制度
- 2025年中国汽车用PTC电加热器数据监测报告
- 海参池养殖合作合同协议书
- 幼儿园中班科学语言《望着月亮吃大饼》课件
- 日本《大肠癌治疗指南》解读
- 颞下颌关节肿瘤
- 素质的课件教学课件
评论
0/150
提交评论