📄 calculator.java
字号:
//编制人员:秦星 学号:G0417015
package pack_calculator;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends WindowAdapter implements ActionListener
{
static int tag1,tag2,tag3;
static double nd1,nd2,nd3;
static Double dtemp;
static String ns1,ns2,ns3,op1,op2,op3;
static TextField t;
Frame f;
//将String型字符变成double型数字
public double TurnTodou(String str)
{
Double dou1 = new Double(0);
Double dou2 = new Double(0);
dou2 = dou1.valueOf(str);
double d = dou2.doubleValue();
return d;
}
//将Double型数字变成String型字符
public String TurnToStr(Double dou)
{
String str;
double d = dou.doubleValue();
str = dou.toString(d);
return str;
}
//得到运算优先级比较数字
public int CompareNum(String str)
{
int i=0;
if(str.equals("+")==true||str.equals("-")==true){i=1;}
if(str.equals("*")==true||str.equals("/")==true){i=2;}
return i;
}
//当遇到分母为零时的清零函数
public void ClearText()
{
ns1="$";ns2="$";ns3="$";
op1="$";op2="$";op3="$";
nd1=0;nd2=0;nd3=0;
tag1=0;tag2=0;tag3=0;
t.setText("E");
}
//检验计算结果是否为小数
public int CheckFloat(String str)
{
int j=0;
for(int k=0;k<str.length();k++)
{
if(str.charAt(k)=='.') {j=1;break;}
else {j=0;}
}
return j;
}
//主函数
public static void run()
{
Calculator mcal = new Calculator();
mcal.MyWindowShow();
}
public void MyWindowShow()
{//变量初始化
ns1=new String("$");ns2=new String("$");ns3=new String("$");
op1=new String("$");op2=new String("$");op3=new String("$");
nd1=0;nd2=0;nd3=0;
tag1=0;tag2=0;tag3=0;
t=new TextField("0");
t.setEnabled(false);
//窗体初始化
int i=0;
f = new Frame("MyCalculator!");
Panel p = new Panel();
t = new TextField("0");
Button b[] = new Button[17];//"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/","CLS"
b[0] = new Button("7");b[1] = new Button("8");b[2] = new Button("9");b[3] = new Button("+");
b[4] = new Button("4");b[5] = new Button("5");b[6] = new Button("6");b[7] = new Button("-");
b[8] = new Button("1");b[9] = new Button("2");b[10] = new Button("3");b[11] = new Button("*");
b[12] = new Button("0");b[13] = new Button(".");b[14] = new Button("=");b[15] = new Button("/");
b[16] = new Button("CLS");
f.add(t,"North");
f.add(p,"Center");
f.add(b[16],"South");
p.setLayout(new GridLayout(4,4));
for(i=0;i<=15;i++) {p.add(b[i]);}
f.addWindowListener(this);//注册窗体兼听程序
for(i=0;i<=16;i++) {b[i].addActionListener(this);}//注册按钮按动兼听程序
f.setSize(300,250);
f.setVisible(true);
}
//覆盖WindowAdapter类中的windowClosing()方法
public void windowClosing(WindowEvent e) {f.setVisible(false);}
//覆盖ActionListener接口中的actionPerformed()方法
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(".")==true) {tag1++;}
if(e.getActionCommand().equals(".")==false&&tag1>=2) {tag1=1;}
if(e.getActionCommand().equals("CLS")==false)
{
if(e.getActionCommand().equals(".")==true||e.getActionCommand().equals("0")==true||e.getActionCommand().equals("1")==true||e.getActionCommand().equals("2")==true||e.getActionCommand().equals("3")==true||e.getActionCommand().equals("4")==true||e.getActionCommand().equals("5")==true||e.getActionCommand().equals("6")==true||e.getActionCommand().equals("7")==true||e.getActionCommand().equals("8")==true||e.getActionCommand().equals("9")==true)
{//tag1限制一个浮点数中小数点个数只能为1;tag2限制一个浮点数的有效数字是16个;当输入一个新的运算符时其值自动回零
if(op1.equals("$")==true)//输入第一个运算数
{
if(tag1<2&&tag2<=15)
{
if(ns1.equals("$")==true&&e.getActionCommand().equals(".")==true) {ns1="0"+e.getActionCommand();}
else
{
if(ns1.equals("$")==true&&e.getActionCommand().equals(".")==false) {ns1=e.getActionCommand();}
else
{
if(ns1.equals("$")==true) {ns1=e.getActionCommand();}
else
{
if(ns1.equals("0")==true&&e.getActionCommand().equals("0")==true) {ns1="0";}
else
{
if(ns1.equals("$")==false&&e.getActionCommand().equals(".")==true)
{
if(this.TurnTodou(ns1)==0){ns1="0.";tag3=0;}
else {ns1=ns1+e.getActionCommand();}
}
else
{
if(tag3==1) {ns1=e.getActionCommand();tag3=0;}
else
{
if(ns1.equals("0")==true&&e.getActionCommand().equals("0")==false) {ns1=e.getActionCommand();}
else{ns1=ns1+e.getActionCommand();}
}
}
}
}
}
}
}
tag2++;
t.setText(ns1);
}
if(op1.equals("$")==false&&op2.equals("$")==true)//输入第二个运算数
{
if(tag1<2&&tag2<=15)
{
if(ns2.equals("$")==true&&e.getActionCommand().equals(".")==true)
{ns2="0"+e.getActionCommand();}
else
{
if(ns2.equals("$")==true&&e.getActionCommand().equals(".")==false){ns2=e.getActionCommand();}
else
{
if(ns2.equals("$")==true) {ns1=e.getActionCommand();}
else
{
if(ns2.equals("0")==true&&e.getActionCommand().equals("0")==true) {ns2="0";}
else {ns2=ns2+e.getActionCommand();}
}
}
}
}
tag2++;
t.setText(ns2);
}
if(op1.equals("$")==false&&op2.equals("$")==false)//输入第三个运算数
{
if(tag1<2&&tag2<=15)
{
if(ns3.equals("$")==true&&e.getActionCommand().equals(".")==true)
{ns3="0"+e.getActionCommand();}
else
{
if(ns3.equals("$")==true&&e.getActionCommand().equals(".")==false){ns3=e.getActionCommand();}
else
{
if(ns3.equals("$")==true) {ns1=e.getActionCommand();}
else
{
if(ns3.equals("0")==true&&e.getActionCommand().equals("0")==true) {ns3="0";}
else {ns3=ns3+e.getActionCommand();}
}
}
}
}
tag2++;
t.setText(ns3);
}
}
if(e.getActionCommand().equals("+")==true||e.getActionCommand().equals("-")==true||e.getActionCommand().equals("*")==true||e.getActionCommand().equals("/")==true)
{//利用堆栈的原理,根据优先级,进行计算,在只有+-*/四种运算时只有三个变量就可以储存相继的运算符
tag1=0;tag2=0;
if(op1.equals("$")==true||ns2.equals("$")==true)//储存一个运算符时的计算
{
op1=e.getActionCommand();
if(ns1.equals("$")==true){ns1="0";}
}
if(op1.equals("$")==false&&ns2.equals("$")==false&&ns3.equals("$")==true)//储存二个运算符时的计算
{
op2=e.getActionCommand();
if(this.CompareNum(op1)>=this.CompareNum(op2))
{
nd1=this.TurnTodou(ns1);nd2=this.TurnTodou(ns2);
if(op1.equals("+")==true) {nd1=nd1+nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("-")==true) {nd1=nd1-nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("*")==true) {nd1=nd1*nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("/")==true)
{
if(nd2!=0) {nd1=nd1/nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
else
{
if(nd1==0) {ns1="1";}
else {this.ClearText();}
}
}
ns2="$";op1=op2;op2="$";
t.setText(ns1);
}
}
if(op2.equals("$")==false&&ns3.equals("$")==false)//储存三个运算符时的计算
{
op3=e.getActionCommand();
nd2=this.TurnTodou(ns2);nd3=this.TurnTodou(ns3);
if(op2.equals("+")==true) {nd2=nd2+nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
if(op2.equals("-")==true) {nd2=nd2-nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
if(op2.equals("*")==true) {nd2=nd2*nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
if(op2.equals("/")==true)
{
if(nd3!=0) {nd2=nd2/nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
else
{
if(nd2==0) {ns2="1";}
else {this.ClearText();}
}
}
ns3="$";op2=op3;op3="$";
if(this.CompareNum(op1)>=this.CompareNum(op2))
{
nd1=this.TurnTodou(ns1);nd2=this.TurnTodou(ns2);
if(op1.equals("+")==true) {nd1=nd1+nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("-")==true) {nd1=nd1-nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("*")==true) {nd1=nd1*nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("/")==true)
{
if(nd2!=0) {nd1=nd1/nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
else
{
if(nd1==0) {ns1="1";}
else {this.ClearText();}
}
}
ns2="$";op1=op2;op2="$";
t.setText(ns1);
}
}
}
if(e.getActionCommand().equals("=")==true)//输入等号时的计算
{
if(ns1.equals("$")==false&&ns2.equals("$")==true&&op1.equals("$")==false)
{
nd1=this.TurnTodou(ns1);
if(op1.equals("+")==true) {nd1=nd1+nd1;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("-")==true) {nd1=nd1-nd1;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("*")==true) {nd1=nd1*nd1;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("/")==true) {ns1="1";}
op1="$";tag1=0;tag2=0;tag3=1;t.setText(ns1);
}
if(ns1.equals("$")==false&&op1.equals("$")==false&&ns2.equals("$")==false&&ns3.equals("$")==true)
{
nd1=this.TurnTodou(ns1);nd2=this.TurnTodou(ns2);
if(op1.equals("+")==true) {nd1=nd1+nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("-")==true) {nd1=nd1-nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("*")==true) {nd1=nd1*nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("/")==true)
{
if(nd2!=0) {nd1=nd1/nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
else
{
if(nd1==0) {ns1="1";}
else {this.ClearText();}
}
}
ns2="$";op1="$";op2="$";tag1=0;tag2=0;tag3=1;t.setText(ns1);
}
if(ns1.equals("$")==false&&op1.equals("$")==false&&ns2.equals("$")==false&&op2.equals("$")==false&&ns3.equals("$")==false)
{
nd2=this.TurnTodou(ns2);nd3=this.TurnTodou(ns3);
if(op2.equals("+")==true) {nd2=nd2+nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
if(op2.equals("-")==true) {nd2=nd2-nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
if(op2.equals("*")==true) {nd2=nd2*nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
if(op2.equals("/")==true)
{
if(nd3!=0) {nd2=nd2/nd3;dtemp=new Double(nd2);ns2=this.TurnToStr(dtemp);}
else
{
if(nd2==0) {ns2="1";}
else {this.ClearText();}
}
}
ns3="$";op2="$";op3="$";
nd1=this.TurnTodou(ns1);nd2=this.TurnTodou(ns2);
if(op1.equals("+")==true) {nd1=nd1+nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("-")==true) {nd1=nd1-nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("*")==true) {nd1=nd1*nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
if(op1.equals("/")==true)
{
if(nd2!=0) {nd1=nd1/nd2;dtemp=new Double(nd1);ns1=this.TurnToStr(dtemp);}
else
{
if(nd1==0) {ns1="1";}
else {this.ClearText();}
}
}
ns2="$";op1="$";op2="$";tag1=0;tag2=0;tag3=1;t.setText(ns1);
}
}
}
else//清零
{
this.ClearText();
t.setText("0");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -