一个简单的计算器

这是在大1第2个学期在学校参加比赛的时候做的,呵呵~~现在去看这些代码,觉得青涩,却又饶有回忆,不过如果当初没有这个小例子,我有可能已经去NET的行列了.呵呵~~不罗嗦那么多了.自己看把.时间有些紧张,没有写解释.不懂可以留言,或者百度一下.

 

 

package CalcApp;
import java.awt.*;
import java.awt.event.*;
public class CalcAppDemo extends Frame
{
private TextField t_result;
private Panel p_main;
private Panel p_num;
private Panel p_oper;
private Panel p_show;
private Button b_num[];
private Button b_oper[];

public CalcAppDemo(String title)
{
  setTitle(title);
  setLocation(500,300);
  t_result=new TextField("0.0",21);
  p_main=new Panel();
                p_num=new Panel();
  p_oper=new Panel();
  p_show=new Panel();
  p_main.setLayout(new BorderLayout());
  p_num.setLayout(new GridLayout(4,3,1,1));
  p_oper.setLayout(new GridLayout(4,2,1,1));
  
  b_num=new Button[12];
  for(int i=0;i<9;i++)
  {
                    b_num[i]=new Button(new Integer(i+1).toString());
  }
  b_num[9]=new Button(" 0");
  b_num[10]=new Button(" cls");
  b_num[11]=new Button(" .");
  for(int i=0;i<12;i++)
  {
                    p_num.add(b_num[i]);
  }
  
  b_oper=new Button[8];
  b_oper[0]=new Button(" +");
  b_oper[1]=new Button(" -");
  b_oper[2]=new Button(" *");
  b_oper[3]=new Button(" /");
  b_oper[4]=new Button(" pow");
  b_oper[5]=new Button(" sqrt");
                b_oper[6]=new Button(" +/-");
  b_oper[7]=new Button(" =");
  for(int i=0;i<8;i++)
  {
                    p_oper.add(b_oper[i]);
  }
  
  t_result.setEditable(false);
  p_show.add(t_result,BorderLayout.NORTH);
  p_main.add(p_show,BorderLayout.NORTH);
  p_main.add(p_num,BorderLayout.WEST);
  p_main.add(p_oper,BorderLayout.EAST);
  this.add(p_main,BorderLayout.CENTER);
  setSize(400,400);
  setResizable(false);
  pack();
  
  this.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });
  ButtonListener bl=new ButtonListener();
  for(int i=0;i<12;i++)
  {
   b_num[i].addActionListener(bl);
  }
  for(int i=0;i<8;i++)
  {
   b_oper[i].addActionListener(bl);
  }
}

class ButtonListener implements ActionListener
{
  private String lastOp;
  private String strVal;
  private double total;
  private double number;
  private boolean firsttime;
  private boolean operatorPressed;
  
  ButtonListener()
  {
   firsttime=true;
   strVal="";
  }
  
  public void actionPerformed(ActionEvent e)
  {
   String s=((Button)e.getSource()).getLabel().trim();
   
   if(Character.isDigit(s.charAt(0)))
   {
    handleNumber(s);
   }
   else
   {
    calculate(s);
   }
  }
  
  void calculate(String op)
  {
   operatorPressed=true;
   
   if(firsttime&&!isUnary(op))
   {
    total=getNumberOnDisplay();
    firsttime=false;
   }
   if(isUnary(op))
   {
    handleUnaryOp(op);
   }
   else if(lastOp!=null)
   {
    handleBinaryOp(lastOp);
   }
   if(!isUnary(op))
   {
    lastOp=op;
   }
  }
  
  boolean isUnary(String s)
  {
   return s.equals("=")||s.equals("cls")||s.equals("sqrt")||s.equals("+/-")||s.equals(".");
  }
  
  void handleUnaryOp(String op)
  {
   if(op.equals("+/-"))
   {
    number=negate(getNumberOnDisplay()+"");
    t_result.setText("");
    t_result.setText(number+"");
    return;
   }
   else if(op.equals("."))
   {
    handleDecPoint();
    return;
   }
   else if(op.equals("sqrt"))
   {
    number=Math.sqrt(getNumberOnDisplay());
    t_result.setText("");
    t_result.setText(number+"");
    return;
   }
   else if(op.equals("="))
   {
    if(lastOp!=null&&!isUnary(lastOp))
    {
     handleBinaryOp(lastOp);
    }
    lastOp=null;
    firsttime=true;
    return;
   }
   else
   {
    clear();
   }
  }
  
  void handleBinaryOp(String op)
  {
   if(op.equals("+"))
   {
    total+=number;
   }
   else if(op.equals("-"))
   {
    total-=number;
   }
   else if(op.equals("*"))
   {
    total*=number;
   }
   else if(op.equals("/"))
   {
    try
    {
     total/=number;
    }
    catch(ArithmeticException ae)
    {
    }
   }
   else if(op.equals("pow"))
   {
    total=Math.pow(total,number);
   }
   t_result.setText("");
   lastOp=null;
   strVal="";
   number=0;
   t_result.setText(total+"");
  }
  
  void handleNumber(String s)
  {
   if(!operatorPressed)
   {
    strVal+=s;
   }
   else
   {
    operatorPressed=false;
    strVal=s;
   }
   number=new Double(strVal).doubleValue();
   t_result.setText("");
   t_result.setText(strVal);
  }
  
  void handleDecPoint()
  {
   operatorPressed=false;
   if(strVal.indexOf(".")<0)
   {
    strVal+=".";
   }
   t_result.setText("");
   t_result.setText(strVal);
  }
  
  double negate(String s)
  {
   operatorPressed=false;
   if(number==(int)number)
   {
    s=s.substring(0,s.indexOf("."));
   }
   if(s.indexOf("-")<0)
   {
    strVal="-"+s;
   }
   else
   {
    strVal=s.substring(1);
   }
   return new Double(strVal).doubleValue();
  }
  
  double getNumberOnDisplay()
  {
   return new Double(t_result.getText()).doubleValue();
  }
  
  void clear()
  {
   firsttime=true;
   lastOp=null;
   strVal="";
   total=0;
   number=0;
   t_result.setText("0");
  }
}

public static void main(String args[])
{
  CalcAppDemo c=new CalcAppDemo("简单计算器");
  c.setVisible(true);
}
}
 

你可能感兴趣的:(.net,百度)