加法不能运算,一点等号就无响应,其他都没问题
private void getResult() {
String exp = edt.getText().toString();
if (exp == null || exp.equals("")) {
return;
}
if (!exp.contains(" ")) {
return;
}
if (clear_flag) {
clear_flag = false;
return;
}
clear_flag = true;
double result = 0;
while (exp.contains("×") || exp.contains("÷")) {
int a;
if (exp.contains("×") && exp.contains("÷")) {
a = Math.min(exp.indexOf("×"), exp.indexOf("÷"));
} else if (!exp.contains("×")) {
a = exp.indexOf("÷");
} else {
a = exp.indexOf("×");
}
String exp1 = exp.substring(0, a - 1);
String exp2 = exp.substring(a + 2);
String op = exp.substring(a, a + 1);
String s1;
String s2;
if (exp1.contains(" ")) {
s1 = exp1.substring(exp1.lastIndexOf(" ") + 1);
} else {
s1 = exp1;
}
if (exp2.contains(" ")) {
s2 = exp2.substring(0, exp2.indexOf(" "));
} else {
s2 = exp2;
}
String s3 = s1 + " " + op + " " + s2;
String s4;
if (!s1.equals("") && !s2.equals("")) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (op.equals("×")) {
result = d1 * d2;
s4 = result + "";
exp = exp.replaceFirst(s3, s4);
} else if (op.equals("÷")) {
if (d2 == 0) {
edt.setText("error");
} else {
result = d1 / d2;
s4 = result + "";
exp = exp.replaceFirst(s3, s4);
}
}
}
}
while (exp.contains("+") || exp.contains("-")) {
int a;
if (exp.contains("+") && exp.contains("-")) {
a = Math.min(exp.indexOf("+"), exp.indexOf("-"));
} else if (!exp.contains("+")) {
a = exp.indexOf("-");
} else {
a = exp.indexOf("+");
}
String exp1 = exp.substring(0, a - 1);
String exp2 = exp.substring(a + 2);
String op = exp.substring(a, a + 1);
String s1;
String s2;
if (exp1.contains(" ")) {
s1 = exp1.substring(exp1.lastIndexOf(" ") + 1);
} else {
s1 = exp1;
}
if (exp2.contains(" ")) {
s2 = exp2.substring(0, exp2.indexOf(" "));
} else {
s2 = exp2;
}
String s3 = s1 + " " + op + " " + s2;
String s4;
if (!s1.equals("") && !s2.equals("")) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
result = d1 + d2;
s4 = result + "";
exp = exp.replaceFirst(s3, s4);
} else if (op.equals("-")) {
result = d1 - d2;
s4 = result + "";
exp = exp.replaceFirst(s3, s4);
}
}
}
edt.setText(result + "");
}