发布网友 发布时间:2022-07-29 05:31
共1个回答
热心网友 时间:2023-11-19 07:14
private void getResult() {
needclear = true;
String exp = et_showview.getText().toString();
double r = 0;
int space = exp.indexOf(' ');//用于搜索空格位置
String s1 = exp.substring(0, space);//s1用于保存第一个运算数
String op = exp.substring(space + 1, space + 2);//op用于保存运算符
String s2 = exp.substring(space + 3);//s2用于保存第二个运算数
double arg1 = Double.parseDouble(s1);//将运算数从string转换为Single
double arg2 = Double.parseDouble(s2);
if(op.equals("+")){
r = arg1 + arg2;
}else if(op.equals("-")){
r = arg1 - arg2;
}else if(op.equals("×")){
r = arg1 * arg2;
}else if(op.equals("÷")){
if (arg2 == 0)
{
r=0;
}
else
{
r = arg1 / arg2;
}
}
if(!s1.contains(".")&&!s2.contains(".")){
int result = (int)r;
et_showview.setText(result+"");
}else{
et_showview.setText(r+"");
}
}
//
///***
// * @param exp
// * 算数表达式
// * @return 根据表达式返回结果
// */
//private String getRs(String exp) {
//Interpreter bsh = new Interpreter();
//Number result = null;
//try {
//exp = filterExp(exp);
//result = (Number) bsh.eval(exp);
//} catch (EvalError e) {
//e.printStackTrace();
//return "0";
//}
//return result.doubleValue() + "";
//}
//
///**
// * @param exp
// * 算数表达式
// * @return 因为计算过程中,全程需要有小数参与.
// */
//private String filterExp(String exp) {
//String num[] = exp.split("");
//String temp = null;
//double dtemp = 0 ;
//String str_temp="";
//for (int i = 0; i < num.length; i++) {
//temp = num[i];
//
//if (temp.equals("+") || temp.equals("-") || temp.equals("×")
//|| temp.equals("÷")) {
//if(dtemp==0){
//dtemp=Double.parseDouble(str_temp);
//}
//
//str_temp="";
//}else{
//str_temp=str_temp+temp;
//}
//}
//
//int begin = 0, end = 0;
//for (int i = 1; i < num.length; i++) {
//temp = num[i];
//if (temp.matches("[+-/()*]")) {
//if (temp.equals("."))
//continue;
//end = i - 1;
//temp = exp.substring(begin, end);
//if (temp.trim().length() > 0 && temp.indexOf(".") < 0)
//num[i - 1] = num[i - 1] + ".0";
//begin = end + 1;
//}
//}
//return Arrays.toString(num).replaceAll("[\\[\\], ]", "");