突然翻到大一的写的连续运算的计算器。分享一下
public class MainActivity extends Activity {
private EditText et_getText;
private String str = "";
boolean symbol = false;
private int length = 0;
boolean updatasymbol = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void bt_onclick(View view){
out:for(int i = 0;i<10;i++){
int identifier = getResources().getIdentifier("btn_num"+i, "id", getPackageName());
if(view.getId()==identifier){
str = str+i+"";
et_getText.setText(str);
symbol = true;
updatasymbol = false;
break out;
}
}
if(symbol){
if(updatasymbol){
str = str.substring(0, length);
}else{
length = str.length();
}
if(view.getId()==R.id.btn_add){
str = str+"+";
et_getText.setText(str);
updatasymbol = true;
}
if(view.getId()==R.id.btn_subtraction){
str = str+"-";
et_getText.setText(str);
updatasymbol = true;
}
if(view.getId()==R.id.btn_multiplication){
str = str+"x";
et_getText.setText(str);
updatasymbol = true;
}
if(view.getId()==R.id.btn_division){
str = str+"/";
et_getText.setText(str);
updatasymbol = true;
}
}
if(view.getId()==R.id.btn_point){
str = str+".";
et_getText.setText(str);
}
if(view.getId()==R.id.btn_amount){
if(TextUtils.isEmpty(str)){
return;
}
ArrayList<String> neirong = neirong(str);
et_getText.setText(dy(neirong));
}
if(view.getId()==R.id.btn_clear){
str="";
et_getText.setText(str);
}
}
private void init() {
et_getText = (EditText) findViewById(R.id.et_getText);
}
public ArrayList<String> neirong(String str){
ArrayList <String>list = new ArrayList<String>();
int sp = 0;
for(int i = 1 ;i<str.length();i++){
char c = str.charAt(i);
if(c=='+'||'-'==c||'x'==c||c=='/'){
list.add(str.substring(sp, i));
list.add(""+c);
sp=i+1;
i=i+1;
}
}
list.add(str.substring(sp, str.length()));
return list;
}
public String dy(ArrayList<String> list){
ArrayList<String>list1 = new ArrayList<String>();
System.out.println(list.size());
int sp = 0;
Double sum =null;
for(int i =1;i<list.size();i+=2){
if(("x".equals(list.get(i)))){
if(sum==null){
sum=Double.parseDouble(list.get(i-1))* Double.parseDouble(list.get(i+1));
}else{
sum = sum * Double.parseDouble(list.get(i+1));
}
if (i==list.size()-2){
list1.add(sum+"");
// System.out.println("进入");
}
}else if(("/".equals(list.get(i)))){
System.out.println("/...");
if(Double.parseDouble(list.get(i+1))!=0){
if(sum==null){
//System.out.println(Double.parseDouble(s));
System.out.println("进入");
sum= Double.parseDouble(list.get(i-1))/ Double.parseDouble(list.get(i+1));
}else{
sum = sum / Double.parseDouble(list.get(i+1));
}
if (i==list.size()-2){
list1.add(sum+"");
}
}else{
Toast.makeText(getApplicationContext(), "除数不能为零", 1).show();
et_getText.setText("0");
return "0";
}
}else if(("+".equals(list.get(i)))||("-".equals(list.get(i)))){
if(sum!=null){
list1.add(sum+"");
sum = null;
}else{
list1.add(list.get(i-1));
}
list1.add(list.get(i));
if (i==list.size()-2){
list1.add(list.get(i+1));
}
}
}
BigDecimal big = new BigDecimal(list1.get(0));
for(int i = 0 ;i<list1.size();i++){
if("+".equals(list1.get(i))){
big=big.add(new BigDecimal(list1.get(i+1)));
}else if ("-".equals(list1.get(i))){
big = big.subtract(new BigDecimal(list1.get(i+1)));
}
}
return big.toString();
//return null;
}
}