work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Java 计算器实现

Posted on 2012-12-14 16:33  work hard work smart  阅读(469)  评论(0编辑  收藏  举报

1、加减乘除操作

public class MyMath {
	public static final int DEFAULT_SCALE = 20;
	
	public static double add(double num1, double num2) {
		
		BigDecimal first = getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.add(second).doubleValue();
	}
	
	public static double substract(double num1, double num2) {
		
		BigDecimal first = getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.subtract(second).doubleValue();
	}
	
	public static double multiply(double num1, double num2) {
		
		BigDecimal first = getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.multiply(second).doubleValue();
	}
	
	public static double divide(double num1, double num2) {
		
		BigDecimal first = getBigDecimal(num1);
		BigDecimal second = getBigDecimal(num2);
		return first.divide(second,DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	private static BigDecimal getBigDecimal(double num)
	{
		return new BigDecimal(num);
	}

}

 

2、UI

public class CalFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TextField textField;
	private final int Frame_Width = 360;
	private final int Frame_Height = 250;
	private CalService service = new CalService();
	
	private String[] mOpStrings = {"MC","MR","MS","M+"};
	private String[] nOp = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*",
			"%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
	private String[] rOp = { "Back", "CE", "C" };
	
	public CalFrame(){
		super();
		initialize();
	}

	private  void initialize() {
		this.setTitle("我的计算器");
		this.setResizable(false);
		JPanel panel = new JPanel();
		
	    textField = new TextField("0");
		textField.setBackground(Color.white);
		textField.setEditable(false);
		
		panel.setLayout(new BorderLayout(10, 1));
		panel.add(textField, BorderLayout.NORTH);
		
		panel.setPreferredSize(new DimensionUIResource(Frame_Width, Frame_Height));
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayout(5, 1, 0, 5));
		JButton[] mButton = new JButton[mOpStrings.length];
		for (int i = 0; i < this.mOpStrings.length; i++) {
			JButton b = new JButton(this.mOpStrings[i]);
			b.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					String cmdString = arg0.getActionCommand();
					String result = null;
					
					try {
						  result = service.callMethod(cmdString, textField.getText());
					} catch (Exception e) {
						// TODO: handle exception
						System.out.println(e.getMessage());
					}
					
					if (cmdString.indexOf("MC") == 0) {
						
					}
					
					if (result != null) {
						textField.setText(result);
					}
					
				}
			});
			b.setBackground(Color.red);
			mButton[i] = b;
		}	
	

		for (JButton b : mButton) {
			panel1.add(b);
		}
		
		/////////////////////////////////////////////////////////
		Panel panel2 = new Panel();
		panel2.setLayout(new BorderLayout(1,5));
		
		Panel panel21 = new Panel();	
		JButton[] result = new JButton[rOp.length];
		for (int i = 0; i < this.rOp.length; i++) {
			JButton b = new JButton(this.rOp[i]);
			b.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			b.setBackground(Color.red);
			result[i] = b;
		}
		
		panel21.setLayout(new GridLayout(1,3,3,3));
		for (JButton button : result) {
			panel21.add(button);
		}
		
		Panel panel22 = new Panel();
		panel22.setLayout(new GridLayout(4,5,3,5));
		JButton[] result2 = new JButton[nOp.length];
		String[] redButton = { "/", "*", "-", "+", "=" };
		for (int i = 0; i < this.nOp.length; i++) {
			JButton b2 = new JButton(this.nOp[i]);
			b2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
			Arrays.sort(redButton);
			if (Arrays.binarySearch(redButton, nOp[i]) >= 0) {
				b2.setForeground(Color.red);
			}
			else {
				b2.setForeground(Color.blue);
			}
			result2[i] = b2;
		}
		for (JButton button : result2) {
			panel22.add(button);
		}
		
		
		
		panel2.add(panel21, BorderLayout.NORTH);
		panel2.add(panel22,BorderLayout.CENTER);
		
		panel.add(panel1, BorderLayout.WEST);
		panel.add(panel2, BorderLayout.CENTER);

		this.add(panel);
	}
}

  

3、业务类

public class CalService {
	private String firstNum = null;
	private String secondNum = null;
	private String numString = "0123456789.";
	private String opString = "+-*/";
	
	public String callMethod(String cmd, String text)throws Exception  {
		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 cal(text, false);
		} 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 {
			return mCmd(cmd, text);
		}
	}

	private String backSpace(String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String catNum(String cmd, String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String setOp(String cmd, String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String setNegative(String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String setReciprocal(String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String sqrt(String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String cal(String text, boolean b) {
		// TODO Auto-generated method stub
		return null;
	}

	private String mCmd(String cmd, String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String clear(String text) {
		// TODO Auto-generated method stub
		return null;
	}

	private String clearAll() {
		// TODO Auto-generated method stub
		this.firstNum = "0";
		this.secondNum = null;
		return this.firstNum;
	}
}

  

4、Main类

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		CalFrame frame = new CalFrame();
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	}