mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-06-05 21:49:13 +02:00
reordering some functions to give it more sense
This commit is contained in:
@ -36,52 +36,26 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
showNewResult(inputDisplayedFormula)
|
showNewResult(inputDisplayedFormula)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showNewResult(value: String) {
|
private fun zeroClicked() {
|
||||||
callback!!.showNewResult(value, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showNewFormula(value: String) {
|
|
||||||
callback!!.showNewFormula(value, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateResult() {
|
|
||||||
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
|
||||||
baseValue = 1.0
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lastKey != EQUALS) {
|
|
||||||
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
||||||
val parts = valueToCheck.split(operationsRegex).filter { it.trim().isNotEmpty() }
|
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
||||||
baseValue = parts.first().replace(",", "").toDouble()
|
if (value != "0" || value.contains(".")) {
|
||||||
if (inputDisplayedFormula.startsWith("-")) {
|
addDigit(0)
|
||||||
baseValue *= -1
|
|
||||||
}
|
|
||||||
|
|
||||||
secondValue = parts.getOrNull(1)?.replace(",", "")?.toDouble() ?: secondValue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lastOperation != "") {
|
|
||||||
val expression = "${baseValue.format()}${getSign(lastOperation)}${secondValue.format()}".replace("√", "sqrt")
|
|
||||||
try {
|
|
||||||
val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate()
|
|
||||||
showNewResult(result.format())
|
|
||||||
baseValue = result
|
|
||||||
inputDisplayedFormula = result.format()
|
|
||||||
showNewFormula(expression.replace("sqrt", "√"))
|
|
||||||
} catch (e: Exception) {
|
|
||||||
context.toast(R.string.unknown_error_occurred)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
|
private fun decimalClicked() {
|
||||||
private fun handlePercent() {
|
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
||||||
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
|
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
||||||
val result = operation.getResult()
|
if (!value.contains(".")) {
|
||||||
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
|
when {
|
||||||
inputDisplayedFormula = result.format()
|
value == "0" && !valueToCheck.contains(operationsRegex.toRegex()) -> inputDisplayedFormula = "0."
|
||||||
showNewResult(result.format())
|
value == "" -> inputDisplayedFormula += "0."
|
||||||
baseValue = result
|
else -> inputDisplayedFormula += "."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
showNewResult(inputDisplayedFormula)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleOperation(operation: String) {
|
fun handleOperation(operation: String) {
|
||||||
@ -117,6 +91,16 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
showNewResult(inputDisplayedFormula)
|
showNewResult(inputDisplayedFormula)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
|
||||||
|
private fun handlePercent() {
|
||||||
|
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
|
||||||
|
val result = operation.getResult()
|
||||||
|
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
|
||||||
|
inputDisplayedFormula = result.format()
|
||||||
|
showNewResult(result.format())
|
||||||
|
baseValue = result
|
||||||
|
}
|
||||||
|
|
||||||
fun handleEquals() {
|
fun handleEquals() {
|
||||||
if (lastKey == EQUALS) {
|
if (lastKey == EQUALS) {
|
||||||
calculateResult()
|
calculateResult()
|
||||||
@ -141,26 +125,42 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
return value.toDouble()
|
return value.toDouble()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun decimalClicked() {
|
private fun calculateResult() {
|
||||||
|
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
||||||
|
baseValue = 1.0
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastKey != EQUALS) {
|
||||||
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
||||||
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
val parts = valueToCheck.split(operationsRegex).filter { it.trim().isNotEmpty() }
|
||||||
if (!value.contains(".")) {
|
baseValue = parts.first().replace(",", "").toDouble()
|
||||||
when {
|
if (inputDisplayedFormula.startsWith("-")) {
|
||||||
value == "0" && !valueToCheck.contains(operationsRegex.toRegex()) -> inputDisplayedFormula = "0."
|
baseValue *= -1
|
||||||
value == "" -> inputDisplayedFormula += "0."
|
}
|
||||||
else -> inputDisplayedFormula += "."
|
|
||||||
|
secondValue = parts.getOrNull(1)?.replace(",", "")?.toDouble() ?: secondValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastOperation != "") {
|
||||||
|
val expression = "${baseValue.format()}${getSign(lastOperation)}${secondValue.format()}".replace("√", "sqrt")
|
||||||
|
try {
|
||||||
|
val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate()
|
||||||
|
showNewResult(result.format())
|
||||||
|
baseValue = result
|
||||||
|
inputDisplayedFormula = result.format()
|
||||||
|
showNewFormula(expression.replace("sqrt", "√"))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
context.toast(R.string.unknown_error_occurred)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
showNewResult(inputDisplayedFormula)
|
private fun showNewResult(value: String) {
|
||||||
|
callback!!.showNewResult(value, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun zeroClicked() {
|
private fun showNewFormula(value: String) {
|
||||||
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
callback!!.showNewFormula(value, context)
|
||||||
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
|
||||||
if (value != "0" || value.contains(".")) {
|
|
||||||
addDigit(0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleClear() {
|
fun handleClear() {
|
||||||
|
Reference in New Issue
Block a user