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 resetValue = false
|
||||||
private var baseValue: BigDecimal = BigDecimal.ZERO
|
private var baseValue: BigDecimal = BigDecimal.ZERO
|
||||||
private var secondValue: BigDecimal = BigDecimal.ZERO
|
private var secondValue: BigDecimal = BigDecimal.ZERO
|
||||||
private val operations = listOf("+", "-", "*", "/", "^", "%")
|
private val operations = listOf("+", "-", "*", "/", "^", "%", "√")
|
||||||
private var moreOperationsInRaw = false
|
private var moreOperationsInRaw = false
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -57,19 +57,20 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateFormula() {
|
private fun updateFormula() {
|
||||||
val first = baseValue.format()
|
var first = baseValue.format()
|
||||||
val second = secondValue.format()
|
val second = secondValue.format()
|
||||||
val sign = getSign(lastOperation)
|
val sign = getSign(lastOperation)
|
||||||
|
|
||||||
when {
|
if (sign.isNotEmpty()) {
|
||||||
sign == "√" -> setFormula(sign + first)
|
if (secondValue.compareTo(BigDecimal.ZERO) == 0 && sign == "/") {
|
||||||
sign.isNotEmpty() -> {
|
context.toast(context.getString(R.string.formula_divide_by_zero_error))
|
||||||
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)
|
setValue(inputDisplayedFormula)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleRoot() {
|
|
||||||
baseValue = getDisplayedNumberAsDouble()
|
|
||||||
calculateResult()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateResult(update: Boolean = true) {
|
private fun calculateResult(update: Boolean = true) {
|
||||||
if (update) {
|
if (update) {
|
||||||
updateFormula()
|
updateFormula()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
||||||
|
baseValue = 1.toBigDecimal()
|
||||||
|
}
|
||||||
|
|
||||||
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
||||||
if (operation != null) {
|
if (operation != null) {
|
||||||
try {
|
try {
|
||||||
@ -151,26 +151,30 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
inputDisplayedFormula = "0"
|
inputDisplayedFormula = "0"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operation != ROOT) {
|
if (operation == ROOT && inputDisplayedFormula == "0") {
|
||||||
if (inputDisplayedFormula.last() == '+' ||
|
inputDisplayedFormula = "√"
|
||||||
inputDisplayedFormula.last() == '-' ||
|
}
|
||||||
inputDisplayedFormula.last() == '*' ||
|
|
||||||
inputDisplayedFormula.last() == '/' ||
|
if (inputDisplayedFormula.last() == '+' ||
|
||||||
inputDisplayedFormula.last() == '^' ||
|
inputDisplayedFormula.last() == '-' ||
|
||||||
inputDisplayedFormula.last() == '%') {
|
inputDisplayedFormula.last() == '*' ||
|
||||||
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
inputDisplayedFormula.last() == '/' ||
|
||||||
|
inputDisplayedFormula.last() == '^' ||
|
||||||
|
inputDisplayedFormula.last() == '%' ||
|
||||||
|
inputDisplayedFormula.last() == '√') {
|
||||||
|
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
||||||
|
inputDisplayedFormula += getSign(operation)
|
||||||
|
} else {
|
||||||
|
if (!inputDisplayedFormula.contains('+') &&
|
||||||
|
!inputDisplayedFormula.substring(1).contains('-') &&
|
||||||
|
!inputDisplayedFormula.contains('*') &&
|
||||||
|
!inputDisplayedFormula.contains('/') &&
|
||||||
|
!inputDisplayedFormula.contains('^') &&
|
||||||
|
!inputDisplayedFormula.contains('%') &&
|
||||||
|
!inputDisplayedFormula.contains('√')) {
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
} else {
|
} else {
|
||||||
if (!inputDisplayedFormula.contains('+') &&
|
moreOperationsInRaw = true
|
||||||
!inputDisplayedFormula.substring(1).contains('-') &&
|
|
||||||
!inputDisplayedFormula.contains('*') &&
|
|
||||||
!inputDisplayedFormula.contains('/') &&
|
|
||||||
!inputDisplayedFormula.contains('^') &&
|
|
||||||
!inputDisplayedFormula.contains('%')) {
|
|
||||||
inputDisplayedFormula += getSign(operation)
|
|
||||||
} else {
|
|
||||||
moreOperationsInRaw = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,14 +183,15 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
handlePercent()
|
handlePercent()
|
||||||
lastKey = tempOp
|
lastKey = tempOp
|
||||||
lastOperation = tempOp
|
lastOperation = tempOp
|
||||||
} else if (lastKey == DIGIT && operation != ROOT) {
|
} else if (lastKey == DIGIT) {
|
||||||
handleResult()
|
handleResult()
|
||||||
if (inputDisplayedFormula.last() != '+' &&
|
if (inputDisplayedFormula.last() != '+' &&
|
||||||
inputDisplayedFormula.last() != '-' &&
|
inputDisplayedFormula.last() != '-' &&
|
||||||
inputDisplayedFormula.last() != '*' &&
|
inputDisplayedFormula.last() != '*' &&
|
||||||
inputDisplayedFormula.last() != '/' &&
|
inputDisplayedFormula.last() != '/' &&
|
||||||
inputDisplayedFormula.last() != '^' &&
|
inputDisplayedFormula.last() != '^' &&
|
||||||
inputDisplayedFormula.last() != '%') {
|
inputDisplayedFormula.last() != '%' &&
|
||||||
|
inputDisplayedFormula.last() != '√') {
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -195,11 +200,6 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
lastKey = operation
|
lastKey = operation
|
||||||
lastOperation = operation
|
lastOperation = operation
|
||||||
setValue(inputDisplayedFormula)
|
setValue(inputDisplayedFormula)
|
||||||
|
|
||||||
if (operation == ROOT) {
|
|
||||||
handleRoot()
|
|
||||||
resetValue = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handlePercent() {
|
private fun handlePercent() {
|
||||||
@ -227,7 +227,13 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
newValue = newValue.replace("\\.$".toRegex(), "")
|
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)
|
newValue = formatString(newValue)
|
||||||
}
|
}
|
||||||
setValue(newValue)
|
setValue(newValue)
|
||||||
|
@ -14,7 +14,7 @@ object OperationFactory {
|
|||||||
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
|
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
|
||||||
PERCENT -> PercentOperation(baseValue, secondValue)
|
PERCENT -> PercentOperation(baseValue, secondValue)
|
||||||
POWER -> PowerOperation(baseValue, secondValue)
|
POWER -> PowerOperation(baseValue, secondValue)
|
||||||
ROOT -> RootOperation(baseValue)
|
ROOT -> RootOperation(baseValue, secondValue)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package com.simplemobiletools.calculator.operation
|
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.Operation
|
||||||
import com.simplemobiletools.calculator.operation.base.UnaryOperation
|
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
import kotlin.math.sqrt
|
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