-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_MainActivity.java
More file actions
201 lines (173 loc) · 5.85 KB
/
Copy pathtest_MainActivity.java
File metadata and controls
201 lines (173 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.example.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView display;
private String currentNumber = "";
private String operator = "";
private String firstNumber = "";
private boolean isNewOperation = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化显示屏
display = findViewById(R.id.display);
// 初始化数字按钮
int[] numberButtonIds = {
R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4,
R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9
};
for (int id : numberButtonIds) {
findViewById(id).setOnClickListener(this);
}
// 初始化运算符按钮
findViewById(R.id.btnAdd).setOnClickListener(this);
findViewById(R.id.btnSubtract).setOnClickListener(this);
findViewById(R.id.btnMultiply).setOnClickListener(this);
findViewById(R.id.btnDivide).setOnClickListener(this);
findViewById(R.id.btnEquals).setOnClickListener(this);
findViewById(R.id.btnClear).setOnClickListener(this);
findViewById(R.id.btnDelete).setOnClickListener(this);
findViewById(R.id.btnDot).setOnClickListener(this);
// 初始化显示
display.setText("0");
}
@Override
public void onClick(View v) {
Button button = (Button) v;
String buttonText = button.getText().toString();
switch (v.getId()) {
case R.id.btn0:
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn4:
case R.id.btn5:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
handleNumberInput(buttonText);
break;
case R.id.btnDot:
handleDotInput();
break;
case R.id.btnAdd:
case R.id.btnSubtract:
case R.id.btnMultiply:
case R.id.btnDivide:
handleOperatorInput(buttonText);
break;
case R.id.btnEquals:
handleEqualsInput();
break;
case R.id.btnClear:
handleClearInput();
break;
case R.id.btnDelete:
handleDeleteInput();
break;
}
}
private void handleNumberInput(String number) {
if (isNewOperation) {
currentNumber = number;
isNewOperation = false;
} else {
if (currentNumber.equals("0")) {
currentNumber = number;
} else {
currentNumber += number;
}
}
display.setText(currentNumber);
}
private void handleDotInput() {
if (isNewOperation) {
currentNumber = "0.";
isNewOperation = false;
} else if (!currentNumber.contains(".")) {
if (currentNumber.isEmpty()) {
currentNumber = "0.";
} else {
currentNumber += ".";
}
}
display.setText(currentNumber);
}
private void handleOperatorInput(String op) {
if (!firstNumber.isEmpty() && !operator.isEmpty() && !isNewOperation) {
// 如果有待处理的运算,先计算
handleEqualsInput();
}
firstNumber = currentNumber.isEmpty() ? "0" : currentNumber;
operator = op;
isNewOperation = true;
}
private void handleEqualsInput() {
if (firstNumber.isEmpty() || operator.isEmpty() || currentNumber.isEmpty()) {
return;
}
try {
double num1 = Double.parseDouble(firstNumber);
double num2 = Double.parseDouble(currentNumber);
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "×":
result = num1 * num2;
break;
case "÷":
if (num2 == 0) {
display.setText("错误:除零");
handleClearInput();
return;
}
result = num1 / num2;
break;
}
// 格式化结果显示
String resultString;
if (result == (long) result) {
resultString = String.valueOf((long) result);
} else {
resultString = String.valueOf(result);
}
display.setText(resultString);
currentNumber = resultString;
firstNumber = "";
operator = "";
isNewOperation = true;
} catch (NumberFormatException e) {
display.setText("错误");
handleClearInput();
}
}
private void handleClearInput() {
currentNumber = "";
firstNumber = "";
operator = "";
isNewOperation = true;
display.setText("0");
}
private void handleDeleteInput() {
if (!currentNumber.isEmpty() && !isNewOperation) {
currentNumber = currentNumber.substring(0, currentNumber.length() - 1);
if (currentNumber.isEmpty()) {
currentNumber = "";
display.setText("0");
} else {
display.setText(currentNumber);
}
}
}
}