import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class computer extends Frame implements ActionListener{
Button[] button_number,button_calculater;
Button button_pointer;
Panel panel;
TextField field_number;
JLabel label_dialog;
Dialog dialog;
String[] str={"sqr","Del","CE","C","/","*","-","+","="};
String text_number="",text_string,text_calculater,string_calculater,string_number1,string_number2;
char text_char,judge_char;
int j=0;
boolean flag=true,sqr=true,pow=true;
Stack<String> operate=new Stack();
Stack<String> number=new Stack();
public computer(){
super("计算器");
this.setSize(300,200);
this.setLocation(200,150);
this.setLayout(new java.awt.BorderLayout());
this.setResizable(false);
this.addWindowListener(new WinClose());
dialog=new Dialog(this,"提示",true);
label_dialog=new JLabel("",JLabel.CENTER);
button_number=new Button[10];
button_calculater=new Button[10];
panel=new Panel(new java.awt.GridLayout(5,4,1,1));
field_number=new TextField("",10);
button_pointer=new Button("·");
button_pointer.addActionListener(this);
for(int i=0;i<10;i++){
button_number[i]=new Button(i+"");
button_number[i].addActionListener(this);
}
for(int i=0;i<9;i++){
button_calculater[i]=new Button(str[i]);
button_calculater[i].addActionListener(new calculater());
}
this.add("North",field_number);
this.add("Center",panel);
for(int i=0;i<4;i++)
panel.add(button_calculater[i]);
for(int i=0;i<3;i++)
panel.add(button_number[i+7]);
panel.add(button_calculater[4]);
for(int i=0;i<3;i++)
panel.add(button_number[i+4]);
panel.add(button_calculater[5]);
for(int i=0;i<3;i++)
panel.add(button_number[i+1]);
panel.add(button_calculater[6]);
panel.add(button_number[0]);
panel.add(button_pointer);
panel.add(button_calculater[7]);
panel.add(button_calculater[8]);
dialog.add(label_dialog);
dialog.setVisible(false);
dialog.setSize(300,100);
dialog.addWindowListener(new WinClose());
this.setVisible(true);
}
public static void main(String[] args){
new computer();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button_pointer){
text_string=field_number.getText();
text_char=text_string.charAt(text_string.length()-1);
if(text_char=='+'||text_char=='-'||text_char=='*'||text_char=='/')
field_number.setText(field_number.getText());
else
field_number.setText(field_number.getText()+".");
}
else{
if(field_number.getText().equals("0"))
field_number.setText("");
field_number.setText(field_number.getText()+e.getActionCommand());
}
}
class WinClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
if(e.getSource()==dialog){
dialog.setVisible(false);
field_number.setText("");
}
else
System.exit(0);
}
}
class calculater implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
if(e.getSource()==button_calculater[1]){
text_string=field_number.getText();
field_number.setText(text_string.substring(0,text_string.length()-1));
}
}
catch(StringIndexOutOfBoundsException b){}
if(e.getSource()==button_calculater[2])
field_number.setText("0");
for(int i=0;i<4;i++)
if(e.getSource()==button_calculater[i+4]){
if(field_number.getText().equals("0"))
field_number.setText("");
field_number.setText(field_number.getText()+e.getActionCommand());
}
if(e.getSource()==button_calculater[8]){
text_string=field_number.getText();
text_string+="#";
operate.push("#");
for(j=0;j<text_string.length();j++){
text_char=text_string.charAt(j);
if(text_char!='#'&&text_char!='+'&&text_char!='-'&&text_char!='*'&&text_char!='/')
text_number+=String.valueOf(text_char);
else{
text_calculater=String.valueOf(text_char);
if(flag) number.push(text_number);
text_number="";
if(operate.peek().equals("+")&&(text_calculater.equals("+")||text_calculater.equals("-")))
judge_char='>';
else if(operate.peek().equals("-")&&(text_calculater.equals("+")||text_calculater.equals("-")))
judge_char='>';
else if(operate.peek().equals("+")&&(text_calculater.equals("*")||text_calculater.equals("/")))
judge_char='<';
else if(operate.peek().equals("-")&&(text_calculater.equals("*")||text_calculater.equals("/")))
judge_char='<';
else if(operate.peek().equals("#"))
judge_char='<';
else
judge_char='>';
switch(judge_char){
case'<':operate.push(text_calculater);flag=true;break;
case'>':new operate();break;
}
}
}
field_number.setText(number.pop());
}
if(e.getSource()==button_calculater[0]){
text_string=field_number.getText();
for(int i=0;i<text_string.length();i++){
text_char=text_string.charAt(i);
if(text_char=='+'||text_char=='-'||text_char=='*'||text_char=='/'){
label_dialog.setText("所输内容不能为表达式或负数,请重新输入");
dialog.setLocation(300+120,200+100);
dialog.setVisible(true);
sqr=false;
break;
}
}
if(sqr) field_number.setText(""+(Math.sqrt(Double.parseDouble(text_string))));
}
if(e.getSource()==button_calculater[3]){
text_string=field_number.getText();
for(int i=0;i<text_string.length();i++){
text_char=text_string.charAt(i);
if(text_char=='+'||text_char=='-'||text_char=='*'||text_char=='/'){
label_dialog.setText("所输内容不能为表达式,请重新输入");
dialog.setLocation(300+120,200+100);
dialog.setVisible(true);
pow=false;
break;
}
}
if(pow) field_number.setText(""+(Math.pow(Double.parseDouble(text_string),2)));
}
}
class operate{
public operate(){
string_calculater=operate.pop();
string_number2=number.pop();
string_number1=number.pop();
if(string_calculater.equals("+"))
number.push(""+(Double.parseDouble(string_number1)+Double.parseDouble(string_number2)));
else if(string_calculater.equals("-"))
number.push(""+(Double.parseDouble(string_number1)-Double.parseDouble(string_number2)));
else if(string_calculater.equals("*"))
number.push(""+(Double.parseDouble(string_number1)*Double.parseDouble(string_number2)));
else
number.push(""+(Double.parseDouble(string_number1)/Double.parseDouble(string_number2)));
j--;
flag=false;
}
}
}
}
可进行表达式的运算。
缺点:可连续输入小数点,不能输入负数。