用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

计算器!!!

2017-04-29 作者: sue举报

[java]代码库

//创建页面Calframe.class
package calframe;


import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
     /**
      * design for the face of cal
      */
	private final static int PRE_WIDTH=500;
	private final static int PRE_HEIGHT=400;
	private JTextField text=null;
	private JButton button=null;   //storage mark
	private String[] nOp={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
    private String[] mOp={"MC","MR","MS","M+"};
    private String[] rOp={"Back","CE","C"};
    private CalService service= new CalService();
    
    public CalFrame(){
    	this.setTitle("Calculation");
    	this.setSize(PRE_WIDTH,PRE_HEIGHT);
    	this.setLocationRelativeTo(null);
    	this.setResizable(false);
    	// add bottom
    	JPanel panel=new JPanel();
    	panel.setLayout(new BorderLayout(10,1));
    	panel.add(getTextField(), BorderLayout.NORTH);
    	panel.setPreferredSize(new Dimension(PRE_WIDTH,PRE_HEIGHT));
    	
    	//WEST
    	JButton[] mBtn=getMButton();
    	JPanel panel1=new JPanel();
    	panel1.setLayout(new GridLayout(5,1,0,5));
    	for(JButton b:mBtn){
    		panel1.add(b);
    	}
    	panel.add(panel1, BorderLayout.WEST);
    	//
    	JButton[] rBtn=getRButton();
    	JPanel panel2=new JPanel();
    	panel2.setLayout(new BorderLayout(1,5));
    	JPanel panel21=new JPanel();
    	panel21.setLayout(new GridLayout(1,3,3,3));
    	for(JButton b:rBtn){
    		panel21.add(b);
    	}
    	panel2.add(panel21, BorderLayout.NORTH);
    	JButton[] nBtn=getNButton();
    	JPanel panel22=new JPanel();
    	panel22.setLayout(new GridLayout(4,5,3,5));
    	for(JButton b:nBtn){
    		panel22.add(b);
    	}
    	panel2.add(panel22,BorderLayout.CENTER);
    	panel.add(panel2, BorderLayout.CENTER);
    	this.add(panel);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	this.setVisible(true);
    }

	private JButton[] getRButton() {
		// TODO Auto-generated method stub
		JButton[] rBtn=new JButton[rOp.length];
		for(int i=0;i<this.rOp.length;i++){
			JButton b=new JButton(this.rOp[i]);
			b.addActionListener(getActionListener());
			b.setForeground(Color.red);
			rBtn[i]=b;
		}
		return rBtn;
	}
    //return data Btn
	private JButton[] getNButton() {
		// TODO Auto-generated method stub
		String[] redBtn={"/","*","-","+","="};
		JButton[] nbutton=new JButton[nOp.length];
		for(int i=0;i<this.nOp.length;i++){
			JButton b = new JButton(this.nOp[i]);
			b.addActionListener(getActionListener());
			Arrays.sort(redBtn);
			if(Arrays.binarySearch(redBtn, nOp[i])>=0){
				b.setForeground(Color.red);
			}else{
				b.setForeground(Color.blue);
			}
			nbutton[i]=b;
		}
		return nbutton;
	}

	private ActionListener getActionListener() {
		// TODO Auto-generated method stub
		ActionListener actionListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
			       String cmd=e.getActionCommand();
			       String result=null;
			       try{
			    	   result=service.callMethod(cmd,text.getText());
			       }catch(Exception e2){
			    	   System.out.println(e2.getMessage());
			       }
			       if(cmd.indexOf("MC")==0){
			    	   button.setText("");
			       }else if(cmd.indexOf("M")==0){
			    	   button.setText("M");
			       }
			       //show the calculate result
			       if(result!=null){
			    	   text.setText(result);
			       }
			}
		};
		return actionListener;
	}

	private JButton[] getMButton() {
		// TODO Auto-generated method stub
        JButton[] mBtn=new JButton[mOp.length+1];
        mBtn[0]=getButton();
        for(int i=0;i<this.mOp.length;i++){
        	JButton b=new JButton(this.mOp[i]);
        	b.addActionListener(getActionListener());
        	b.setForeground(Color.red);
        	mBtn[i+1] = b;
        }
		return mBtn;
	}
	
    private JButton getButton() {
		// TODO Auto-generated method stub
    	button=new JButton();
		return button;
	}

	//return show 
	private JTextField getTextField() {
		// TODO Auto-generated method stub
		text = new JTextField("0",10);
		return text;
	}
	public static void main(String[] args){
		new CalFrame();
	}
}

public class CalService {
    
	private boolean isSecondNum=false;
	private String lastOp;
	private String firstNum="0";
	private String secondNum="null";
	private double store;
	private String numString = "0123456789.";
	private String opString="+-*/";
	
	public String catNum(String cmd,String text){
		
		String result = cmd;
		if(!"0".equals(text)){
			if(isSecondNum){
				isSecondNum=false;
			}else{
				result = text + cmd;
			}
		}
		if(result.indexOf(".")==0){
			result = "0" + result;
		}
		
		return result;
	}
	public String setOp(String cmd,String text){
		this.lastOp = cmd;
		this.firstNum=text;
		this.secondNum=null;
		this.isSecondNum=true;
		return null;
	}
	public String cal(String text,boolean isPercent){
		double secondResult = secondNum==null?Double.valueOf(text).doubleValue():Double.valueOf(secondNum).doubleValue();
		// chushu is zero
		if(secondResult == 0 && this.lastOp.equals("/")){
			return "0";
		}
		//hava "%"
		if(isPercent){
			secondResult = MyMath.multiply(Double.valueOf(firstNum),(double) MyMath.divide(secondResult,100.000));
		}
		if(this.lastOp.equals("+")){
			firstNum=String.valueOf(MyMath.add(Double.valueOf(firstNum),secondResult));
		}else if(this.lastOp.equals("-")){
			firstNum=String.valueOf(MyMath.subtract(Double.valueOf(firstNum),secondResult));
		}else if(this.lastOp.equals("*")){
			firstNum=String.valueOf(MyMath.multiply(Double.valueOf(firstNum),secondResult));
		}else if(this.lastOp.equals("/")){
			firstNum=String.valueOf(MyMath.divide(Double.valueOf(firstNum),secondResult));
		}
		secondNum=secondNum==null?text:secondNum;
		this.isSecondNum=true;
		return firstNum;
	}
	
	// sqrt
	public String sqrt(String text){
		this.isSecondNum = true;
		return String.valueOf(Math.sqrt(Double.valueOf(text)));
	}
	// qiu daoshu
	public String setReciprocal(String text){
		if(text.equals("0")){
			return text;
		}else {
			this.isSecondNum = true;
			return String.valueOf(MyMath.divide(1, Double.valueOf(text)));
		}
	}
	
	//storage
	public String mCmd(String cmd,String text){
		if(cmd.equals("M+")){
			store=MyMath.add(store, Double.valueOf(text));
		}else if(cmd.equals("MC")){
			store=0;
		}else if(cmd.equals("MR")){
			isSecondNum=true;
			return String.valueOf(store);
		}else if(cmd.equals("MS")){
			store = Double.valueOf(text).doubleValue();
		}
		return null;
	}
	
	
	
	public String setNegative(String text){
		if(text.indexOf("-")==0){
			return text.substring(1, text.length());
		}else {
			return "-" + text;
		}
	}
	public String clear(String text){
		return "0";
	}
	public String clearAll(){
		this.firstNum="0";
		this.secondNum=null;
		return this.firstNum;
	}
	public String backSpace(String text){
		return text.equals("0") || text.equals("")?"0":text.substring(0,text.length()-1);
	}
	public String callMethod(String cmd, String text) {
		// TODO Auto-generated method stub
		if(cmd.equals("C")){
			return clearAll();
		}else if(cmd.equals("CE")){
			return clear(text);
		}else if(cmd.equals("Back")){
			return backSpace(text);
		}else if(numString.indexOf(cmd) != -1){
			return catNum(cmd,text);
		}else if(opString.indexOf(cmd) != -1){
			return setOp(cmd,text);
		}else if(cmd.equals("+/-")){
			return setNegative(text);
		}else if(cmd.equals("1/x")){
			return setReciprocal(text);
		}else if(cmd.equals("sqrt")){
			return sqrt(text);
		}else if(cmd.equals("%")){
			return cal(text,true);
		}else if(cmd.equals("=")){
			return cal(text,false);
		}else{
			return mCmd(cmd,text);
		}
	}

}








//  计算


import java.math.BigDecimal;

public class MyMath {
	/**
	 * Double for BigDecimal
	 * @param secondResult
	 * @param double1
	 * @return
	 */
	private static BigDecimal getBigDecimal(final double number){
		return new BigDecimal(number);
	}

	public static Object divide(double num1, double num2) {
		// TODO Auto-generated method stub
		BigDecimal first=getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.divide(second, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	public static double multiply(double num1, double num2) {
		// TODO Auto-generated method stub
		BigDecimal first=getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.multiply(second).doubleValue();
	}

	public static double add(double num1, double num2) {
		// TODO Auto-generated method stub
		BigDecimal first=getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.add(second).doubleValue();
	}

	public static double subtract(double num1, double num2) {
		// TODO Auto-generated method stub
		BigDecimal first=getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.subtract(second).doubleValue();
	}


}




























网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...