简单的android计算器

1.网上android计算器很多,这个虽然简单,但毕竟也是我自己手写的,可以进行连续运算,

也能经受退格清除和不合理输入的检验,基本符合现实计算器的使用效果

页面布局使用了TableLayout,操作符绑定btnClick事件,数字和点绑定numClick事件

具体代码略自行补充,布局效果如下:

2.存储字符对象类CharElement

public class CharElement {
	
	private String letter;
	private String type;
	private int level;
	
	public CharElement() {
		super();
	}

	public CharElement(String letter, String type, int level) {
		super();
		this.letter = letter;
		this.type = type;
		this.level = level;
	}
	
	public String getLetter() {
		return letter;
	}
	public void setLetter(String letter) {
		this.letter = letter;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
}
3.主程序逻辑

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	EditText edt = null;
	String showContent = "";
	CharElement ele = new CharElement("","",1);
	List<String> list = new ArrayList<String>();
	boolean dotMark = true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		list.add("+");
		list.add("-");
		list.add("x");
		list.add("/");
		edt = (EditText) findViewById(R.id.resultWin);
	}
	
	//计算公式
	private String calTwoNum(String no1, String cal, String no2) {
		float result = 0;
		
		if(no1.contains("+")||no1.contains("-")||no1.contains("x")||no1.contains("/")) {
			no1 = splitNum(no1);
		}
		if(no2.contains("+")||no2.contains("-")||no2.contains("x")||no2.contains("/")) {
			no2 = splitNum(no2);
		}
		
		//java中普通浮点计算精度不准,用bigdecimal包装
		//float n1 = Float.parseFloat(no1);
		//float n2 = Float.parseFloat(no2);
		BigDecimal n1 = new BigDecimal(no1);
		BigDecimal n2 = new BigDecimal(no2);
		if(cal.equals("x")) {
			//result = n1*n2;
			result = n1.multiply(n2).floatValue();
		}else if(cal.equals("/")) {
			//result = n1/n2;
			result = n1.divide(n2, 6, BigDecimal.ROUND_HALF_EVEN).floatValue();
		}else if(cal.equals("+")) {
			//result = n1+n2;
			result = n1.add(n2).floatValue();
		}else if(cal.equals("-")) {
			//result = n1-n2;
			result = n1.subtract(n2).floatValue();
		}
		//3000000.0f-85.1f这个额是个例外...15.0不是...14.9
		return result+"";
	}
	
	//拆分公式
	private String splitNum(String text) {
		String result = "";
		String[] txs1,txs2,txs3,txs4;
		
		txs1 = text.split("\\+");
		txs2 = text.split("-");
		txs3 = text.split("x");
		txs4 = text.split("/");
		
		int i = 1;
		if(txs1.length>1){
			result = txs1[0];
			for(; i < txs1.length; i++){
				result = calTwoNum(result, "+", txs1[i]);
			}
		}else if(txs2.length>1){
			if(txs2[0].equals("")) txs2[0] = "0";
			result = txs2[0];
			for(; i < txs2.length; i++){
				result = calTwoNum(result, "-", txs2[i]);
			}
		}else if(txs3.length>1){
			result = txs3[0];
			for(; i < txs3.length; i++){
				result = calTwoNum(result, "x", txs3[i]);
			}
		}else if(txs4.length>1){
			result = txs4[0];
			for(; i < txs4.length; i++){
				result = calTwoNum(result, "/", txs4[i]);
			}
		}else{
			result = text;
		}
		
		return result;
	}
	//获取操作符
	public void btnClick(View v) {
		//c  ←  =    x  /  +  -
		Button btn = (Button) v;
		String letter = btn.getText().toString();
		
		if(letter.equals("c")) {
			showContent = "";
			ele = new CharElement("","",1);
			dotMark = true;
		} else if(letter.equals("←")) {
			if(showContent.length()>0) {
				if(showContent.substring(showContent.length()-1, showContent.length()).equals(".")) dotMark = true;
				showContent = showContent.substring(0, showContent.length()-1);
				String e = "";
				String type = "num";
				if(showContent.length()>0) {
					e = showContent.substring(showContent.length()-1, showContent.length());
					if(list.contains(e)) {
						type = "cal";
					}
				}
				setEle(e,type);
			}
		} else if(letter.equals("=")) {
			showContent = splitNum(showContent);
			setEle(showContent,"num");
			if(showContent.contains(".")) dotMark = false;
			else dotMark = true;
		} else {
			
			if(ele.getLetter().equals(".")){
				showContent += "0";
			}
			
			if(ele.getType().equals("cal")){
				showContent = showContent.substring(0, showContent.length()-1);
				if(showContent.length()==0) ele = new CharElement("","",1);
			}
			
			if(showContent.equals("")&&!letter.equals("-"));
			else {
				dotMark = true;
				showContent += letter;
				setEle(letter,"cal");
			}
		}
		
		edt.setText(showContent);
	}
	//获取数字
	public void numClick(View v) {
		//.  num:0-9
		Button btn = (Button) v;
		String letter = btn.getText().toString();
		
		
		if(letter.equals(".")) {
			if(ele.getLetter().equals(letter));
			else if(dotMark == true){ 
				if(!ele.getType().equals("num")){
					showContent += "0"+letter;
					dotMark = false;
				} else {
					showContent += letter;
					dotMark = false;
				}
			}else return;
		} else {
			showContent += letter;
		}
		
		setEle(letter,"num");
		
		edt.setText(showContent);

	}
	
	private void setEle(String letter, String type) {
		ele.setLetter(letter);
		ele.setType(type);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值