import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.regex.Pattern;
public class Cal extends WindowAdapter implements ActionListener
{

int i;
int nOp=1;
String OP;
int length;
double math1;
double math2;
String str;
String str1;
String str2;
private Frame frame;
private TextField text;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,b10,b11,badd,bminu,
bmuti,bdiv,bclear,b,bequal,bback;
private boolean keyAvailable=true;
public void go(){

frame = new Frame("计算器"); 
text = new TextField("",2);
text.setEditable(false);
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("."); 
b11 = new Button("+/-");
badd = new Button("+");
bminu = new Button("-");
bmuti = new Button("*");
bdiv = new Button("/");
bequal = new Button("=");
bclear = new Button("clear"); 
b = new Button("1/x");
bback = new Button("back");
frame.add(text,BorderLayout.NORTH);
Panel panel = new Panel();
panel.setLayout(new GridLayout(5,4,1,1));
Button[] bt = {bclear,b,bback,bequal,b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,b10,b11,badd,bminu,
bmuti,bdiv};
for(Button bu:bt)
{
panel.add(bu);bu.addActionListener(this);
}
frame.add(panel,BorderLayout.CENTER);

frame.addWindowListener(this);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}


public void windowClosing(WindowEvent e) {
System.exit(0);
}

public static void main(String[] args) {
Cal cal = new Cal();
cal.go();


}
public void actionPerformed(ActionEvent e) {
if (keyAvailable&&e.getActionCommand().length() == 1
&& e.getActionCommand().compareTo("0") >= 0
&& e.getActionCommand().compareTo("9") <= 0)
{
 str = (String)text.getText()+e.getActionCommand();
text.setText(str);
 length = str.length();
}
if(e.getActionCommand()=="clear")
{
 str = "";
text.setText(str);
}

int count =str.length();
if(e.getActionCommand()=="back")
{
str = this.text.getText();
for(int i = 0;i<count;i++)
{
str =str.substring(0,count-1);
this.text.setText(str);

}
count--;

}

if(e.getActionCommand()=="+")
{
 OP = "+";
 str = str +OP;
 this.text.setText(str);
 nOp =1;
 

}
if(e.getActionCommand()=="-")
{
OP ="-";
 str = str +OP;
 this.text.setText(str);
nOp =2;
}
if(e.getActionCommand()=="*")
{
OP ="*";
 str = str +OP;
 this.text.setText(str);
nOp =3;
}
if(e.getActionCommand()=="+/-")
{
double d = Double.parseDouble(str); 
d = 0-d;
str = String.valueOf(d);
this.text.setText(str);
}
if(e.getActionCommand()==".")
{
OP =".";
 str = str +OP;
 this.text.setText(str);
}
if(e.getActionCommand()=="/")
{OP ="/";
 str = str +OP;
 this.text.setText(str);
nOp =4;
}
if(e.getActionCommand()=="1/x")
{
str = this.text.getText();
math1 = Double.parseDouble(str);
math1 = 1/math1;
str = String.valueOf(math1);
this.text.setText(str);

}
if(e.getActionCommand()=="=")
{

switch (nOp){
case 1:
 i = str.indexOf("+");
str1 = str.substring(0,i);
math1 = Double.parseDouble(str1);
    str2 = str.substring(i+1);
math2 = Double.parseDouble(str2);
math1 += math2;
str = String.valueOf(math1);


this.text.setText(str);

break;
case 2:
 i = str.indexOf("-");
str1 = str.substring(0,i);
math1 = Double.parseDouble(str1);
str2 = str.substring(i+1);
math2 = Double.parseDouble(str2);
math1 -= math2;
str = String.valueOf(math1);
this.text.setText(str);
break;
case 3:
i = str.indexOf("*");
str1 = str.substring(0,i);
str2 = str.substring(i+1);
math1 = Double.parseDouble(str1);
math2 = Double.parseDouble(str2);
math1 *= math2;
str = String.valueOf(math1);
this.text.setText(str);
break;

case 4:
i = str.indexOf("/");
str1 = str.substring(0,i);
str2 = str.substring(i+1);
math1 = Double.parseDouble(str1);
math2 = Double.parseDouble(str2);
if(math2==0)
{
this.text.setText("除数不能为零");
}
else
{
math1 /=math2;
str = String.valueOf(math1);
this.text.setText(str);
}
break;
}
} }

}