public void actionPerformed(ActionEvent e){
String act=e.getActionCommand();
if(e.getSource()==backspace){
f.setText(f.getText().substring(0, f.getText().length()-1));
}
if(e.getSource()==ce){
f.setText("0");
return;
}
if(e.getSource()==c){
f.setText("0");
ch='#';
}
if(act!="1/x"&&act.charAt(0)>='0'&&act.charAt(0)<='9'){
if(can){
f.setText(act);
can=false;
}
else{
try{
if(Float.parseFloat(f.getText())==0){
if(f.getText().equals("0.")){
f.setText(f.getText()+act);
}
else{
f.setText(act);
}
return;
}
else{
f.setText(f.getText()+act);
return;
}
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(null,"输入格式错误!","警告!",JOptionPane.ERROR_MESSAGE);
return;
}
}
if(act.equals("+")||act.equals("-")||act.equals("*")||act.equals("/")){
if(ch!='#'){
try{
num=Calculator(num,ch,Float.parseFloat(f.getText()));
f.setText(String.valueOf(num));
ch=act.charAt(0);
can=true;
return;
}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",
JOptionPane.ERROR_MESSAGE);
return;
}
}
else{
try{
num=
Float.parseFloat(f.getText());
ch=act.charAt(0);
can=true;
return;
}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",
JOptionPane.ERROR_MESSAGE);return;}
}
}
if(act.equals("=")){
can=true;
try{
if(ch=='#'){
return;
}
float numm=Float.parseFloat(f.getText());
num=Calculator(num,ch,numm);
f.setText(String.valueOf(num));
ch='#';
return;
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(null,"输入格式错误!","警告!",JOptionPane.ERROR_MESSAGE);
return;
} }
}
}
public float Calculator(float a,char c,float b){
float sum=0;
switch(c){
case'+':sum=a+b;break;
case'-':sum=a-b;break;
case'*':sum=a*b;break;
case'/':
if(b==0){
JOptionPane.showMessageDialog(null,"除0错误!","警告!", JOptionPane.ERROR_MESSAGE);
return 0;
}
sum=a/b;
break;
}
return sum;
}
String act=e.getActionCommand();
if(e.getSource()==backspace){
f.setText(f.getText().substring(0, f.getText().length()-1));
}
if(e.getSource()==ce){
f.setText("0");
return;
}
if(e.getSource()==c){
f.setText("0");
ch='#';
}
if(act!="1/x"&&act.charAt(0)>='0'&&act.charAt(0)<='9'){
if(can){
f.setText(act);
can=false;
}
else{
try{
if(Float.parseFloat(f.getText())==0){
if(f.getText().equals("0.")){
f.setText(f.getText()+act);
}
else{
f.setText(act);
}
return;
}
else{
f.setText(f.getText()+act);
return;
}
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(null,"输入格式错误!","警告!",JOptionPane.ERROR_MESSAGE);
return;
}
}
if(act.equals("+")||act.equals("-")||act.equals("*")||act.equals("/")){
if(ch!='#'){
try{
num=Calculator(num,ch,Float.parseFloat(f.getText()));
f.setText(String.valueOf(num));
ch=act.charAt(0);
can=true;
return;
}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",
JOptionPane.ERROR_MESSAGE);
return;
}
}
else{
try{
num=
Float.parseFloat(f.getText());
ch=act.charAt(0);
can=true;
return;
}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",
JOptionPane.ERROR_MESSAGE);return;}
}
}
if(act.equals("=")){
can=true;
try{
if(ch=='#'){
return;
}
float numm=Float.parseFloat(f.getText());
num=Calculator(num,ch,numm);
f.setText(String.valueOf(num));
ch='#';
return;
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(null,"输入格式错误!","警告!",JOptionPane.ERROR_MESSAGE);
return;
} }
}
}
public float Calculator(float a,char c,float b){
float sum=0;
switch(c){
case'+':sum=a+b;break;
case'-':sum=a-b;break;
case'*':sum=a*b;break;
case'/':
if(b==0){
JOptionPane.showMessageDialog(null,"除0错误!","警告!", JOptionPane.ERROR_MESSAGE);
return 0;
}
sum=a/b;
break;
}
return sum;
}