mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-06-05 21:49:13 +02:00
fixing some negative number related glitches
This commit is contained in:
@ -124,7 +124,9 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateResult(update: Boolean = true) {
|
private fun calculateResult(update: Boolean = true) {
|
||||||
if (update) updateFormula()
|
if (update) {
|
||||||
|
updateFormula()
|
||||||
|
}
|
||||||
|
|
||||||
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
||||||
if (operation != null) {
|
if (operation != null) {
|
||||||
@ -140,12 +142,26 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun handleOperation(operation: String) {
|
fun handleOperation(operation: String) {
|
||||||
|
if (inputDisplayedFormula.isEmpty()) {
|
||||||
|
inputDisplayedFormula = "0"
|
||||||
|
}
|
||||||
|
|
||||||
if (operation != ROOT) {
|
if (operation != ROOT) {
|
||||||
if (inputDisplayedFormula.last() == '+' || inputDisplayedFormula.last() == '-' || inputDisplayedFormula.last() == '*' || inputDisplayedFormula.last() == '/' || inputDisplayedFormula.last() == '^' || inputDisplayedFormula.last() == '%') {
|
if (inputDisplayedFormula.last() == '+' ||
|
||||||
|
inputDisplayedFormula.last() == '-' ||
|
||||||
|
inputDisplayedFormula.last() == '*' ||
|
||||||
|
inputDisplayedFormula.last() == '/' ||
|
||||||
|
inputDisplayedFormula.last() == '^' ||
|
||||||
|
inputDisplayedFormula.last() == '%') {
|
||||||
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
} else {
|
} else {
|
||||||
if (!inputDisplayedFormula.contains('+') && !inputDisplayedFormula.contains('-') && !inputDisplayedFormula.contains('*') && !inputDisplayedFormula.contains('/') && !inputDisplayedFormula.contains('^') && !inputDisplayedFormula.contains('%')) {
|
if (!inputDisplayedFormula.contains('+') &&
|
||||||
|
!inputDisplayedFormula.substring(1).contains('-') &&
|
||||||
|
!inputDisplayedFormula.contains('*') &&
|
||||||
|
!inputDisplayedFormula.contains('/') &&
|
||||||
|
!inputDisplayedFormula.contains('^') &&
|
||||||
|
!inputDisplayedFormula.contains('%')) {
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
} else {
|
} else {
|
||||||
moreOperationsInRaw = true
|
moreOperationsInRaw = true
|
||||||
@ -160,6 +176,14 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
lastOperation = tempOp
|
lastOperation = tempOp
|
||||||
} else if (lastKey == DIGIT && operation != ROOT && operation != FACTORIAL) {
|
} else if (lastKey == DIGIT && operation != ROOT && operation != FACTORIAL) {
|
||||||
handleResult()
|
handleResult()
|
||||||
|
if (inputDisplayedFormula.last() != '+' &&
|
||||||
|
inputDisplayedFormula.last() != '-' &&
|
||||||
|
inputDisplayedFormula.last() != '*' &&
|
||||||
|
inputDisplayedFormula.last() != '/' &&
|
||||||
|
inputDisplayedFormula.last() != '^' &&
|
||||||
|
inputDisplayedFormula.last() != '%') {
|
||||||
|
inputDisplayedFormula += getSign(operation)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resetValue = true
|
resetValue = true
|
||||||
@ -170,8 +194,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
if (operation == ROOT) {
|
if (operation == ROOT) {
|
||||||
handleRoot()
|
handleRoot()
|
||||||
resetValue = false
|
resetValue = false
|
||||||
}
|
} else if (operation == FACTORIAL) {
|
||||||
if (operation == FACTORIAL) {
|
|
||||||
handleFactorial()
|
handleFactorial()
|
||||||
resetValue = false
|
resetValue = false
|
||||||
}
|
}
|
||||||
@ -206,12 +229,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
newValue = formatString(newValue)
|
newValue = formatString(newValue)
|
||||||
}
|
}
|
||||||
setValue(newValue)
|
setValue(newValue)
|
||||||
if (newValue != "0") {
|
inputDisplayedFormula = if (newValue != "0") newValue else ""
|
||||||
inputDisplayedFormula = newValue
|
|
||||||
} else {
|
|
||||||
inputDisplayedFormula = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,7 +247,13 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
if (lastKey != DIGIT)
|
if (lastKey != DIGIT)
|
||||||
return
|
return
|
||||||
|
|
||||||
displayedNumber = displayedNumber!!.substring(displayedNumber!!.indexOfAny(operations, 0, false) + 1)
|
val numberToCheck = if (inputDisplayedFormula.startsWith("-")) {
|
||||||
|
inputDisplayedFormula.substring(1)
|
||||||
|
} else {
|
||||||
|
inputDisplayedFormula
|
||||||
|
}
|
||||||
|
|
||||||
|
displayedNumber = numberToCheck.substring(numberToCheck.indexOfAny(operations, 0, false) + 1)
|
||||||
secondValue = getDisplayedNumberAsDouble()
|
secondValue = getDisplayedNumberAsDouble()
|
||||||
calculateResult()
|
calculateResult()
|
||||||
lastKey = EQUALS
|
lastKey = EQUALS
|
||||||
|
Reference in New Issue
Block a user