mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-04-26 07:48:43 +02:00
fixing the behaviour of Root
This commit is contained in:
parent
c7b1899146
commit
6db8017611
@ -19,7 +19,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
private var resetValue = false
|
||||
private var baseValue: BigDecimal = BigDecimal.ZERO
|
||||
private var secondValue: BigDecimal = BigDecimal.ZERO
|
||||
private val operations = listOf("+", "-", "*", "/", "^", "%")
|
||||
private val operations = listOf("+", "-", "*", "/", "^", "%", "√")
|
||||
private var moreOperationsInRaw = false
|
||||
|
||||
init {
|
||||
@ -57,19 +57,20 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
}
|
||||
|
||||
private fun updateFormula() {
|
||||
val first = baseValue.format()
|
||||
var first = baseValue.format()
|
||||
val second = secondValue.format()
|
||||
val sign = getSign(lastOperation)
|
||||
|
||||
when {
|
||||
sign == "√" -> setFormula(sign + first)
|
||||
sign.isNotEmpty() -> {
|
||||
if (sign.isNotEmpty()) {
|
||||
if (secondValue.compareTo(BigDecimal.ZERO) == 0 && sign == "/") {
|
||||
context.toast(context.getString(R.string.formula_divide_by_zero_error))
|
||||
}
|
||||
|
||||
setFormula(first + sign + second)
|
||||
if (sign == "√" && first == "0") {
|
||||
first = ""
|
||||
}
|
||||
|
||||
setFormula(first + sign + second)
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,16 +124,15 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
setValue(inputDisplayedFormula)
|
||||
}
|
||||
|
||||
private fun handleRoot() {
|
||||
baseValue = getDisplayedNumberAsDouble()
|
||||
calculateResult()
|
||||
}
|
||||
|
||||
private fun calculateResult(update: Boolean = true) {
|
||||
if (update) {
|
||||
updateFormula()
|
||||
}
|
||||
|
||||
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
||||
baseValue = 1.toBigDecimal()
|
||||
}
|
||||
|
||||
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
||||
if (operation != null) {
|
||||
try {
|
||||
@ -151,13 +151,17 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
inputDisplayedFormula = "0"
|
||||
}
|
||||
|
||||
if (operation != ROOT) {
|
||||
if (operation == ROOT && inputDisplayedFormula == "0") {
|
||||
inputDisplayedFormula = "√"
|
||||
}
|
||||
|
||||
if (inputDisplayedFormula.last() == '+' ||
|
||||
inputDisplayedFormula.last() == '-' ||
|
||||
inputDisplayedFormula.last() == '*' ||
|
||||
inputDisplayedFormula.last() == '/' ||
|
||||
inputDisplayedFormula.last() == '^' ||
|
||||
inputDisplayedFormula.last() == '%') {
|
||||
inputDisplayedFormula.last() == '%' ||
|
||||
inputDisplayedFormula.last() == '√') {
|
||||
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
||||
inputDisplayedFormula += getSign(operation)
|
||||
} else {
|
||||
@ -166,27 +170,28 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
!inputDisplayedFormula.contains('*') &&
|
||||
!inputDisplayedFormula.contains('/') &&
|
||||
!inputDisplayedFormula.contains('^') &&
|
||||
!inputDisplayedFormula.contains('%')) {
|
||||
!inputDisplayedFormula.contains('%') &&
|
||||
!inputDisplayedFormula.contains('√')) {
|
||||
inputDisplayedFormula += getSign(operation)
|
||||
} else {
|
||||
moreOperationsInRaw = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) {
|
||||
val tempOp = lastOperation
|
||||
handlePercent()
|
||||
lastKey = tempOp
|
||||
lastOperation = tempOp
|
||||
} else if (lastKey == DIGIT && operation != ROOT) {
|
||||
} else if (lastKey == DIGIT) {
|
||||
handleResult()
|
||||
if (inputDisplayedFormula.last() != '+' &&
|
||||
inputDisplayedFormula.last() != '-' &&
|
||||
inputDisplayedFormula.last() != '*' &&
|
||||
inputDisplayedFormula.last() != '/' &&
|
||||
inputDisplayedFormula.last() != '^' &&
|
||||
inputDisplayedFormula.last() != '%') {
|
||||
inputDisplayedFormula.last() != '%' &&
|
||||
inputDisplayedFormula.last() != '√') {
|
||||
inputDisplayedFormula += getSign(operation)
|
||||
}
|
||||
}
|
||||
@ -195,11 +200,6 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
lastKey = operation
|
||||
lastOperation = operation
|
||||
setValue(inputDisplayedFormula)
|
||||
|
||||
if (operation == ROOT) {
|
||||
handleRoot()
|
||||
resetValue = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun handlePercent() {
|
||||
@ -227,7 +227,13 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
}
|
||||
|
||||
newValue = newValue.replace("\\.$".toRegex(), "")
|
||||
if (!newValue.contains('+') && !newValue.contains('-') && !newValue.contains('*') && !newValue.contains('/') && !newValue.contains('%') && !newValue.contains('^')) {
|
||||
if (!newValue.contains('+') &&
|
||||
!newValue.contains('-') &&
|
||||
!newValue.contains('*') &&
|
||||
!newValue.contains('/') &&
|
||||
!newValue.contains('%') &&
|
||||
!newValue.contains('^') &&
|
||||
!newValue.contains('√')) {
|
||||
newValue = formatString(newValue)
|
||||
}
|
||||
setValue(newValue)
|
||||
|
@ -14,7 +14,7 @@ object OperationFactory {
|
||||
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
|
||||
PERCENT -> PercentOperation(baseValue, secondValue)
|
||||
POWER -> PowerOperation(baseValue, secondValue)
|
||||
ROOT -> RootOperation(baseValue)
|
||||
ROOT -> RootOperation(baseValue, secondValue)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package com.simplemobiletools.calculator.operation
|
||||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import com.simplemobiletools.calculator.operation.base.UnaryOperation
|
||||
import java.math.BigDecimal
|
||||
import kotlin.math.sqrt
|
||||
|
||||
class RootOperation(value: BigDecimal) : UnaryOperation(value), Operation {
|
||||
class RootOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult() = BigDecimal(sqrt(value.toDouble()))
|
||||
override fun getResult() = BigDecimal(sqrt(secondValue.toDouble()).times(baseValue.toDouble()))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user