public class MainActivity extends AppCompatActivity {
private Button button1, button2, button3, button_div, button_7, button_8, button_9, button_mul, button_4, button_5, button_6, button_sub, button_1, button_2, button_3, button_add, button_0, button_dot, button_equal;
private EditText et_res;
private final String TAG = "CAL";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button_div = (Button) findViewById(R.id.button_div);
button_7 = (Button) findViewById(R.id.button_7);
button_8 = (Button) findViewById(R.id.button_8);
button_9 = (Button) findViewById(R.id.button_9);
button_4 = (Button) findViewById(R.id.button_4);
button_5 = (Button) findViewById(R.id.button_5);
button_6 = (Button) findViewById(R.id.button_6);
button_1 = (Button) findViewById(R.id.button_1);
button_2 = (Button) findViewById(R.id.button_2);
button_3 = (Button) findViewById(R.id.button_3);
button_add = (Button) findViewById(R.id.button_add);
button_0 = (Button) findViewById(R.id.button_0);
button_dot = (Button) findViewById(R.id.button_dot);
button_equal = (Button) findViewById(R.id.button_equal);
button_mul = (Button) findViewById(R.id.button_mul);
button_sub = (Button) findViewById(R.id.button_sub);
et_res = (EditText) findViewById(R.id.et_res);
}
private class MyOnClickLis implements View.OnClickListener {
private String str1 = "0";//第一个操作数
private String str2 = "";//第二个操作数
private String oper = "";//当前的运算符
private String strResult = "0";//当前显示的结果
private int lastInput = 0;//最后输入的值,0=数字,1=运算符
private void numInput(int num) {
if (strResult.equals("0")) {
strResult = " " + num;
} else if (lastInput == 0) {
strResult = strResult + num;
} else if (lastInput == 1) {
strResult = " " + num;
}
et_res.setText(strResult);
lastInput = 0;
}
private void dotInput() {
if (strResult.indexOf(".") == -1) {
strResult = strResult + ".";
et_res.setText(strResult);
}
}
private void operationInput(String op) {
if (lastInput == 1) {
oper = op;
lastInput = 1;
return;
}
if (oper.isEmpty()) {
oper = op;
str1 = strResult;
} else if (!str1.isEmpty()) {
str2 = strResult;
}
if (op.equals("+")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 + op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
} else if (op.equals("-")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 - op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
} else if (op.equals("*")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 * op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
} else if (op.equals("/")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
if (op2 == 0) {
et_res.setText("错误");
} else if (op2 != 0) {
strResult = String.valueOf(op1 / op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
}
}
}
private void percentInput () {
double op1 = Double.parseDouble(strResult);
strResult = Double.toString(op1 / 100);
et_res.setText(strResult);
}
private void acInput(){
strResult = "0";
lastInput = 0;
oper = "";
str1 = str2 = "0";
et_res.setText(strResult);
}
private void psInput(){
double op1=Double.parseDouble(strResult);
strResult=Double.toString(-op1);
et_res.setText(strResult);
}
private void equalInput(){
if (oper.isEmpty()) {
return;
}
if (lastInput == 1 && !str1.isEmpty()) {
if (oper.equals("+")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 + op2);
et_res.setText(strResult);
} else if (oper.equals("-")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 - op2);
et_res.setText(strResult);
} else if (oper.equals("*")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 * op2);
et_res.setText(strResult);
} else if (oper.equals("/")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 / op2);
et_res.setText(strResult);
}
} else if (lastInput == 0 && !str2.isEmpty()) {
if (oper.equals("+")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 + op2);
et_res.setText(strResult);
} else if (oper.equals("-")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 - op2);
et_res.setText(strResult);
} else if (oper.equals("*")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 * op2);
et_res.setText(strResult);
} else if (oper.equals("/")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 / op2);
et_res.setText(strResult);
}
}
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button_0:
numInput(0);
break;
case R.id.button_1:
numInput(1);
break;
case R.id.button_2:
numInput(2);
break;
case R.id.button_3:
numInput(3);
break;
case R.id.button_4:
numInput(4);
break;
case R.id.button_5:
numInput(5);
break;
case R.id.button_6:
numInput(6);
break;
case R.id.button_7:
numInput(7);
break;
case R.id.button_8:
numInput(8);
break;
case R.id.button_9:
numInput(9);
break;
case R.id.button_add:
operationInput("+");
break;
case R.id.button_sub:
operationInput("-");
break;
case R.id.button_mul:
operationInput("*");
break;
case R.id.button_div:
operationInput("/");
break;
case R.id.button1:
acInput();
break;
case R.id.button2:
psInput();
break;
case R.id.button3:
percentInput();
break;
case R.id.button_dot:
dotInput();
break;
case R.id.button_equal:
equalInput();
break;
}
}
}
}