|  | 
 
| 本帖最后由 天使怪盗Dick 于 2011-1-7 12:54 编辑 
 复制代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//界面
public class jiemian extends JFrame
{
     JFrame c;
          
     JTextField tf=new JTextField(23);
    
     JButton b1=new JButton("1");
     JButton b2=new JButton("2");
     JButton b3=new JButton("3");
     JButton b4=new JButton("4");
     JButton b5=new JButton("5");
     JButton b6=new JButton("6");
     JButton b7=new JButton("7");
     JButton b8=new JButton("8");
     JButton b9=new JButton("9");
     JButton b0=new JButton("0");
     JButton bjia=new JButton("+");
     JButton bjian=new JButton("-");
     JButton bcheng=new JButton("*");
     JButton bchu=new JButton("/");
     JButton blk=new JButton("(");
     JButton brk=new JButton(")");
     JButton bdeng=new JButton("=");
     JButton bdian=new JButton(".");
     JButton bfu=new JButton("+/-");
     JButton bmc=new JButton("MC");
     JPanel p1=new JPanel();
     JPanel p2=new JPanel();
     JPanel p3=new JPanel();
     String tf1=new String();
 void disp(){
  c=new JFrame("计算器");
  c.setResizable(false);
     c.setBounds(200,200,300,300);
     p3.add(tf);
     c.add(p3,"North");
     c.add(p1,"West");
     p1.setLayout(new GridLayout(4,3,4,3));
     p1.add(b1);
     p1.add(b2);
     p1.add(b3);
     p1.add(b4);
     p1.add(b5);
     p1.add(b6);
     p1.add(b7);
     p1.add(b8);
     p1.add(b9);
     p1.add(b0);
     p1.add(bdian);
     p1.add(bfu);
     b1.addActionListener(new Press1());
     b2.addActionListener(new Press2());
     b3.addActionListener(new Press3());
     b4.addActionListener(new Press4());
     b5.addActionListener(new Press5());
     b6.addActionListener(new Press6());
     b7.addActionListener(new Press7());
     b8.addActionListener(new Press8());
     b9.addActionListener(new Press9());
     b0.addActionListener(new Press0());
     bjia.addActionListener(new Pressjia());
     bjian.addActionListener(new Pressjian());
     bcheng.addActionListener(new Presscheng());
     bchu.addActionListener(new Presschu());
     blk.addActionListener(new Presslk());
     brk.addActionListener(new Pressrk());
     bmc.addActionListener(new Pressmc());
     bdeng.addActionListener(new Pressdeng());
     bfu.addActionListener(new Pressfu());
     c.add(p2,"East");
     p2.setLayout(new GridLayout(4,2,2,2));
     p2.add(bjia);
     p2.add(bmc);
     p2.add(bjian);
     p2.add(blk);
     p2.add(bcheng);
     p2.add(brk);
     p2.add(bchu);
     p2.add(bdeng);
 
     c.setVisible(true);
 }
    class Press0 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"0");
   tf1=tf1+"0";
   }
  
 }
 class Press1 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"1");
   tf1=tf1+"1";
  }
 }
 class Press2 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"2");
   tf1=tf1+"2";
  }
 }
 class Press3 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"3");
   tf1=tf1+"3";
  }
 }
 class Press4 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"4");
   tf1=tf1+"4";
  }
 }
 class Press5 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"5");
   tf1=tf1+"5";
  }
 }
 class Press6 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"6");
   tf1=tf1+"6";
  }
 }
 class Press7 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"7");
   tf1=tf1+"7";
  }
 }
 class Press8 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"8");
   tf1=tf1+"8";
  }
 }
 class Press9 implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"9");
   tf1=tf1+"9";
  }
 }
 class Pressjia implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"+");
   tf1=tf1+"+";
  }
 }
 class Pressjian implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"-");
   tf1=tf1+"-";
  }
 }
 class Presscheng implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"*");
   tf1=tf1+"*";
  }
 }
 class Presschu implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"/");
   tf1=tf1+"/";
  }
 }
 class Presslk implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"(");
   tf1=tf1+"(";
  }
 }
 class Pressrk implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+")");
   tf1=tf1+")";
  }
 }
 class Pressfu implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String a=tf.getText();
   tf.setText(a+"-");
   tf1=tf1+"0-";
  }
 }
 class Pressmc implements ActionListener{
  public void actionPerformed(ActionEvent e){   
   tf.setText("");
   tf1=new String();
  }
 }
 class Pressdeng implements ActionListener{
  public void actionPerformed(ActionEvent e){
   String f=tf1;
   
   int i=0,j=0,n=0,flag=1;char []d=new char[100];char []a=new char[100];char x;
   f.getChars(0,f.length(),a,0);a[f.length()]='#';a[f.length()+1]='\0';
   
     
   ysf b=new ysf();
   sz c=new sz();
   //b.push_ysf('#');
   while(a[i]!='#'){
    
     if(a[i]=='('){
      b.push_ysf('(');i++;}
     else if(a[i]==')'){
      if(b.i!=-1){
       x=b.top_ysf();
         if(x=='(')b.cz_ysf();
         else flag=0;i++;}
      else {tf.setText("括号不匹配");i++;flag=0;}
      //if(flag==0)
      //System.out.print("括号不匹配!");
      }
     else i++;
     
    }
    if(b.empty_ysf()==0&&flag==1)
    
    
    tf.setText("括号不匹配");
    
    if(b.empty_ysf()==1&&flag==1){
    i=0;b.push_ysf('#');
    while(b.i!=-1&&a[i]!='\0'){
     if(a[i]>='0'&& a[i]<='9'||a[i]=='.'){
        
                 d[j++]=a[i];}
              else{
               if(a[i]!='#')d[j++]=' ';
               switch(a[i]){
                case'(':b.push_ysf('(');break;
                case')':x=b.cz_ysf();
                        while(x!='('){
                       d[j++]=x;
                       x=b.cz_ysf();
                         }break;
                   default:x=b.top_ysf();while(b.i!=-1&&b.bj(x)>=b.bj(a[i])){
                 if(x!='#')d[j++]=x;
                 b.cz_ysf();if(b.i!=-1)x=b.top_ysf();
                 }
                 if(a[i]!='#')b.push_ysf(a[i]);
                }
               
               }
              
              i++;
    }
    d[j]='#';System.out.print(d);
    
    char []num=new char[30];double z,t1,t2,temp;i=0;
    while(d[i]!='#'){
    
      if(d[i]>='0'&&d[i]<='9'||d[i]=='.'){
      
                         for(j=0;d[i]>='0'&&d[i]<='9'||d[i]=='.'; i++)num[j++]=d[i];
                       num[j]='\0';  
                      z=c.ctof(num);
   
                      c.push_sz(z);}
     
    if(d[i]==' ')i++;
    if(d[i]=='+'||d[i]=='-'||d[i]=='*'||d[i]=='/')
              {
             t2=c.cz_sz();
             t1=c.cz_sz();
             char m=d[i];
             switch(m)
             {
              case '+':temp=t1+t2;c.push_sz(temp);break;
              case '-':temp=t1-t2;c.push_sz(temp);break;
              case '*':temp=t1*t2;c.push_sz(temp);break;
              case '/':temp=t1/t2;c.push_sz(temp);break;
             }
             
    i++;}
    }
    
   
    
    
    
    
    double t3=c.cz_sz();
   
   //sc.setText();
   tf.setText(f+"="+t3+"");}
   }
  
 }
 
 public static void main(String [] args){
  jiemian a=new jiemian();
  a.disp();
 }
}
class ysf{
 char ysf[];
 int i;
 ysf(){
  ysf=new char[100];
  i=-1;
 }
 char cz_ysf(){
  i--;
  return ysf[i+1];
  
 }
 char top_ysf(){
  
   
                   
  return ysf[i];
  
 }
 int empty_ysf(){
  if(i==-1)
  return 1;
  return 0;
 }
 void push_ysf(char a){i++;
  ysf[i]=a;
  
     
 }
 int bj(char a){
  if(a=='+'||a=='-') return 1;
 else if(a=='#') return -1;
 else if(a=='*'||a=='/') return 2;
 else  return 0;
 
 }
}
class sz{
 double sz[];
 int i;
 sz(){
  sz=new double[100];
  i=-1;
 }
 double cz_sz(){
  i--;
  return sz[i+1];
  
 }
 double top_sz(){
  
  return sz[i];
 }
 int empty_sz(){
  if(i==-1)
  return 1;
  return 0;
 }
 void push_sz(double a){
  i++;
  sz[i]=a;
  
 }
 double ctof(char c[]){
 int i,j,n=0;while(c[n]!='\0')n++; 
 int x=0; 
 double y=0.0;
 for(i=0;i<n;i++){
    if(c[i]=='.')break;
    else x=(x+(c[i]-48))*10;
 }
    x=x/10;
    if(c[i]=='.')
  for(j=n-1;j>i;j--)y=(y+(c[j]-48))*0.1;
 return x+y;
 }
}
 | 
 |