用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

简单计算器

2015-05-25 作者: yolen举报

[java]代码库

import java.io.*;
import java.text.Format;
import java.text.NumberFormat;
import java.awt.*;
import java.awt.event.*;
public class Calc {
	public static void main(String[] args){
		CalcFrame CF = new CalcFrame();
		}
} 

class CalcFrame extends Frame{

	public TextField inputArea = new TextField(30);
	Panel p1 = new Panel(),p2 = new Panel();
	boolean dotFg = false;    //记录有没有按下小数点
	boolean opFlag =false;
	double d1,d2;//两操作 数
	int whichSymbol =0;
	int dfg = 1;
	    numButton b1 = new numButton("1")
	    ,b2 = new numButton("2")
	    ,b3 = new numButton("3")
	    ,b4 = new numButton("4")
	 	,b5 = new numButton("5")
		,b6 = new numButton("6")
		,b7 = new numButton("7")
		,b8 = new numButton("8")
		,b9 = new numButton("9")
	    ,b0 = new numButton("0");
		
	    zfButton bzf= new zfButton("-");
	   dotButton dot = new dotButton(".");
	   basCalButton  add = new basCalButton("+")
	   				,sub = new basCalButton("-")
	   				,mul = new basCalButton("*")
	   				,div = new basCalButton("/");
	   funCalButton sqrt = new funCalButton("sqrt")
	                ,sin = new funCalButton("sin")
	   				,cos = new funCalButton("cos")
	   				,power = new funCalButton("power")
	   				,ln = new funCalButton("ln");
	   clearButton1  ce = new clearButton1("CE");
	   clearButton2  back = new clearButton2("<-"); 
	   resultButton equal = new resultButton("=");
	
  public CalcFrame(){
		
		super("Calculator");
		setLayout(new FlowLayout());
		setBackground(Color.lightGray);
		setSize(250,180);
		setVisible(true);
	    inputArea.setBackground(Color.white);
		p1.setBackground(Color.white);
		p1.setSize(100,100);
		p1.setLayout(new GridLayout(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(bzf);
		p1.add(dot);
		p2.setBackground(Color.white);
		p2.setSize(100,100);
		p2.setLayout(new GridLayout(4,3));
		p2.add(div);
		p2.add(sqrt);
		p2.add(ln);
		p2.add(mul);
		p2.add(sin);
		p2.add(back);
		p2.add(sub);
		p2.add(cos);
		p2.add(ce);
		p2.add(add);
		p2.add(power);
		p2.add(equal);
		
		add("North",inputArea);
		add(p1);
		add(p2);
		
		addWindowListener(new WindowAdapter(){
	        public void windowClosing(WindowEvent e){
	    		System.exit(0);}
	        });
	}
	
	  
  
  class numButton extends Button implements ActionListener{   //数字键类 
		  	public String s1;
		 	public numButton(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
			        if( inputArea.getText().equals("0.0")||opFlag )
			        {
			        	inputArea.setText("");
			        	opFlag = false;
			        }
			    	String temp = inputArea.getText();
			    	inputArea.setText("");
			    	inputArea.setText(temp+s1);
			       
			   
			}
		
	     }
 class zfButton extends Button implements ActionListener{  //正负键 
		  	public String s1;
		  	static final int flag = 0;
		 	public zfButton(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
			    
			   String temp = inputArea.getText();
			   if(!dotFg)
			   inputArea.setText("");
			   inputArea.setText("-"+temp);
			  
				
			 }
		
	     }
	   class dotButton extends Button implements ActionListener{
		   public String s1;
		   public dotButton(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
				dotFg = true;
				String temp = inputArea.getText();
				if(temp.equals(""))
				 inputArea.setText("0.");
				else{
				inputArea.setText(temp+".");
				
			 }
				
			 }
	   }
	   class basCalButton extends Button implements ActionListener{
		   public String s1;
		   public basCalButton(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
			     opFlag =true;
			     d1 = Double.parseDouble(inputArea.getText());
				inputArea.setText("");
				if(s1.equals("+"))
					whichSymbol = 1;
				else if(s1.equals("-"))
					whichSymbol  = 2;
				else if(s1.equals("*"))
					 whichSymbol = 3;
				else if(s1.equals("/"))
					whichSymbol = 4	;		
					}
	   }
	 class funCalButton extends Button implements ActionListener{
		   public String s1;
		   public funCalButton(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
			   double temp = Double.parseDouble(inputArea.getText());
			   if(s1.equals("sqrt"))
			     temp = Math.sqrt(temp*3.14159/180);
			   else if(s1.equals("sin"))
                 temp = Math.sin(temp*3.14159/180);
			   else if(s1.equals("cos"))
			     temp = Math.cos(temp*3.14159/180);
			   else if(s1.equals("power"))
			    { opFlag =true;
				whichSymbol = 5;
				d1 = temp;
				inputArea.setText("");
			   }
			   else if(s1.equals("ln"))
			     temp = Math.log(temp);
				 inputArea.setText(String.valueOf(temp));
			 }
	   }
	   class clearButton1 extends Button implements ActionListener{
		   public String s1;
		   public clearButton1(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
			 inputArea.setText("");
			 d1 =0;
			 d2 =0;
			 dotFg = false;
			 whichSymbol = 0;
			 opFlag =false;
			 }
	   }
	   class clearButton2 extends Button implements ActionListener{
		   public String s1;
		   public clearButton2(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
			  String temp,s1;
			  temp = inputArea.getText();
			  s1 = temp.substring(0, temp.length()-1);
			  inputArea.setText(s1);
			 }
	   }
	  class resultButton extends Button implements ActionListener{
		   public String s1;
		   public resultButton(String s){
		 		super(s);
		 		s1 =s;
		 		setBackground(Color.cyan);
		 		addActionListener(this);
			}
			 
			public void actionPerformed(ActionEvent e) {  //重写响应单击操作
				double result = 0;
				d2 = Double.parseDouble(inputArea.getText());
				switch(whichSymbol){
				case 1:
				    result = d1 + d2;
				    break;
				case 2:
					result = d1 - d2;
					break;
				case 3:
					result = d1 * d2;
					break;
				case 4:
					result = d1 / d2;
					break;
			    case 5:
					result = Math.pow(d1,d2);
				
				}

				NumberFormat form = NumberFormat.getIntegerInstance();
				form.setMinimumFractionDigits(8);
				String is = form.format(result);
				inputArea.setText(is);
			    	d1 = 0;
					d2 = 0;
					dotFg = false;
					whichSymbol = 0;
					opFlag =false;
			 }
	   }
	   
	  
}




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...