计算器代码(Java)

package jyh;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class helloa extends Applet implements ActionListener
{
 String x="",y="",symbol="";
 double answer;
 JTextField tfAnswer; 
 Button button[]=new Button[10];
 Button Jia,Jian,Cheng,Chu,ReStart,Pow,Sqrt,Deng,Fu,Point,sin,cos,tan,ln;
 public void init()
 {
  this.setBackground(Color.CYAN);  
  setLayout(null);
  tfAnswer=new JTextField();
  tfAnswer.setBounds(5, 5, 265, 30);
  tfAnswer.setHorizontalAlignment(JTextField.RIGHT); 
  tfAnswer.setText("0.");
  add(tfAnswer);
  SetButton(button[9],"9",95,50,40,30,Color.blue);
  SetButton(button[8],"8",50,50,40,30,Color.blue);
  SetButton(button[7],"7",5,50,40,30,Color.blue);
  SetButton(button[6],"6",95,85,40,30,Color.blue);
  SetButton(button[5],"5",50,85,40,30,Color.blue);
  SetButton(button[4],"4",5,85,40,30,Color.blue);
  SetButton(button[3],"3",95,120,40,30,Color.blue);
  SetButton(button[2],"2",50,120,40,30,Color.blue);
  SetButton(button[1],"1",5,120,40,30,Color.blue);
  SetButton(button[0],"0",5,155,40,30,Color.blue);
  SetButton(Jia,"+",140,155,40,30,Color.red);
  SetButton(Jian,"-",140,120,40,30,Color.red);
  SetButton(Cheng,"*",140,85,40,30,Color.red);
  SetButton(Chu,"/",140,50,40,30,Color.red);
  SetButton(ReStart,"ReStart",185,50,40,30,Color.red);
  SetButton(Pow,"^",185,85,40,30,Color.blue);
  SetButton(Sqrt,"sqrt",185,120,40,30,Color.blue);
  SetButton(Deng,"=",185,155,40,30,Color.red);
  SetButton(Fu,"+/-",50,155,40,30,Color.blue);
  SetButton(Point,".",95,155,40,30,Color.blue);
  SetButton(sin, "sin", 230, 50, 40, 30, Color.red);
  SetButton(cos, "cos", 230, 85, 40, 30, Color.red);
  SetButton(tan, "tan", 230, 120, 40, 30, Color.red);
  SetButton(ln, "ln", 230, 155, 40, 30, Color.red);
 }



 public void SetButton(Button name,String symbol,int x,int y,int width,int height,Color color)
 {
  setLayout(null);
  name=new Button(symbol);
  name.setBounds(x,y,width,height);
  name.setBackground(Color.white);
  name.setForeground(color);
  name.addActionListener(this);
  add(name);
 }


 public void Equal(String z)
 {
  if(z.equals("+")) 
      answer=Double.parseDouble(x)+Double.parseDouble(y);
  if(z.equals("-")) 
      answer=Double.parseDouble(x)-Double.parseDouble(y);
  if(z.equals("*")) 
      answer=Double.parseDouble(x)*Double.parseDouble(y);
  if(z.equals("/")) 
      answer=Double.parseDouble(x)/Double.parseDouble(y);
  if(z.equals("^"))
      answer=Math.pow(Double.parseDouble(x),Double.parseDouble(y));
  if(z.equals("sin"))
      answer=Math.sin(Double.parseDouble(x));
  if(z.equals("cos"))
      answer=Math.cos(Double.parseDouble(x));
  if(z.equals("tan"))
      answer=Math.tan(Double.parseDouble(x));
  if(z.equals("ln"))
      answer=Math.log(Double.parseDouble(x));
  x=Double.toString(answer);
  tfAnswer.setText(x);
  y="";symbol="";
 }

 public void actionPerformed(ActionEvent e) throws IndexOutOfBoundsException
 {
  if (
    e.getActionCommand().equals("0")||
    e.getActionCommand().equals("1")||
    e.getActionCommand().equals("2")||
    e.getActionCommand().equals("3")||
    e.getActionCommand().equals("4")||
    e.getActionCommand().equals("5")||
    e.getActionCommand().equals("6")||
    e.getActionCommand().equals("7")||
    e.getActionCommand().equals("8")||
    e.getActionCommand().equals("9")
    )
    {
         if (symbol.equals(""))
         {
             x=x+e.getActionCommand();
                 if (x.startsWith("00"))
                 {
                  x=x.substring(1);
                 }
             tfAnswer.setText(x);
         }
         else
         {
             y=y+e.getActionCommand();
                 if (y.startsWith("00"))
                 {
                  y=y.substring(1);
                 }
            tfAnswer.setText(y);
         }
    }

  if (e.getActionCommand().equals("."))
  {
   if (symbol.equals(""))
   {
    int i=0,j=0;
        for (i=0;i<x.length();i++)
        {
             if (x.charAt(i)=='.')
             {
              j++;
             }
        }
        if (j==0)
        {
         x=x+".";
        }
    tfAnswer.setText(x);
   }
   else
   {
       int i=0,j=0;
            for (i=0;i<y.length();i++)
            {
                 if (y.charAt(i)=='.')
                 {
                  j++;
                 }
            }
             if (j==0)
             {
              y=y+".";
             }
    tfAnswer.setText(y);
   }
  }

  if (e.getActionCommand().equals("ReStart"))
  {
       x="";y="";symbol="";
       tfAnswer.setText("0.");
  }  
  if (e.getActionCommand().equals("+/-"))
  {
       if (symbol.equals(""))
       {
            if(x.substring(0,1).equals("-"))
            {
             x=x.substring(1);
            }
            else
            {
             x="-"+x;
            }
            tfAnswer.setText(x);
       }
   else
   {
        if(y.substring(0,1).equals("-"))
        {
            y=y.substring(1);
        }
        else
        {
            y="-"+y;
        }

        tfAnswer.setText(y);
   }
  }

  if(e.getActionCommand().equals("sqrt"))
  {
       if(symbol!="") 
       {
           Equal(symbol);
       }
       answer=Math.sqrt(Double.parseDouble(x));
       x=Double.toString(answer);
       tfAnswer.setText(x);
  }  
  if(e.getActionCommand().equals("+"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="+";
  }
  if(e.getActionCommand().equals("-"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="-";
  }
  if(e.getActionCommand().equals("*"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="*";
  }
  if(e.getActionCommand().equals("/"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="/";
  }
  if(e.getActionCommand().equals("^"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="^";
  }
  if(e.getActionCommand().equals("sin"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.sin(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("ln"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.log(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("cos"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.cos(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("tan"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.tan(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("=")) 
      Equal(symbol);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值