中间有负数运算的时候有些bug, 时间紧迫,算法问题有待后期完善,
package com.lw.calc; import java.math.BigDecimal; import java.math.RoundingMode; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; /*** * 我的计算器 * @author david.li * @version 1.0 * create date : 20140905 */ public class CALCActivity extends Activity { private static final String NUM_0 = "0"; private static final String NUM_MAX = "∞"; private static final String NUM_MIN = "-∞"; private EditText text; private EditText calc_text; private Button btn_no0; private Button btn_no1; private Button btn_no2; private Button btn_no3; private Button btn_no4; private Button btn_no5; private Button btn_no6; private Button btn_no7; private Button btn_no8; private Button btn_no9; private Button btn_add; private Button btn_sub; private Button btn_mul; private Button btn_div; private Button btn_pot; private Button btn_clr; private Button btn_del; private Button btn_equ; private OnClickListener listener_no0; private OnClickListener listener_no1; private OnClickListener listener_no2; private OnClickListener listener_no3; private OnClickListener listener_no4; private OnClickListener listener_no5; private OnClickListener listener_no6; private OnClickListener listener_no7; private OnClickListener listener_no8; private OnClickListener listener_no9; private OnClickListener listener_btn_add; private OnClickListener listener_btn_sub; private OnClickListener listener_btn_mul; private OnClickListener listener_btn_div; private OnClickListener listener_btn_pot; private OnClickListener listener_btn_clr; private OnClickListener listener_btn_del; private OnClickListener listener_btn_equ; public enum CALC_TYPE { ADD("add", 1), SUB("sub", 2), MUL("mul", 3), DIV("div", 4); private String name; private int value; private CALC_TYPE(String name, int value) { this.name = name; this.value = value; } @Override public String toString() { return this.name; } } /*** * 创建打开我的计算器窗体 * 1、获取窗口控件 * 2、实现每一个控件的点击事件监听(处理操作过程) * 3、将事件监听附加到对应的窗体控件上 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("David 计算器"); /*** * 获取每一个窗体控件 */ text = (EditText)findViewById(R.id.text); text.setText(NUM_0); calc_text = (EditText)findViewById(R.id.calc_text); btn_no0 = (Button)findViewById(R.id.btn_no0); btn_no1 = (Button)findViewById(R.id.btn_no1); btn_no2 = (Button)findViewById(R.id.btn_no2); btn_no3 = (Button)findViewById(R.id.btn_no3); btn_no4 = (Button)findViewById(R.id.btn_no4); btn_no5 = (Button)findViewById(R.id.btn_no5); btn_no6 = (Button)findViewById(R.id.btn_no6); btn_no7 = (Button)findViewById(R.id.btn_no7); btn_no8 = (Button)findViewById(R.id.btn_no8); btn_no9 = (Button)findViewById(R.id.btn_no9); btn_add = (Button)findViewById(R.id.btn_add); btn_sub = (Button)findViewById(R.id.btn_sub); btn_mul = (Button)findViewById(R.id.btn_mul); btn_div = (Button)findViewById(R.id.btn_div); btn_pot = (Button)findViewById(R.id.btn_pot); btn_clr = (Button)findViewById(R.id.btn_clr); btn_del = (Button)findViewById(R.id.btn_del); btn_equ = (Button)findViewById(R.id.btn_equ); /*** * 实现每一个控件的点击事件监听(处理操作过程) */ listener_no0 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = NUM_0; } else { num_now += NUM_0; } text.setText(num_now); } }; listener_no1 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "1"; } else { num_now += "1"; } text.setText(num_now); } }; listener_no2 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "2"; } else { num_now += "2"; } text.setText(num_now); } }; listener_no3 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "3"; } else { num_now += "3"; } text.setText(num_now); } }; listener_no4 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "4"; } else { num_now += "4"; } text.setText(num_now); } }; listener_no5 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "5"; } else { num_now += "5"; } text.setText(num_now); } }; listener_no6 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "6"; } else { num_now += "6"; } text.setText(num_now); } }; listener_no7 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "7"; } else { num_now += "7"; } text.setText(num_now); } }; listener_no8 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "8"; } else { num_now += "8"; } text.setText(num_now); } }; listener_no9 = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(reInput(num_now)) { num_now = "9"; } else { num_now += "9"; } text.setText(num_now); } }; listener_btn_pot = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(num_now.indexOf(".") == -1) { num_now += "."; } text.setText(num_now); } }; listener_btn_add = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(num_now.endsWith("+") || num_now.endsWith("-") || num_now.endsWith("x") || num_now.endsWith("÷")) { num_now = num_now.substring(0, num_now.length() - 1); } text.setText(num_now + "+"); } }; listener_btn_sub = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(num_now.endsWith("+") || num_now.endsWith("-") || num_now.endsWith("x") || num_now.endsWith("÷")) { num_now = num_now.substring(0, num_now.length() - 1); } text.setText(num_now + "-"); } }; listener_btn_mul = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(num_now.endsWith("+") || num_now.endsWith("-") || num_now.endsWith("x") || num_now.endsWith("÷")) { num_now = num_now.substring(0, num_now.length() - 1); } text.setText(num_now + "x"); } }; listener_btn_div = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(num_now.endsWith("+") || num_now.endsWith("-") || num_now.endsWith("x") || num_now.endsWith("÷")) { num_now = num_now.substring(0, num_now.length() - 1); } text.setText(num_now + "÷"); } }; listener_btn_clr = new OnClickListener() { public void onClick(View v) { text.setText(NUM_0); calc_text.setText(""); } }; listener_btn_del = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(!NUM_0.equals(num_now)) { if(num_now.length() > 1) { num_now = num_now.substring(0, num_now.length() - 1); } else { num_now = NUM_0; } } text.setText(num_now); if(NUM_0.equals(num_now)) { calc_text.setText(""); } } }; listener_btn_equ = new OnClickListener() { public void onClick(View v) { String num_now = text.getText().toString(); if(num_now.endsWith("+") || num_now.endsWith("-") || num_now.endsWith("x") || num_now.endsWith("÷")) { num_now = num_now.substring(0, num_now.length() - 1); } try { String num = calc(num_now); text.setText(num); calc_text.setText(num_now + "="); } catch (Exception e) { } } }; /*** * 给数字按钮、运算操作添加点击事件监听 */ // 数字按钮监听 btn_no0.setOnClickListener(listener_no0); btn_no1.setOnClickListener(listener_no1); btn_no2.setOnClickListener(listener_no2); btn_no3.setOnClickListener(listener_no3); btn_no4.setOnClickListener(listener_no4); btn_no5.setOnClickListener(listener_no5); btn_no6.setOnClickListener(listener_no6); btn_no7.setOnClickListener(listener_no7); btn_no8.setOnClickListener(listener_no8); btn_no9.setOnClickListener(listener_no9); // 运算操作按钮监听 btn_pot.setOnClickListener(listener_btn_pot); btn_add.setOnClickListener(listener_btn_add); btn_sub.setOnClickListener(listener_btn_sub); btn_mul.setOnClickListener(listener_btn_mul); btn_div.setOnClickListener(listener_btn_div); btn_clr.setOnClickListener(listener_btn_clr); btn_del.setOnClickListener(listener_btn_del); btn_equ.setOnClickListener(listener_btn_equ); } private String calc(String num) { if(num.indexOf(NUM_MAX) > -1) { return NUM_MAX; } if(num.indexOf(NUM_MIN) > -1) { return NUM_MIN; } boolean div_zero = false; // n÷0 boolean zero_div = false; // 0÷n String[] array_add = num.split("\\+"); BigDecimal bd = BigDecimal.ZERO; for(int i=0; i<array_add.length; i++) { if(array_add[i].indexOf("-") > -1 ||array_add[i].indexOf("x") > -1 ||array_add[i].indexOf("÷") > -1) { String[] array_sub = array_add[i].split("\\-"); for(int j=0; j<array_sub.length; j++) { if(array_sub[j].indexOf("x") > -1 ||array_sub[j].indexOf("÷") > -1) { String[] array_div = array_sub[j].split("\\÷"); BigDecimal sum_mul = BigDecimal.ONE; BigDecimal sum_div = BigDecimal.ONE; for(int k=0; k<array_div.length; k++) { if(array_div[k].indexOf("x") > -1) { String[] array_mul = array_div[k].split("x"); for(int h=0; h<array_mul.length; h++) { if(k!=0 && h==0) { if(sum_mul.compareTo(BigDecimal.ZERO) == 0) { zero_div = true; } if("0".equals(array_mul[h])) { div_zero = true; sum_mul = sum_mul.divide(BigDecimal.ONE); // 用于计算结果的正负 } else { sum_mul = sum_mul.divide(new BigDecimal(array_mul[h]), 10, RoundingMode.HALF_UP); } } else { sum_mul = sum_mul.multiply(new BigDecimal(array_mul[h])); } } } else { if(k==0) { sum_div = sum_div.multiply(new BigDecimal(array_div[k])); } else { if(sum_div.compareTo(BigDecimal.ZERO) == 0) { zero_div = true; } if(NUM_0.equals(array_div[k])) { div_zero = true; sum_div = sum_div.divide(BigDecimal.ONE); // 用于计算结果的正负 } else { sum_div = sum_div.divide(new BigDecimal(array_div[k]), 10, RoundingMode.HALF_UP); } } } } if(j==0) { bd = bd.add(sum_mul.multiply(sum_div)); } else { bd = bd.subtract(sum_mul.multiply(sum_div)); } } else { if(j==0) { if("".equals(array_sub[j]) == false) { bd = bd.add(new BigDecimal(array_sub[j])); } } else { bd = bd.subtract(new BigDecimal(array_sub[j])); } } } } else { bd = bd.add(new BigDecimal(array_add[i])); } } String result = bd.toString(); if(result.indexOf(".") > -1) { String[] ns = result.split("\\."); if(new BigDecimal(ns[1]).equals(BigDecimal.ZERO)) { result = ns[0]; } else { String end = ns[1]; int len = end.length(); for(int i=0; i<len; i++) { if(end.endsWith("0")) { end = end.substring(0, end.length() - 1); } } result = ns[0] + "." + end; } } if(div_zero && bd.compareTo(BigDecimal.ZERO) >= 0) { result = NUM_MAX; } else if(div_zero && bd.compareTo(BigDecimal.ZERO) < 0) { result = NUM_MIN; } if(zero_div) { result = NUM_0; } if(zero_div && div_zero) { result = "NAN"; } return result; } private boolean reInput(String num_now) { if(NUM_0.equals(num_now) || NUM_MAX.equals(num_now) || NUM_MIN.equals(num_now)) { return true; } return false; } }
源码查看附件: