Java计算器.docx
《Java计算器.docx》由会员分享,可在线阅读,更多相关《Java计算器.docx(18页珍藏版)》请在冰豆网上搜索。
![Java计算器.docx](https://file1.bdocx.com/fileroot1/2023-1/31/8bcb9938-6c62-4246-9bf1-459a097cab48/8bcb9938-6c62-4246-9bf1-459a097cab481.gif)
Java计算器
Java平台第一版版最后结果如图:
可实现连等、连加、开平方、正负运算。
代码:
importjava.awt.BorderLayout;//还可以增加的功能,整数运算超过Long上限的提示。
-ANS的快速显示
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
publicclassMyCalculatorextendsKeyAdapterimplementsActionListener
{
@Override
publicvoidkeyPressed(KeyEvente)throwsjava.lang.NumberFormatException
{
//TODOAuto-generatedmethodstub
if(e.getKeyCode()>=KeyEvent.VK_0&&e.getKeyChar()<=KeyEvent.VK_9)//输入的key值为0~9
{
chars=e.getKeyChar();
if(index==1)
{
str+=s;
totle+=s;
}
else
{
ano+=s;
totle+=s;
}
}
elseif(e.getKeyCode()==KeyEvent.VK_ENTER)//就是=按钮的监听器粘贴过来
{
if(MyCalculator.str.contains(".")||MyCalculator.ano.contains(".")
||(MyCalculator.str.length()==1&&MyCalculator.in==2))//length为
//1和equals0等价。
实数(+-*/)一个数则结果
//应也为实数。
MyCalculator.play2();
else
MyCalculator.play();
}
elseif(e.getKeyCode()==KeyEvent.VK_BACK_SPACE)//退格键只在输入时有效
//,连算时无效,参加Windows计算器
{
if(index==1&&str.length()>1)
{
str=str.substring(0,str.length()-1);
totle=totle.substring(0,str.length()-1);
}
}
else
{
System.out.println(e.getKeyCode());
thrownewjava.lang.NumberFormatException();
}
}
@Override
publicvoidactionPerformed(ActionEvente)
{
Strings=e.getActionCommand();//返回键的值
if(index==1)
{
MyCalculator.str+=s;
totle+=s;
text.setText(px+totle);
}
else
{
MyCalculator.ano+=s;
totle+=s;
text.setText(px+totle);
}
}
privateJFrameboard=newJFrame("CalculatorbyXTANMY");
privateJPanelp=newJPanel();
staticJTextFieldtext=newJTextField();
privateJButtonb0=newJButton("0");
privateJButtonb1=newJButton("1");
privateJButtonb2=newJButton("2");
privateJButtonb3=newJButton("3");
privateJButtonb4=newJButton("4");
privateJButtonb5=newJButton("5");
privateJButtonb6=newJButton("6");
privateJButtonb7=newJButton("7");
privateJButtonb8=newJButton("8");
privateJButtonb9=newJButton("9");
privateJButtonsum=newJButton("+");
privateJButtonequ=newJButton("=");
privateJButtondot=newJButton(".");
privateJButtonmin=newJButton("-");
privateJButtonmul=newJButton("*");
privateJButtondiv=newJButton("/");
privateJButtonfu=newJButton("+/-");
privateJButtonsq=newJButton("sqrt");
privateJButtonclr=newJButton("C");
staticStringpx="";//可以不用px,使用replace()
staticStringtotle="";//用于显示
staticStringstr="0";//为空是不能parseInt的,加0不影响数值
staticStringano="0";
staticintindex=1;//标示第一个数还是第二个数
staticintin=0;//标示是整数还是实数
staticcharop;//操作符
staticbooleannow=false;//标示是否连等
staticbooleanjump=false;//标示是否从整除跳到实数除
staticLonga=0l;
staticLongb=0l;
staticDoublec=0.0;
staticDoubled=0.0;
staticintflag1=1;//表示负号
staticintflag2=1;
staticbooleandotonly=true;//只能加一个小数点
voidfirst()
{
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);
}
voidsecond()
{
p.setLayout(newGridLayout(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(newsum());
equ.addActionListener(newequ());
dot.addActionListener(newdot());
min.addActionListener(newmin());
mul.addActionListener(newmul());
div.addActionListener(newdiv());
fu.addActionListener(newfu());
sq.addActionListener(newsq());
clr.addActionListener(newclr());
board.pack();
board.setVisible(true);
board.setLocationRelativeTo(null);//设置窗口居中显示
}
staticvoidclr()
{
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;
}
staticvoidplay()//整数相加,相减,相乘,不提供相除
{
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;
}
Longc;//包装器才有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*6
switch(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;
}
}
staticvoidplay2()//实数相加、相减、相乘、相除
{
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.0ans为整数的问题。
}
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;
}
Doublex;
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;
}
//elseif(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'&&c<0)
{
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;
}
intm=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;
intsum=m+n;
if(mwhile(op=='*'&&Math.abs(c*d*Math.pow(10,sum))>Long.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;
}
}
staticvoidchange()//防止显示--形式
{
if(MyCalculator.totle.charAt(0)=='-')
if(totle.charAt(0)=='-'&&px=="")
{
px="-";
totle=totle.replace('-','');
}
if(totle.charAt(0)=='-'&&px=="-")
{
px="";
totle=totle.replace('-','');
}
}
publicstaticvoidmain(String[]args)throwsjava.lang.NumberFormatException
{
MyCalculatormy=newMyCalculator();
my.first();
my.second();
}
}
classsumimplementsActionListener
{
@Override
publicvoidactionPerformed(ActionEvente)
{
//TODOAuto-generatedmethodstub
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();
elseif(MyCalculator.str.length()==1&&(MyCalculator.in==1||MyCalculator.in==0))
MyCalculator.totle=MyCalculator.a.toStr