📄 +
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.yaguo.com/~victor527/program_calculateresource.htm -->
<HTML><HEAD><TITLE>计算器源代码[原创]</TITLE>
<META content="MSHTML 5.00.3700.6699" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId>
<META content="text/html; charset=gb2312" http-equiv=Content-Type></HEAD>
<BODY bgColor=#000000 text=#3399ff>
<P><FONT color=#99ccff>import java.awt.*;<BR>import java.awt.event.*;<BR>import
java.applet.Applet;<BR>public class calculate extends Applet implements
ActionListener<BR>{<BR>char symbol='\0',PressKey='\0';<BR>double x=0,y=0;<BR>int
n=0;<BR>boolean a=false,b=false;<BR>String t=null;<BR>TextField result=new
TextField("0.0");<BR>Button btn0=new Button("0");<BR>Button btn1=new
Button("1");<BR>Button btn2=new Button("2");<BR>Button btn3=new
Button("3");<BR>Button btn4=new Button("4");<BR>Button btn5=new
Button("5");<BR>Button btn6=new Button("6");<BR>Button btn7=new
Button("7");<BR>Button btn8=new Button("8");<BR>Button btn9=new
Button("9");<BR>Button btn10=new Button(".");<BR>Button btn11=new
Button("+");<BR>Button btn12=new Button("-");<BR>Button btn13=new
Button("*");<BR>Button btn14=new Button("/");<BR>Button btn15=new
Button("=");<BR>Button btnc=new Button("C");<BR>Button btnce=new
Button("CE");<BR>Button btnbak=new Button("Backspace");<BR>Button btnmd=new
Button("%");<BR>Button btnd=new Button("1/x");<BR>Button btnsqrt=new
Button("sqrt");<BR>Button btnrev=new Button("+/-");<BR>public void
init()<BR>{<BR>setLayout(null);<BR>result.setEditable(false);<BR>add(result);<BR>result.setBounds(5,0,230,20);
<BR>add(btn0);<BR>btn0.setBounds(10,135,40,20);<BR>add(btn1);<BR>btn1.setBounds(10,60,40,20);<BR>add(btn2);<BR>btn2.setBounds(55,60,40,20);<BR>add(btn3);<BR>btn3.setBounds(100,60,40,20);<BR>add(btn4);<BR>btn4.setBounds(10,85,40,20);<BR>add(btn5);<BR>btn5.setBounds(55,85,40,20);<BR>add(btn6);<BR>btn6.setBounds(100,85,40,20);<BR>add(btn7);<BR>btn7.setBounds(10,110,40,20);<BR>add(btn8);<BR>btn8.setBounds(55,110,40,20);<BR>add(btn9);<BR>btn9.setBounds(100,110,40,20);<BR>add(btn10);<BR>btn10.setBounds(100,135,40,20);<BR>add(btn11);<BR>btn11.setBounds(145,135,40,20);<BR>add(btn12);<BR>btn12.setBounds(145,110,40,20);<BR>add(btn13);<BR>btn13.setBounds(145,85,40,20);<BR>add(btn14);<BR>btn14.setBounds(145,60,40,20);
<BR>add(btn15);<BR>btn15.setBounds(190,135,40,20);<BR>add(btnrev);<BR>btnrev.setBounds(55,135,40,20);<BR>add(btnsqrt);<BR>btnsqrt.setBounds(190,60,40,20);<BR>add(btnd);<BR>btnd.setBounds(190,110,40,20);<BR>add(btnmd);<BR>btnmd.setBounds(190,85,40,20);
<BR>add(btnc);<BR>btnc.setBounds(180,30,50,20);<BR>add(btnce);<BR>btnce.setBounds(125,30,50,20);<BR>add(btnbak);<BR>btnbak.setBounds(35,30,80,20);
<BR>btn0.addActionListener(this);<BR>btn1.addActionListener(this);<BR>btn2.addActionListener(this);<BR>btn3.addActionListener(this);<BR>btn4.addActionListener(this);<BR>btn5.addActionListener(this);<BR>btn6.addActionListener(this);<BR>btn7.addActionListener(this);<BR>btn8.addActionListener(this);<BR>btn9.addActionListener(this);<BR>btn10.addActionListener(this);<BR>btn11.addActionListener(this);<BR>btn12.addActionListener(this);<BR>btn13.addActionListener(this);<BR>btn14.addActionListener(this);<BR>btn15.addActionListener(this);<BR>btnc.addActionListener(this);<BR>btnce.addActionListener(this);<BR>btnbak.addActionListener(this);<BR>btnsqrt.addActionListener(this);<BR>btnd.addActionListener(this);<BR>btnmd.addActionListener(this);<BR>btnrev.addActionListener(this);
<BR>setBackground(Color.white); <BR>}<BR>public void actionPerformed(ActionEvent
e)<BR>{<BR>if(e.getSource()==btnc)<BR>{<BR>symbol='\0';<BR>x=0;<BR>y=0;<BR>n=0;<BR>a=false;<BR>b=false;<BR>result.setText("0.0");<BR>}<BR>if(e.getSource()==btn0)
<BR>{<BR>if(n==0)<BR>t=String.valueOf(x);<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x*=10;<BR>result.setText(String.valueOf(x));<BR>}<BR>else<BR>{<BR>++n;
<BR>if(n>1)<BR>t=t+"0";<BR>result.setText(t);<BR>}<BR>}<BR>if(e.getSource()==btn1)
<BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{ <BR>x=x*10+1;
<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*1;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn2)
<BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{<BR>x=x*10+2;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*2;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn3) <BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x=x*10+3;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*3;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn4) <BR>{
<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{<BR>x=x*10+4;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*4;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn5) <BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x=x*10+5;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*5;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn6) <BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x=x*10+6;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*6;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn7) <BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x=x*10+7;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*7;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn8) <BR>{<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x=x*10+8;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*8;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn9)
<BR>{<BR>if(n==0)<BR>t=String.valueOf(x);<BR>if(b==true)<BR>x=0;<BR>if(a==false)<BR>{
<BR>x=x*10+9;<BR>result.setText(String.valueOf(x));<BR>b=false;<BR>}<BR>else<BR>{<BR>++n;<BR>x=x+Math.pow(10,-n)*9;<BR>x=((double)Math.round(x*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(x));<BR>}<BR>t=String.valueOf(x);<BR>}
<BR>if(e.getSource()==btn10) <BR>{<BR>a=true;<BR>} <BR>if(e.getSource()==btn11)
<BR>{<BR>n=0;<BR>a=false;<BR>if(symbol=='\0')<BR>{ <BR>y=x;
<BR>}<BR>else<BR>{<BR>switch(symbol)<BR>{<BR>case
'+':<BR>y=y+x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'-':<BR>y=y-x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'*':<BR>y=y*x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'/':<BR>if(x!=0)<BR>{<BR>y=y/x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>}<BR>else<BR>result.setText("Error");break;
<BR>default: break; <BR>}<BR>}<BR>b=true;<BR>symbol='+'; <BR>x=y; <BR>}
<BR>if(e.getSource()==btn12) <BR>{<BR>n=0;<BR>a=false;<BR>if(symbol=='\0')<BR>{
<BR>y=x;<BR>}<BR>else<BR>{<BR>switch(symbol)<BR>{<BR>case
'+':<BR>y=y+x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'-':<BR>y=y-x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'*':<BR>y=y*x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'/':<BR>if(x!=0)<BR>{<BR>y=y/x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>}<BR>else<BR>result.setText("Error");break;
<BR>default: break; <BR>}<BR><BR>}<BR>b=true;<BR>symbol='-';<BR>x=y; <BR>}
<BR>if(e.getSource()==btn13) <BR>{<BR>n=0;<BR>a=false;<BR>if(symbol=='\0')<BR>{
<BR>y=x;<BR>}<BR>else<BR>{<BR>switch(symbol)<BR>{<BR>case
'+':<BR>y=y+x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'-':<BR>y=y-x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'*':<BR>y=y*x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'/':<BR>if(x!=0)<BR>{<BR>y=y/x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>}<BR>else<BR>result.setText("Error");break;
<BR>default: break; <BR>}<BR>}<BR>b=true; <BR>symbol='*';<BR>x=y; <BR>}
<BR>if(e.getSource()==btn14)
<BR>{<BR>n=0;<BR>a=false;<BR>if(symbol=='\0')<BR>{<BR>y=x;<BR>}<BR>else<BR>{<BR>switch(symbol)<BR>{<BR>case
'+':<BR>y=y+x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'-':<BR>y=y-x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'*':<BR>y=y*x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>break;<BR>case
'/':<BR>if(x!=0)<BR>{<BR>y=y/x;<BR>y=((double)Math.round(y*(1e+13)))/(1e+13);<BR>result.setText(String.valueOf(y));<BR>}<BR>else<BR>result.setText("Error");break;
<BR>default: break; <BR>}<BR>}<BR>symbol='/';<BR>b=true;<BR>x=y; <BR>}
<BR>if(e.getSource()==btn15)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -