mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-16 11:31:01 +01:00
implement division
This commit is contained in:
parent
3be2aee4d3
commit
9b1c979be6
@ -23,6 +23,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private static final int PLUS = 2;
|
private static final int PLUS = 2;
|
||||||
private static final int MINUS = 3;
|
private static final int MINUS = 3;
|
||||||
private static final int MULTIPLY = 4;
|
private static final int MULTIPLY = 4;
|
||||||
|
private static final int DIVIDE = 5;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -76,6 +77,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
baseValue = resultValue;
|
baseValue = resultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void divideNumbers() {
|
||||||
|
double resultValue = 0;
|
||||||
|
if (secondValue != 0)
|
||||||
|
resultValue = baseValue / secondValue;
|
||||||
|
|
||||||
|
result.setText(Formatter.doubleToString(resultValue));
|
||||||
|
baseValue = resultValue;
|
||||||
|
}
|
||||||
|
|
||||||
private void handleOperation(int operation) {
|
private void handleOperation(int operation) {
|
||||||
if (lastKey == operation)
|
if (lastKey == operation)
|
||||||
return;
|
return;
|
||||||
@ -105,6 +115,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
handleOperation(MULTIPLY);
|
handleOperation(MULTIPLY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.btn_divide)
|
||||||
|
public void divideClicked() {
|
||||||
|
handleOperation(DIVIDE);
|
||||||
|
}
|
||||||
|
|
||||||
@OnClick(R.id.btn_equals)
|
@OnClick(R.id.btn_equals)
|
||||||
public void equalsClicked() {
|
public void equalsClicked() {
|
||||||
if (lastKey == EQUALS) {
|
if (lastKey == EQUALS) {
|
||||||
@ -120,15 +135,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
lastKey = EQUALS;
|
lastKey = EQUALS;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleEquals() {
|
|
||||||
if (lastOperation == PLUS)
|
|
||||||
addNumbers();
|
|
||||||
else if (lastOperation == MINUS)
|
|
||||||
subtractNumbers();
|
|
||||||
else if (lastOperation == MULTIPLY)
|
|
||||||
multiplyNumbers();
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnClick(R.id.btn_decimal)
|
@OnClick(R.id.btn_decimal)
|
||||||
public void decimalClicked() {
|
public void decimalClicked() {
|
||||||
String value = getDisplayedNumber();
|
String value = getDisplayedNumber();
|
||||||
@ -147,6 +153,25 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
lastKey = DIGIT;
|
lastKey = DIGIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleEquals() {
|
||||||
|
switch (lastOperation) {
|
||||||
|
case PLUS:
|
||||||
|
addNumbers();
|
||||||
|
break;
|
||||||
|
case MINUS:
|
||||||
|
subtractNumbers();
|
||||||
|
break;
|
||||||
|
case MULTIPLY:
|
||||||
|
multiplyNumbers();
|
||||||
|
break;
|
||||||
|
case DIVIDE:
|
||||||
|
divideNumbers();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9})
|
@OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9})
|
||||||
public void digitClicked(View view) {
|
public void digitClicked(View view) {
|
||||||
lastKey = DIGIT;
|
lastKey = DIGIT;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user