📄 jsq.java
字号:
import java.awt.*;
import java.awt.event.*;
public class jsq extends Frame implements ActionListener{
float a[]=new float[101];
float b[]=new float[101];
String s1,s2,s3,s,op="",op1="",op2="",op3="",op4="",span="",spanH="H";
boolean t=false,t2=false,t3=true,tzuo=false,tyou=false;
int i=0,y=0,u2=0;
float n=0,n4=0;
/* 说明
s这个变量用于获取当前tf的字符串;
op用于数组A的乘除判断,op1用于减的判断;op2用语数组B,OP3功能和OP1一样,只是用于数组B;
SPAN和SPANH用于二进制和十六进制的比较;
T用于加减乘除的判断;t2用于是不是继续输入数字的判断,如果为假,则另外输入;t3用于其他按钮,如x^y有没有按下去等;
Tzuo是用来判断左括号有没有按下去;TYOU...;
N和N4分别是数组A和B的存放临时数的地方;
*/
Button bt=new Button("退出");
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
Button b0=new Button("0");
Button space=new Button("退格");
Button CE=new Button("CE");
Button C= new Button("C");
Button chu= new Button("/");
Button genhao= new Button("sqrt");
Button dian=new Button(".");
Button zhengfu=new Button("+/-");
Button cheng=new Button("*");
Button jian=new Button("-");
Button jia=new Button("+");
Button dengyu=new Button("=");
Button erjinzhi=new Button("D-B");
Button shiliujinzhi=new Button("D-H");
Button quzheng=new Button("Int");
Button qiuyu=new Button("Mod");
Button xycifang=new Button("x^y");
Button xsancifang=new Button("x^3");
Button xercifang=new Button("x^2");
Button jieceng=new Button("n!");
Button zuokuahao=new Button("(");
Button youkuahao=new Button(")");
TextField tf=new TextField(30);
TextField tf2=new TextField(60);
MenuBar mb=new MenuBar();
Menu m1= new Menu("编辑(E)");
MenuItem copy=new MenuItem("复制(C)",new MenuShortcut('C'));
MenuItem paste=new MenuItem("粘贴(p)",new MenuShortcut('V'));
MenuItem exit=new MenuItem("退出(X)",new MenuShortcut('X'));
Menu m2= new Menu("查看(V)" );
MenuItem bzx=new MenuItem("标准型(T)");
MenuItem kxx=new MenuItem("科学型(S)");
MenuItem szfz=new MenuItem("数字分组(I)");
Menu m3= new Menu("帮助(H)");
MenuItem help=new MenuItem("帮助主题");
MenuItem about=new MenuItem("关于计算器");
jsq(){
super("Windows");
space.setForeground(Color.red);
CE.setForeground(Color.red);
C.setForeground(Color.red);
genhao.setForeground(Color.red);
chu.setForeground(Color.red);
cheng.setForeground(Color.red);
jia.setForeground(Color.red);
dengyu.setForeground(Color.red);
zuokuahao.setForeground(Color.red);
youkuahao.setForeground(Color.red);
setBackground(Color.black);
setForeground(Color.blue);
jian.setForeground(Color.red);
erjinzhi.setForeground(Color.red);
shiliujinzhi.setForeground(Color.red);
xycifang.setForeground(Color.red);
xsancifang.setForeground(Color.red);
xercifang.setForeground(Color.red);
qiuyu.setForeground(Color.red);
quzheng.setForeground(Color.red);
jieceng.setForeground(Color.red);
setSize(260,240);
setLayout(null);
space.setLocation(60,65);
space.setSize(60,30);
CE.setLocation(120,65);
CE.setSize(60,30);
C.setLocation(180,65);
C.setSize(60,30);
add(CE);
add(C);
b6.setLocation(60,95);
b6.setSize(30,40);
add(b6);
b7.setLocation(90,95);
b7.setSize(30,40);
add(b7);
b8.setLocation(120,95);
b8.setSize(30,40);
add(b8);
b9.setLocation(150,95);
b9.setSize(30,40);
add(b9);
chu.setLocation(180,95);
chu.setSize(30,40);
add(chu);
genhao.setLocation(210,95);
genhao.setSize(30,40);
add(genhao);
b2.setLocation(60,135);
b2.setSize(30,40);
add(b2);
b3.setLocation(90,135);
b3.setSize(30,40);
add(b3);
b4.setLocation(120,135);
b4.setSize(30,40);
add(b4);
b5.setLocation(150,135);
b5.setSize(30,40);
add(b5);
b0.setLocation(60,175);
b0.setSize(30,40);
add(b0);
b1.setLocation(90,175);
b1.setSize(30,40);
add(b1);
dian.setLocation(120,175);
dian.setSize(30,40);
add(dian);
zhengfu.setLocation(150,175);
zhengfu.setSize(30,40);
add(zhengfu);
cheng.setLocation(180,135);
cheng.setSize(30,40);
add(cheng);
jian.setLocation(210,135);
jian.setSize(30,40);
add(jian);
jia.setLocation(180,175);
jia.setSize(30,40);
add(jia);
dengyu.setLocation(210,175);
dengyu.setSize(30,40);
add(dengyu);
erjinzhi.setLocation(0,65);
erjinzhi.setSize(30,30);
add(erjinzhi);
shiliujinzhi.setLocation(30,65);
shiliujinzhi.setSize(30,30);
add(shiliujinzhi);
xycifang.setLocation(0,95);
xycifang.setSize(30,40);
add(xycifang);
xsancifang.setLocation(0,135);
xsancifang.setSize(30,40);
add(xsancifang);
xercifang.setLocation(30,95);
xercifang.setSize(30,40);
add(xercifang);
jieceng.setLocation(30,135);
jieceng.setSize(30,40);
add(jieceng);
qiuyu.setLocation(0,175);
qiuyu.setSize(30,40);
add(qiuyu);
quzheng.setLocation(30,175);
quzheng.setSize(30,40);
add(quzheng);
zuokuahao.setLocation(0,215);
zuokuahao.setSize(30,40);
add(zuokuahao);
youkuahao.setLocation(30,215);
youkuahao.setSize(30,40);
add(youkuahao);
tf2.setLocation(60,215);
tf2.setSize(180,40);
tf.setLocation(-1,45);
tf.setSize(240,20);
add(tf2);
add(tf);
add(space);
exit.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);
b0.addActionListener(this);
CE.addActionListener(this);
C.addActionListener(this);
dian.addActionListener(this);
quzheng.addActionListener(this);
dengyu.addActionListener(this);
qiuyu.addActionListener(this);
space.addActionListener(this);
xercifang.addActionListener(this);
xycifang.addActionListener(this);
xsancifang.addActionListener(this);
jieceng.addActionListener(this);
cheng.addActionListener(this);
jia.addActionListener(this);
jian.addActionListener(this);
chu.addActionListener(this);
erjinzhi.addActionListener(this);
shiliujinzhi.addActionListener(this);
zhengfu.addActionListener(this);
zuokuahao.addActionListener(this);
youkuahao.addActionListener(this);
m1.add(copy);
m1.add(paste);
m1.add(exit);
mb.add(m1);
m2.add(bzx);
m2.add(kxx);
m2.addSeparator();
m2.add(szfz);
mb.add(m2);
m3.add(help);
m3.add(about);
mb.add(m3);
setMenuBar(mb);
show();
setVisible(true);
tf.setText("0");
}
public static void main(String f[])
{
new jsq();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==exit)
System.exit(0);
if(e.getSource()==b1) //按纽1
{String s = tf.getText();
float k;
k=Float.parseFloat(s);
t3=false;
if(t2) s+="1";
else {
if(t) {s="1";u2=0;}
else {if(s==""||k==0) s="1";
else
s+="1";}}
t=false;
tf.setText(s);
tf2.setText(tf2.getText()+"1");
}
if(e.getSource()==b2) //按纽2
{String s = tf.getText();
float k;
t3=false;
if(t2) s+="2";
else {
k=Float.parseFloat(s);
if(t) {s="2";u2=0;}
else {if(s==""||k==0) s="2";
else
s+="2";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"2");
}
if(e.getSource()==b3)//按纽3
{String s = tf.getText();
float k;
t3=false;
k=Float.parseFloat(s);
if(t2) s+="3";
else {
if(t) {s="3";u2=0;}
else {if(s==""||k==0) s="3";
else
s+="3";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"3");
}
if(e.getSource()==b4)//按纽4
{String s = tf.getText();
float k;
t3=false;
k=Float.parseFloat(s);
if(t2) s+="4";
else {
if(t) {s="4";u2=0;}
else {if(s==""||k==0) s="4";
else
s+="4";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"4");
}
if(e.getSource()==b5)//按纽5
{String s = tf.getText();
float k;
t3=false;
k=Float.parseFloat(s);
if(t2) s+="5";
else {
if(t) {s="5";u2=0;}
else {if(s==""||k==0) s="5";
else
s+="5";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"5");
}
if(e.getSource()==b6)//按纽6
{String s = tf.getText();
float k;
t3=false;
k=Float.parseFloat(s);
if(t2) s+="6";
else {
if (t) {s="6";u2=0;}
else {
if(s==""||k==0) s="6";
else
s+="6";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"6");
}
if(e.getSource()==b7)//按纽7
{String s = tf.getText();
t3=false;
float k;
if(t2) s+="7";
else {
if(t) {s="7";u2=0;}
else {
k=Float.parseFloat(s);
if(s==""||k==0) s="7";
else
s+="7";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"7");
}
if(e.getSource()==b8)//按纽8
{String s = tf.getText();
t3=false;
float k;
k=Float.parseFloat(s);
if(t2) s+="8";
else {
if(t) {s="8";u2=0;}
else {
if(s==""||k==0) s="8";
else
s+="8";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"8");
}
if(e.getSource()==b9)//按纽9
{String s = tf.getText();
float k;
t3=false;
k=Float.parseFloat(s);
if(t2) s+="9";
else {
if(t) {s="9";u2=0;}
else {
if(s==""||k==0) s="9";
else
s+="9";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"9");
}
if(e.getSource()==b0) //按纽0
{String s = tf.getText();
float k;
k=Float.parseFloat(s);
if(t2) s+="0";
else {
if (t) {s="0";u2=0;}
else {
if(s==""||k==0) s="0";
else
s+="0";}}
t=false;
t2=true;
tf.setText(s);
tf2.setText(tf2.getText()+"0");
}
if(e.getSource()==CE)//置零1
{String s = tf.getText();
int c;
for(c=0;c<=20;c++)
a[c]=0;
for(c=0;c<=100;c++)
b[c]=0;
s = "0";
i=0;
y=0;
tf.setText(s);
t2=false;
op="";
op1="";
op2="";
op3="";
t3=true;
u2=0;
n=0;
n4=0;
tzuo=false;
tyou=false;
tf2.setText("");
op4="";
}
if(e.getSource()==C)//置零2
{String s = tf.getText();
int c;
s = "0";
i=0;
op4="";
y=0;
n=0;
n4=0;
for(c=0;c<=20;c++)
a[c]=0;
for(c=0;c<=100;c++)
b[c]=0;
tf.setText(s);
t2=false;
op="";
op1="";
op2="";
op3="";
t3=true;
u2=0;
tzuo=false;
tyou=false;
tf2.setText("");
}
if(e.getSource()==zhengfu)//正负数切换
{String s=tf.getText();
float k;
k=Float.parseFloat(s);
k=0-k;
s=String.valueOf(k);
tf.setText(s);
}
if(e.getSource()==dian)//点
{String s = tf.getText();
float k;
k=Float.parseFloat(s);
u2+=1;
t2=true;
if(u2<2)
s+=".";
t=false;
tf.setText(s);
tf2.setText(tf2.getText()+".");
}
if(e.getSource()==quzheng)//取整数
{String s = tf.getText();
int n;
float f;
f=Float.parseFloat(s);
n=(int)f;
s=String.valueOf(n);
tf.setText(s);
t3=true;
t=true;
}
if(e.getSource()==qiuyu) //求余数
{
s1 = tf.getText();
op="%";
s=s1;
tf.setText(s);
t=true;
t2=false;
t3=true;
tf2.setText(tf2.getText()+"%");
}
if(e.getSource()==space) //退格键
{String s = tf.getText(),s2;
int k;
k=s.length();
if(k!=0)
s2=s.substring(0,k-1);
else
s2="";
tf.setText(s2);
}
if(e.getSource()==xycifang)//xy次方
{
s1 = tf.getText();
op4="x^y";
s=s1;
t=true;
t2=false;
tf.setText(s);
t3=true;
tf2.setText(tf2.getText()+"^");
}
if(e.getSource()==xercifang)//X二次方
{ int i;
float k,j=1;;
s1 = tf.getText();
k=Float.parseFloat(s1);
for(i=1;i<=2;i++)
j*=k;
s=String.valueOf(j);
t=true;
tf.setText(s);
t3=true;
tf2.setText(tf2.getText()+"^2");
}
if(e.getSource()==xsancifang)//X三次方
{ int i;
float k,j=1;
s1 = tf.getText();
k=Float.parseFloat(s1);
for(i=1;i<=3;i++)
j*=k;
s=String.valueOf(j);
t=true;
tf.setText(s);
t3=true;
tf2.setText(tf2.getText()+"^3");
}
if(e.getSource()==jieceng)
{ int i,j=1,k;
s1 = tf.getText();
k=Integer.parseInt(s1);
for(i=1;i<=k;i++)
j*=i;
s=String.valueOf(j);
t=true;
tf.setText(s);
t3=true;
}
if(e.getSource()==cheng)//乘法
{
float n1,n2,n3=0,ncifang,njilei;
int j,k;
s1=tf.getText();
n1=Float.parseFloat(s1);
if(op4=="x^y")
{
k=(int)n1;
njilei=1;
ncifang=Float.parseFloat(s);
for(j=1;j<=k;j++)
njilei=njilei*ncifang;
tf.setText(String.valueOf(njilei));
}
s1=tf.getText();
n1=Float.parseFloat(s1);
if(tyou)
{
t=true;
t2=false;
if(op=="")
{ n=n1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -